最短路 + 记录路径 之 zoj 1456 Minimum Transport Cost (hdu 1385)

/*
考虑到测试数据中需要求解任意两点间的最短路,所以采用Floyd-Warshall算法

dp[i][j] = min(dp[i][k] + dp[k][j] + tax[k], dp[i][j]);

关键在于记录路径,并且要保证:if there are more minimal paths, output the lexically smallest one.
分两种情况讨论:
	(1)dp[i][j] > dp[i][k] + dp[k][j] + tax[k]
		直接更新dp[i][j],保证获得最短路
	(2)dp[i][j] == dp[i][k] + dp[k][j] + tax[k]
		看i节点的直接后继节点编号哪个更小,哪个小选哪个,保证获得 the lexically smallest one。

为记录路径:
	path[i][j] := 在节点i到节点j的最短路径上,i的直接后继节点编号。
	初始值:path[i][j] = j
	用这种办法可以记录路径的原因:
		有n个节点,从1->n的最短路径,假设是这样的1->2->3->4->...->n,则2->n的最短路径
		必然是2->3->4->...->n,3->n的最短路径必然是3->4->...->n,如果不是这样的,那么
		当前1->n的路径必然不是1->n的最短路径,与假设矛盾。
	通过循环即可获得1->n的完整最短路径。
*/
  1 #include <iostream>
  2 #include <cstdlib>
  3 #include <cstdio>
  4 #include <cstddef>
  5 #include <iterator>
  6 #include <algorithm>
  7 #include <string>
  8 #include <locale>
  9 #include <cmath>
 10 #include <vector>
 11 #include <cstring>
 12 #include <map>
 13 #include <utility>
 14 #include <queue>
 15 #include <stack>
 16 #include <set>
 17 using namespace std;
 18 const int INF = 0x3f3f3f3f;
 19 const int MaxN = 205;
 20 const int modPrime = 3046721;
 21
 22 int N;
 23 int dp[MaxN][MaxN];
 24 int tax[MaxN];
 25 int path[MaxN][MaxN];
 26
 27
 28 void Solve()
 29 {
 30     for (int i = 0; i < N; ++i)
 31     {
 32         for (int j = 0; j < N; ++j)
 33         {
 34             path[i][j] = j;
 35         }
 36     }
 37     for (int k = 0; k < N; ++k)
 38     {
 39         for (int i = 0; i < N; ++i)
 40         {
 41             for (int j = 0; j < N; ++j)
 42             {
 43                 //dp[i][j] = min(dp[i][k] + dp[k][j] + tax[k], dp[i][j]);
 44                 if (dp[i][j] > dp[i][k] + dp[k][j] + tax[k])
 45                 {
 46                     dp[i][j] = dp[i][k] + dp[k][j] + tax[k];
 47                     path[i][j] = path[i][k];
 48                 }
 49                 else
 50                 {
 51                     if (dp[i][j] == dp[i][k] + dp[k][j] + tax[k])
 52                     {
 53                         if (path[i][j] > path[i][k])
 54                         {
 55                             path[i][j] = path[i][k];
 56                         }
 57                     }
 58                 }
 59             }
 60         }
 61     }
 62
 63     int src, dsn;
 64     while ((~scanf("%d %d", &src, &dsn)) && !(src == -1 && dsn == -1))
 65     {
 66         printf("From %d to %d :\n", src, dsn);
 67         printf("Path: ");
 68         int tmp = src - 1;
 69         printf("%d", src);
 70         while (tmp != dsn - 1)
 71         {
 72             printf("-->");
 73             printf("%d", path[tmp][dsn - 1] + 1);
 74             tmp = path[tmp][dsn - 1];
 75         }
 76
 77
 78         printf("\n");
 79         printf("Total cost : %d\n\n", dp[src - 1][dsn - 1]);
 80     }
 81 }
 82
 83 int main()
 84 {
 85 #ifdef HOME
 86     freopen("in", "r", stdin);
 87     //freopen("out", "w", stdout);
 88 #endif
 89
 90     while (~scanf("%d", &N) && N)
 91     {
 92         for (int i = 0; i < N; ++i)
 93         {
 94             for (int j = 0; j < N; ++j)
 95             {
 96                 scanf("%d", &dp[i][j]);
 97                 if (dp[i][j] == -1)
 98                 {
 99                     dp[i][j] = INF;
100                 }
101             }
102         }
103         for (int i = 0; i < N; ++i)
104         {
105             scanf("%d", &tax[i]);
106         }
107         Solve();
108     }
109
110
111 #ifdef HOME
112     cerr << "Time elapsed: " << clock() / CLOCKS_PER_SEC << " ms" << endl;
113     _CrtDumpMemoryLeaks();
114 #endif
115     return 0;
116 }

 
时间: 2024-11-13 09:26:35

