标签:des style blog http color io os ar java
3 3 0 2 0 2 5 0 1 4 1 2 2
6 1
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 55; 18 struct arc { 19 int to,w,next; 20 arc(int x = 0,int y = 0,int z = -1) { 21 to = x; 22 w = y; 23 next = z; 24 } 25 }; 26 arc e[10000]; 27 int head[maxn],d[maxn][2],cnt[maxn][2],tot,big,ed,n,m; 28 bool vis[maxn][2]; 29 void add(int u,int v,int w){ 30 e[tot] = arc(v,w,head[u]); 31 head[u] = tot++; 32 } 33 void dijkstra(){ 34 for(int i = 0; i <= n; i++){ 35 vis[i][0] = vis[i][1] = false; 36 cnt[i][0] = cnt[i][1] = 0; 37 d[i][0] = d[i][1] = INF; 38 } 39 d[big][0] = 0; 40 cnt[big][0] = 1; 41 while(true){ 42 int u,Min = INF,flag; 43 for(int i = 0; i < n; i++){ 44 if(!vis[i][0] && d[i][0] < Min) 45 Min = d[u = i][flag = 0]; 46 else if(!vis[i][1] && d[i][1] < Min) 47 Min = d[u = i][flag = 1]; 48 } 49 if(u == ed && flag || Min == INF) break; 50 vis[u][flag] = true; 51 for(int i = head[u]; ~i; i = e[i].next){ 52 int v = e[i].to; 53 int w = d[u][flag] + e[i].w; 54 if(d[v][0] > w){ 55 if(d[v][0] < INF){ 56 d[v][1] = d[v][0]; 57 cnt[v][1] = cnt[v][0]; 58 } 59 d[v][0] = w; 60 cnt[v][0] = cnt[u][flag]; 61 }else if(d[v][0] == w) 62 cnt[v][0] += cnt[u][flag]; 63 else if(d[v][1] > w){ 64 d[v][1] = w; 65 cnt[v][1] = cnt[u][flag]; 66 }else if(d[v][1] == w) cnt[v][1] += cnt[u][flag]; 67 } 68 } 69 printf("%d %d\n",d[ed][1],cnt[ed][1]); 70 } 71 int main(){ 72 int u,v,w; 73 while(~scanf("%d %d %d %d",&n,&m,&big,&ed)){ 74 memset(head,-1,sizeof(head)); 75 for(int i = tot = 0; i < m; i++){ 76 scanf("%d %d %d",&u,&v,&w); 77 add(u,v,w); 78 } 79 dijkstra(); 80 } 81 return 0; 82 }
HDU 3191 How Many Paths Are There
标签:des style blog http color io os ar java
原文地址:http://www.cnblogs.com/crackpotisback/p/4001145.html