http://acm.hdu.edu.cn/showproblem.php?pid=1385
典型的floyd。难点在于按照字典序输出路径。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int INF = 1e9;
int n,map[111][111],path[111][111],tax[111];
void floyd(){
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
path[i][j] = j;
for(int k=1; k<=n; k++){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
int tmp = map[i][k] + map[k][j] + tax[k];
if(map[i][j] > tmp){
map[i][j] = tmp;
path[i][j] = path[i][k];
}
else if(tmp == map[i][j]){
if(path[i][j] > path[i][k])
path[i][j] = path[i][k];
}
}
}
}
}
int main(){
// freopen("in.txt", "r", stdin);
while(scanf("%d",&n) == 1 && n){
int tmp;
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
scanf("%d",&tmp);
if(tmp == -1) map[i][j] = INF;
else map[i][j] = tmp;
}
}
for(int i=1; i<=n; i++)
scanf("%d",&tax[i]);
floyd();
int s,e;
while(scanf("%d%d",&s,&e) == 2){
if(s==-1 && e==-1) break;
printf("From %d to %d :\n",s,e);
printf("Path: %d",s);
int temp = s;
while(temp != e)
{
printf("-->%d",path[temp][e]);
temp=path[temp][e];
}
printf("\n");
printf("Total cost : %d\n\n",map[s][e]);
}
}
return 0;
}