http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1456
path[a][b]始终保存从a--->b的第二个节点。
floyd算法:
#include<cstdio> #include<cstring> #include<string> #include<queue> #include<algorithm> #include<queue> #include<stack> #include<iostream> #include<list> #include<set> #include<cmath> #define INF 0x7fffffff #define eps 1e-6 using namespace std; #define maxn 1002 #define MM 1000000 int n; int map[maxn][maxn]; int b[maxn]; int path[maxn][maxn]; //保存路径 int tax[maxn]; void init() { int i,j; for(i=1;i<=n;i++) for(j=1;j<=n;j++) { path[i][j]=j; } } void floyd() { int i,j,k; for(k=1;k<=n;k++) for(i=1;i<=n;i++) { if(map[i][k]==MM) //小小的优化 continue; for(j=1;j<=n;j++) if(map[i][j]>map[i][k]+map[k][j]+tax[k]) { map[i][j]=map[i][k]+map[k][j]+tax[k]; path[i][j]=path[i][k]; } else if(map[i][j]==map[i][k]+map[k][j]+tax[k]) //避免漏掉字典序更小的 { path[i][j]=min(path[i][j],path[i][k]); } } } int main() { int len,u,v; while(scanf("%d",&n)!=EOF && n) { init(); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { scanf("%d",&map[i][j]); if(map[i][j]==-1) map[i][j]=MM; } for(int i=1;i<=n;i++) scanf("%d",&tax[i]); floyd(); while(1) { scanf("%d%d",&u,&v); if(v+u==-2) break; printf("From %d to %d :\n",u,v); int tmp=u; printf("Path: %d",u); while(tmp!=v) { printf("-->%d",path[tmp][v]); tmp=path[tmp][v]; } cout<<endl; cout<<"Total cost : "<<map[u][v]<<endl<<endl; //注意格式 } } return 0; }
zoj1456 Minimum Transport Cost,布布扣,bubuko.com
zoj1456 Minimum Transport Cost
原文:http://blog.csdn.net/u013712847/article/details/38639055