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\n",dis[f][2],path[f][2]);
120     }
121     return 0;
122 }
View Code

 

posted @ 2014-05-06 18:58  null1019  阅读(174)  评论(0编辑  收藏  举报