最短路 + 记录路径 之 zoj 1456 Minimum Transport Cost (hdu 1385)的相关文章

zoj 1456 Minimum Transport Cost (Floyd+路径记录)

Minimum Transport Cost Time Limit: 2 Seconds      Memory Limit: 65536 KB 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 t

HDOJ 5294 Tricks Device 最短路(记录路径)+最小割

最短路记录路径,同时求出最短的路径上最少要有多少条边, 然后用在最短路上的边重新构图后求最小割. Tricks Device Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1584    Accepted Submission(s): 388 Problem Description Innocent Wu follows Dumb Z

HDU1385 Minimum Transport Cost 【Floyd】+【路径记录】

Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7496    Accepted Submission(s): 1918 Problem Description These are N cities in Spring country. Between each pair of cities

HDU 1385 Minimum Transport Cost(Floyd 最短路 打印路径)

HDU 1385 大意: 有N个城市,然后直接给出这些城市之间的邻接矩阵,矩阵中-1代表那两个城市无道路相连,其他值代表路径长度. 如果一辆汽车经过某个城市,必须要交一定的钱(可能是过路费). 现在要从a城到b城,花费为路径长度之和,再加上除起点与终点外所有城市的过路费之和. 求最小花费,如果有多条路经符合,则输出字典序最小的路径. 思路: Floyd求最短路,打印路径即可. 1 /*--------------------------------------------------------

uva 11374 最短路+记录路径 dijkstra最短路模板

UVA - 11374 Airport Express Time Limit:1000MS   Memory Limit:Unknown   64bit IO Format:%lld & %llu [Submit]  [Go Back]  [Status] Description ProblemD: Airport Express In a small city called Iokh, a train service, Airport-Express, takes residents to t

hdu 1385 Minimum Transport Cost (Floyd + 字典序打印路径)

Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7822    Accepted Submission(s): 2028 Problem Description These are N cities in Spring country. Between each pair of cities

HDU 1385 Minimum Transport Cost (字典序打印最短路)

题意  给你一个无向图的邻接矩阵  和途径每个点需要的额外花费首尾没有额外花费  求图中某两点之间的最短路并打印字典序最小路径 要求多组点之间的就用floyd咯  打印路径也比较方便  nex[i][j]表示从i点到j点最短路的第一个途经点  那么如果路径中加入一个节点k后 nex[i][j]应该更新为nex[i][k]  因为要途径k了 #include<cstdio> #include<cstring> using namespace std; const int N = 10

hdu Minimum Transport Cost(按字典序输出路径)

 摘自:Think In Java 从技术角度说,OOP(面向对象程序设计)只是涉及抽象的数据类型.继承以及多形性,但另一些问题也可能显得非常重要.本节将就这些问题进行探讨.最重要的问题之一是对象的创建及破坏方式.对象需要的数据位于哪儿,如何控制对象的"存在时间"呢?针对这个问题,解决的方案是各异其趣的.C++认为程序的执行效率是最重要的一个问题,所以它允许程序员作出选择.为获得最快的运行速度,存储以及存在时间可在编写程序时决定,只需将对象放置在堆栈(有时也叫作自动或定域变量)或者静态

HDU1385 Minimum Transport Cost

Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9052    Accepted Submission(s): 2383 Problem Description These are N cities in Spring country. Between each pair of cities