别琢磨中间过程,我也整不清楚,死记住模板吧
#include <stdio.h> #include <string.h> #include <queue> using namespace std; const int INF = 0x3f3f3f3f; const int Max_N = 10002; struct Node { int v; double w; int next; }edge[Max_N*10]; int head[Max_N]; bool vis[Max_N]; double dis[Max_N]; int n,m,num; void add_edge(int u,int v,double w) { edge[num].v = v; edge[num].w = w; edge[num].next = head[u]; head[u] = num++; } void SPFA() { int u,i,v; queue<int>Q; for(i=0;i<=n;i++) { dis[i] = - INF; } memset(vis,false,sizeof(vis)); dis[1] = 1.0; vis[1] = true; Q.push(1); while(!Q.empty()) { u = Q.front(); Q.pop(); vis[u] = false; for(i=head[u];i!=-1;i=edge[i].next) { v = edge[i].v; double w = edge[i].w; if(dis[v]<dis[u]*w) { dis[v] = dis[u] * w; if(!vis[v]) { vis[v] = true; Q.push(v); } } } } } int main() { int T; scanf("%d",&T); while(T--) { num = 0; int i,j,a,b; double c; memset(head,-1,sizeof(head)); scanf("%d%d",&n,&m); for(i=1;i<=m;i++) { scanf("%d%d%lf",&a,&b,&c); add_edge(a,b,c/100); add_edge(b,a,c/100); } SPFA(); printf("%lf ",dis[n]*100); } return 0; }