hdu 1385 Minimum Transport Cost (最小字典序最短路径)

题目链接:   hdu 1385

题目大意:  给出N个点的邻接矩阵,求任意两点的最短路径

                  若有多条路径,输出字典序最小的路径

解题思路:   边为-1的时候,换成INF,用Floyd求出最短路径,path[ i ][ j ]表示路径i到j经过的点

                  dis[ i ][ j ]=Min( dis[ i ][ j ],dis[ i ][ k ] + dis[ k ][ j ] ),更新了dis[ i ][ j ]之后,记录path[ i ][ j ]=path[ i ][ k ]

代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 205
#define INF 0x3f3f3f3f
int dis[MAX][MAX],path[MAX][MAX];
int main()
{
    int i,j,n,m,k,t,V[MAX],a,b;
    while(scanf("%d",&n)!=EOF&&n)
    {
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                path[i][j]=j;
                scanf("%d",&dis[i][j]);
                if(dis[i][j]==-1)
                    dis[i][j]=INF;
            }
        }
        for(i=1;i<=n;i++)
            scanf("%d",&V[i]);
        for(k=1;k<=n;k++)
        {
            for(i=1;i<=n;i++)
            {
                for(j=1;j<=n;j++)
                {
                    t=dis[i][k]+dis[k][j]+V[k];
                    if(t<dis[i][j])
                    {
                        dis[i][j]=t;
                        path[i][j]=path[i][k];     //更新
                    } 
                    else if(t==dis[i][j]&&path[i][k]<path[i][j])  //最小字典序
                        path[i][j]=path[i][k];
                }
            }
        }
        while(scanf("%d%d",&a,&b))
        {
            if(a==-1&&b==-1)
                break;
            printf("From %d to %d :\nPath: %d",a,b,a);
            m=a;
            while(m!=b)
            {
                printf("-->%d",path[m][b]);
                m=path[m][b];
            }
            printf("\nTotal cost : %d\n",dis[a][b]);
            puts("");
        }
    }
    return 0;
}


阅读更多
换一批

Minimum Transport Cost

02-23

These are N cities in Spring country. Between each pair of cities there may be one transportation track or none. Now there is some cargo that should be delivered from one city to another. The transportation fee consists of two parts:nThe cost of the transportation on the path between these cities, andnna certain tax which will be charged whenever any cargo passing through one city, except for the source and the destination cities.nnYou must write a program to find the route which has the minimum cost.nnnInputnnFirst is N, number of cities. N = 0 indicates the end of input.nnThe data of path cost, city tax, source and destination cities are given in the input, which is of the form:nna11 a12 ... a1Nna21 a22 ... a2Nn...............naN1 aN2 ... aNNnb1 b2 ... bNnnc dne fn...ng hnnwhere aij is the transport cost from city i to city j, aij = -1 indicates there is no direct path between city i and city j. bi represents the tax of passing through city i. And the cargo is to be delivered from city c to city d, city e to city f, ..., and g = h = -1. You must output the sequence of cities passed by and the total cost which is of the form:nnnOutputnnFrom c to d :nPath: c-->c1-->......-->ck-->dnTotal cost : ......n......nnFrom e to f :nPath: e-->e1-->..........-->ek-->fnTotal cost : ......nnNote: if there are more minimal paths, output the lexically smallest one. Print a blank line after each test case.nnnSample Inputnn5n0 3 22 -1 4n3 0 5 -1 -1n22 5 0 9 20n-1 -1 9 0 4n4 -1 20 4 0n5 17 8 3 1n1 3n3 5n2 4n-1 -1n0nnnSample OutputnnFrom 1 to 3 :nPath: 1-->5-->4-->3nTotal cost : 21nnFrom 3 to 5 :nPath: 3-->4-->5nTotal cost : 16nnFrom 2 to 4 :nPath: 2-->1-->5-->4nTotal cost : 17

没有更多推荐了,返回首页