http://acm.split.hdu.edu.cn/showproblem.php?pid=1385
题目大意:
给你城市 i --> j 的矩阵表示 值为-1时表示没有路 否则值为路长 经过每个节点要交一次关税 给起点和终点问最少花费 花费 = 路长 + 关税 并输出字典序最小的路径
分析:
Floyd + 路径记录 路径记录中加个判断保存字典序最小
AC代码:
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include<list>
#include <bitset>
#include <climits>
#include <algorithm>
#define gcd(a,b) __gcd(a,b)
#define mset(a,n) memset(a,n,sizeof(a))
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w",stdout)
typedef long long LL;
const LL mod=1e9+7;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
using namespace std;
int n;
int map0[105][105];
int dis[105][105];
int path[105][105];
int b[105];
void floyd(){
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++){
dis[i][j]=map0[i][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++){
if (dis[i][j]>dis[i][k]+dis[k][j]+b[k])
{
dis[i][j]=dis[i][k]+dis[k][j]+b[k];
path[i][j]=path[i][k];
}
else if (dis[i][j]==dis[i][k]+dis[k][j]+b[k]&&path[i][j]>path[i][k]){// 判断
path[i][j]=path[i][k];
}
}
}
}
}
int main (){
while (scanf ("%d",&n)&&n){
int x;
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++){
scanf ("%d",&map0[i][j]);
if (map0[i][j]==-1) map0[i][j]=INF;
}
}
for (int i=1;i<=n;i++) scanf ("%d",&b[i]);
floyd();
int u,v;
while (scanf ("%d%d",&u,&v)&&u!=-1&&v!=-1){
printf("From %d to %d :\n",u,v);
printf("Path: ");
printf ("%d",u);
int temp=u;
while (temp!=v){
printf("-->%d",path[temp][v]);
temp=path[temp][v];
}
putchar('\n');
printf("Total cost : %d\n\n", dis[u][v]);
}
}
return 0;
}