题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1385
大佬博客:https://blog.youkuaiyun.com/u013480600/article/details/37737677
题意:
给你一个无向图,现在要你输出特定起点到终点的最短距离以及字典序最小的路径.不过本题的两路径之间的距离计算方式与常规不同. 从原点到终点的花费==每条边的花费+路径中每个点的cost值(除去源点和终点)
代码:结果错误了很多次,注意看输出结果给的间距
#include<cstdio>
#include<cstring>
#define INF 1e9
using namespace std;
const int maxn = 100+20;
int n;
int cost[maxn];
int dist[maxn][maxn];//最短距离
int path[maxn][maxn];//path[i][j]保存了从i到j路径的第一个点(除i以外)
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++)
{
if(dist[i][k]<INF&&dist[k][j]<INF)
{
if(dist[i][j]>dist[i][k]+dist[k][j]+cost[k])
{
dist[i][j]=dist[i][k]+dist[k][j]+cost[k];
path[i][j]=path[i][k];
}
else if(dist[i][j]==dist[i][k]+dist[k][j]+cost[k]&&path[i][j]>path[i][k])
{
path[i][j]=path[i][k];
}
}
}
}
int main()
{
while(scanf("%d",&n)==1&&n!=0)
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
scanf("%d",&dist[i][j]);
if(dist[i][j]==-1) dist[i][j]=INF;
}
for(int i=1;i<=n;i++)
{
scanf("%d",&cost[i]);
}
floyd();
int x,y;
while(scanf("%d%d",&x,&y))
{
if(x==-1&&y==-1)
break;
printf("From %d to %d :\n",x,y);
if(x!=y)
{
printf("Path: %d",x);
int beg=path[x][y];
while(1)
{
printf("-->%d",beg);
if(beg==y)
{
printf("\n");
break;
}
beg=path[beg][y];
}
}
else
{
printf("Path: %d\n",x);
}
printf("Total cost : %d\n\n",dist[x][y]);
}
}
return 0;
}