Topic link: Minimum cost
The fee for transferring A to B is x1, and the fee for transferring B to C is x2, then the fee for transferring from A to C is x1 * x2;
if(dis[to] < dis[now] * e[i].w) dis[to] = dis[now] * e[i].w;
code show as below
#include<iostream> #include<cstdio> #include<bits/stdc++.h> using namespace std; const int N = 101000; const int maxn = 2018; struct Node{ int do; int to; double w; }e[N<<1]; int head[maxn]; double dis[maxn]; int n, m, x, y, st, en, val, cnt; bool vis[maxn]; void init() { memset(head,-1,sizeof(head)); memset(vis,0,sizeof(vis)); cnt = 0; } void add(int u,int v,double val) { e [cnt] .to = v; e [cnt] .ne = head [u]; e[cnt].w = 1 - val; // remaining fee head[u] = cnt ++; } void SPFA() { queue<int>q; q.push(st); memset(dis,0,sizeof(dis)); vis [st] = 1; dis [st] = 1; while(!q.empty()) { int now = q.front(); q.pop(); for(int i=head[now];~i;i=e[i].ne) { int to = e[i].to; if(dis[to] < dis[now] * e[i].w ) { dis[to] = dis[now] * e[i].w; if(!vis[to]) { q.push(to); vis [to] = 1; } } } vis[now] = 0; } } intmain() { scanf("%d%d",&n,&m); init(); for(int i=1;i<=m;i++) { scanf("%d%d%d",&x,&y,&val); add(x,y,double(val)/100); add(y,x,double(val)/100); } scanf("%d%d",&st,&en); SPFA(); double ans = 100/dis[en]; printf("%.8lf\n",ans); return 0; }Note the initialization and double! ! !