HDU-1385 Minimum Transport Cost(最短路[Dijkstra])

21 篇文章 0 订阅

Minimum Transport Cost

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)


Problem Description
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: 
The cost of the transportation on the path between these cities, and

a certain tax which will be charged whenever any cargo passing through one city, except for the source and the destination cities.

You must write a program to find the route which has the minimum cost.
 

Input
First is N, number of cities. N = 0 indicates the end of input.

The data of path cost, city tax, source and destination cities are given in the input, which is of the form:

a11 a12 ... a1N
a21 a22 ... a2N
...............
aN1 aN2 ... aNN
b1 b2 ... bN

c d
e f
...
g h

where 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:
 

Output
From c to d :
Path: c-->c1-->......-->ck-->d
Total cost : ......
......

From e to f :
Path: e-->e1-->..........-->ek-->f
Total cost : ......

Note: if there are more minimal paths, output the lexically smallest one. Print a blank line after each test case.

 

Sample Input
  
  
5 0 3 22 -1 4 3 0 5 -1 -1 22 5 0 9 20 -1 -1 9 0 4 4 -1 20 4 0 5 17 8 3 1 1 3 3 5 2 4 -1 -1 0
 

Sample Output
  
  
From 1 to 3 : Path: 1-->5-->4-->3 Total cost : 21 From 3 to 5 : Path: 3-->4-->5 Total cost : 16 From 2 to 4 : Path: 2-->1-->5-->4 Total cost : 17

除了边有权值,点也有权值,一开始没想到能用什么方法,看了讨论版后,发现用Dijkstra的话,每个节点至多只经过一次,所以用Dijkstra几乎不会变太多


#include <cstdio>
#include <cstring>

using namespace std;

const int INF=0x3f3f3f3f;
const int MAXN=51;//题目没说数据范围,直接看讨论版了,发现点不超过50
int n,m,s,e,w[MAXN][MAXN],b[MAXN],dis[MAXN][MAXN],path[MAXN][MAXN],p1[MAXN],p2[MAXN],tmp,u,n1,n2;
bool vis[MAXN],cal[MAXN];//vis[i]表示当前Dijkstra中i点是否用过,cal[i]表示以i为起点的最短路是否算过

int judge(int cur,int a,int c) {//由于题目要求输出字典序最小的最短路,所以需要判断哪条路径字典序最小
    n1=n2=2;
    p1[0]=cur,p2[0]=cur;
    p1[1]=a,p2[1]=c;
    while(a!=s) {
        p1[n1++]=path[s][a];
        a=path[s][a];
    }
    while(c!=s) {
        p2[n2++]=path[s][c];
        c=path[s][c];
    }
    while(1) {
        /*if(--n1<0)
            return p1[1];   //这是讨论版第一个的写法:p1和p2前面的路径完全一样,且有个下标已小于0,则该路径更短
        if(--n2<0)
            return p2[1];*/
        --n1,--n2;//我认为不用判断,因为不会出现两条路径完全一样的情况(每个点每次Dijkstra至多经过一次)[如果大神有发现反例,希望能告诉我]
        if(p1[n1]<p2[n2])
            return p1[1];
        if(p1[n1]>p2[n2])
            return p2[1];
    }
}

void Dijkstra() {
    int i,j;
    for(i=1;i<=n;++i) {
        tmp=INF;
        for(j=1;j<=n;++j)
            if(!vis[j]&&dis[s][j]<tmp)
                tmp=dis[s][u=j];
        if(tmp==INF)
        return ;
        vis[u]=true;
        for(j=1;j<=n;++j) {
            if(!vis[j]) {
                if((tmp=dis[s][u]+w[u][j]+b[u])<dis[s][j]) {//由于每个点至多经过一次,所以直接将税加载费用中
                    dis[s][j]=tmp;
                    path[s][j]=u;
                }
                else if(tmp==dis[s][j]) {//当费用一样时,选择字典序最小的路径
                    path[s][j]=judge(j,path[s][j],u);
                }
            }
        }
    }
}

void printPath() {
    n1=1,p1[0]=e;
    while(e!=s) {
        p1[n1++]=path[s][e];
        e=path[s][e];
    }
    printf("%d",s);
    --n1;
    while(--n1>=0) {
        printf("-->%d",p1[n1]);
    }
    printf("\n");
}

int main() {
    int i,j;
    while(scanf("%d",&n),n) {
        for(i=1;i<=n;++i)
            for(j=1;j<=n;++j) {
                scanf("%d",&w[i][j]);
                path[i][j]=i;
                if(w[i][j]==-1)
                    w[i][j]=INF;
            }
        for(i=1;i<=n;++i)
            scanf("%d",b+i);

        memset(cal,false,sizeof(cal));
        memset(dis,0x3f,sizeof(dis));
        while(scanf("%d%d",&s,&e),s!=-1&&e!=-1) {
            printf("From %d to %d :\nPath: ",s,e);
            if(!cal[s]) {
                dis[s][s]=0;
                memset(vis,false,sizeof(vis));
                Dijkstra();
                tmp=dis[s][e]-b[s];//由于dis[s][e]的值为s到e的最小路费与s到path[s][e]的税(不包括终点),所以结果只用减去起点的税
            }
            if(s==e)//如果是起点==终点,则cost=0
                tmp=0;
            printPath();
            printf("Total cost : %d\n\n",tmp);//强行不看题,忘了每次输出要有一个空行
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值