HDU-1385 Minimum Transport Cost

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
  ————————————————————集训12.5的分割线————————————————————
思路:要求很多的起点和终点之间的路径,只有Floyd才是正解了。
首先是顶点上的权的问题。假设点A不是起点,现在从A走到任意一点,都需要支付经过A点的费用,也就是说,A点出去的边的权值加上了A点的权。这样终点的权必定不会加进来。那么起点呢?
假设A -> B初始值是边ab=5,增加点K,更新G[A][B]为:G[A][K]+G[K][B]+p[K]
你有没有发现,在松弛操作的时候,仅仅只是加上了经过K的税收?起点问题自然而然解决了。这是因为G数组保存的是从起点到终点,加上沿途所有边和“经过的城市的税收”的最短路。
还剩下路径输出按照字典序的问题。
之前一直是按照倒走迷宫的方法再开一个数组解决这个问题,现在发现真是傻,为什么要倒走迷宫?回想Dijkstra,那个时候是因为先从n个顶点之中找到最近的点cur,然后处理因它造成的松弛,我不知道它的下一个结点会是谁,但是我总能知道被松弛的点是从cur走过来的,因此我必须要倒走迷宫。
现在Floyd是一个递推的过程,我知道每个被枚举的起点和终点。顺着来(每步记录儿子)就行了。
这样一来,一旦因为我枚举的点产生同样权值的路,就根据字典序改变儿子。
代码如下:
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <climits>
#include <iostream>
#define INF 0x3f3f3f3f
using namespace std;
/****************************************/
const int N = 100;//居然不给数据范围,耍流氓!
int n, m;
int G[N][N], price[N], nxt[N][N];

void Floyd()
{
	for(int k = 0; k < n; k++) {
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) if(k!=i&&k!=j) {
				if(G[i][j] > G[i][k] + G[k][j] + price[k]) {
					G[i][j] = G[i][k] + G[k][j] + price[k];
					nxt[i][j] = nxt[i][k];
				}
				else if(G[i][j] == G[i][k] + G[k][j] + price[k]) {
					if(nxt[i][j] > nxt[i][k]) nxt[i][j] = nxt[i][k];
				}
			}
		}
	}
}

int main()
{
#ifdef J_Sure
	freopen("000.in", "r", stdin);
//  freopen(".out", "w", stdout);
#endif
	while(scanf("%d", &n)!=EOF&&n) {
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) {
				scanf("%d", &G[i][j]);
				if(G[i][j] == -1) G[i][j] = INF;
				else nxt[i][j] = j;
			}
		}
		for(int i = 0; i < n; i++) 
			scanf("%d", &price[i]);
		int st, ed;
		Floyd();
		while(scanf("%d%d", &st, &ed)) {
			if(st == -1 && ed == -1) break;
			st--; ed--;
			printf("From %d to %d :\nPath: %d", st+1, ed+1, st+1);
			int x = st;
			while(x != ed) {
				printf("-->%d", nxt[x][ed]+1);
				x = nxt[x][ed];
			}
			puts("");
			printf("Total cost : %d\n\n", G[st][ed]);
		}
	}
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值