水题 RE了N久 后来发现是无向图
1 #include <iostream> 2 #include<cstring> 3 #include<algorithm> 4 #include<stdlib.h> 5 #include<vector> 6 #include<cstdio> 7 #include<queue> 8 using namespace std; 9 #define N 2000010 10 #define INF 0xfffffff 11 struct node 12 { 13 int u,v,next; 14 double w; 15 }ed[N<<1]; 16 int t,head[N],vis[N],dis[N],pa[N],o[N]; 17 double p[N]; 18 int n,m; 19 void init() 20 { 21 t = 0; 22 memset(head,-1,sizeof(head)); 23 } 24 void add(int u,int v,int w) 25 { 26 ed[t].u = u; 27 ed[t].v = v; 28 ed[t].w = w; 29 ed[t].next = head[u]; 30 head[u] = t++; 31 } 32 void spfa(int s,int e) 33 { 34 memset(vis,0,sizeof(vis)); 35 int i; 36 for(i = 1; i <= n ; i++) 37 { 38 dis[i] = INF; 39 p[i] = 0; 40 } 41 queue<int>q; 42 dis[s] = 1; 43 p[s] = 1; 44 q.push(s); 45 while(!q.empty()) 46 { 47 int u = q.front(); 48 q.pop(); 49 vis[u] = 0; 50 for(i = head[u] ; i != -1 ; i = ed[i].next) 51 { 52 int v = ed[i].v; 53 double w = ed[i].w; 54 if(dis[v]>=dis[u]+1) 55 { 56 if(dis[v]==dis[u]+1) 57 { 58 if(p[v]<p[u]*w/100.0) 59 { 60 p[v] = p[u]*w/100.0; 61 pa[v] = u; 62 } 63 } 64 else 65 { 66 dis[v] = dis[u]+1; 67 p[v] = p[u]*w/100.0; 68 pa[v] = u; 69 } 70 if(!vis[v]) 71 { 72 vis[v] = 1; 73 q.push(v); 74 } 75 } 76 } 77 } 78 printf("%d %lf ",dis[e],1-p[e]); 79 int x = pa[e],g=1; 80 o[g] = e; 81 while(x!=s) 82 { 83 g++; 84 o[g] = x; 85 x = pa[x]; 86 } 87 g++; 88 o[g] = s; 89 for(i = g ; i > 1; i--) 90 printf("%d ",o[i]); 91 printf("%d ",o[1]); 92 } 93 int main() 94 { 95 int i;init(); 96 int a,b; 97 scanf("%d%d",&n,&m); 98 scanf("%d%d",&a,&b); 99 for(i = 1; i <= m ; i++) 100 { 101 int u,v; 102 double w; 103 scanf("%d%d%lf",&u,&v,&w); 104 add(u,v,100-w); 105 add(v,u,100-w); 106 } 107 spfa(a,b); 108 return 0; 109 }