• hdu 3191 How Many Paths Are There


    http://acm.hdu.edu.cn/showproblem.php?pid=3191

    这道题求次短路经和路径数

      1 #include <cstdio>
      2 #include <cstring>
      3 #include <vector>
      4 #include <queue>
      5 #include <algorithm>
      6 #define maxn 2000
      7 using namespace std;
      8 const int inf=1<<30;
      9 struct edge
     10 {
     11     int v,w;
     12 };
     13 
     14 struct node
     15 {
     16     int d,v;
     17     int mark;
     18     bool operator < (const node &a)const
     19     {
     20         if(d!=a.d)
     21             return d>a.d;
     22         return v>a.v;
     23     }
     24 };
     25 
     26 vector<edge>edges[maxn];
     27 int dis[maxn][3];
     28 int vis[maxn][3];
     29 int path[maxn][3];
     30 int n,m,a,b,c,s1,f;
     31 node st;
     32 
     33 
     34 void inti()
     35 {
     36     for(int i=0; i<=n; i++)
     37     {
     38         dis[i][1]=dis[i][2]=inf;
     39     }
     40     memset(path,0,sizeof(path));
     41     memset(vis,false,sizeof(vis));
     42 }
     43 
     44 void dijkstra(int s,int e)
     45 {
     46     inti();
     47     priority_queue<node>q;
     48     dis[s][1]=0;
     49     path[s][1]=1;
     50     memset(vis,false,sizeof(vis));
     51     st.d=0;
     52     st.v=s;
     53     st.mark=1;
     54     q.push(st);
     55     while(!q.empty())
     56     {
     57         node st1=q.top();
     58         q.pop();
     59         if(vis[st1.v][st1.mark]) continue;
     60         vis[st1.v][st1.mark]=true;
     61         for(int i=0; i<(int)edges[st1.v].size(); i++)
     62         {
     63             int v1=edges[st1.v][i].v;
     64             int w1=edges[st1.v][i].w;
     65             if(!vis[v1][1]&&st1.d+w1<dis[v1][1])
     66             {
     67                 if(dis[v1][1]!=inf)
     68                 {
     69                     dis[v1][2]=dis[v1][1];
     70                     path[v1][2]=path[v1][1];
     71                     st.d=dis[v1][2];
     72                     st.v=v1;
     73                     st.mark=2;
     74                     q.push(st);
     75                 }
     76                 dis[v1][1]=st1.d+w1;
     77                 path[v1][1]=path[st1.v][st1.mark];
     78                 st.v=v1;
     79                 st.mark=1;
     80                 st.d=dis[v1][1];
     81                 q.push(st);
     82             }
     83             else if(!vis[v1][1]&&st1.d+w1==dis[v1][1])
     84             {
     85                 path[v1][1]+=path[st1.v][st1.mark];
     86             }
     87             else if(!vis[v1][2]&&st1.d+w1<dis[v1][2])
     88             {
     89                 dis[v1][2]=st1.d+w1;
     90                 path[v1][2]=path[st1.v][st1.mark];
     91                 st.d=dis[v1][2];
     92                 st.v=v1;
     93                 st.mark=2;
     94                 q.push(st);
     95             }
     96             else if(!vis[v1][2]&&st1.d+w1==dis[v1][2])
     97             {
     98                 path[v1][2]+=path[st1.v][st1.mark];
     99             }
    100         }
    101     }
    102 }
    103 
    104 int main()
    105 {
    106     while(scanf("%d%d%d%d",&n,&m,&s1,&f)!=EOF)
    107     {
    108         inti();
    109         for(int i=0; i<=n; i++) edges[i].clear();
    110         for(int i=0; i<m; i++)
    111         {
    112             scanf("%d%d%d",&a,&b,&c);
    113             edge m1;
    114             m1.v=b;
    115             m1.w=c;
    116             edges[a].push_back(m1);
    117         }
    118         dijkstra(s1,f);
    119         printf("%d %d
    ",dis[f][2],path[f][2]);
    120     }
    121     return 0;
    122 }
    View Code
  • 相关阅读:
    希尔排序
    基数排序
    spring8——AOP之Bean的自动代理生成器
    spring7——AOP之通知和顾问
    spring6——AOP的编程术语
    spring5——Aop的实现原理(动态代理)
    spring4——IOC之基于注解的依赖注入(DI )
    spring3——IOC之基于XML的依赖注入(DI )
    spring2——IOC之Bean的装配
    spring1——IOC之原理
  • 原文地址:https://www.cnblogs.com/fanminghui/p/3712207.html
Copyright © 2020-2023  润新知