题意:给定有向图,输出s->t ,的最短路权值,和权值最小下字典序最小的路径
思路:floyd,dp[][]扩展到路径字典序上
#include"stdio.h"
#include"string.h"
int n;
int tax[111];
int map[111][111];
int path[111][111];
void floyd()
{
int temp;
int k,i,l;
for(i=1;i<=n;i++)
for(l=1;l<=n;l++)
path[i][l]=l;
for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
{
for(l=1;l<=n;l++)
{
temp=map[i][k]+map[k][l]+tax[k];
if(temp<map[i][l])
{
map[i][l]=temp;
path[i][l]=path[i][k];
}
else if(temp==map[i][l])
{
if(path[i][l]>path[i][k])
path[i][l]=path[i][k];
}
}
}
}
}
int main()
{
int i,l;
int temp;
int s,e;
while(scanf("%d",&n),n)
{
for(i=1;i<=n;i++)
for(l=1;l<=n;l++)
{
scanf("%d",&temp);
if(temp==-1) map[i][l]=11111111;
else map[i][l]=temp;
}
for(i=1;i<=n;i++) scanf("%d",&tax[i]);
floyd();
while(scanf("%d%d",&s,&e)!=-1)
{
if(s==-1 && e==-1) break;
printf("From %d to %d :\n",s,e);
printf("Path: %d",s);
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;
}
本文介绍了使用Floyd算法扩展为路径字典序最小的求解过程,包括输入参数解释、算法实现及实例演示。
615

被折叠的 条评论
为什么被折叠?



