HDU1385 Minimum Transport Cost 最短路+输出路径★floyd中的路径dp

题意:给定有向图,输出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;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值