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 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
 
Source
 
题意,给一张n*n的地图,-1表示不联通,正整数为边权,每个点有点权,给定起点s和终点t,s到t的代价是经过的边权和加上路径上除了s和t的点权和,求s到t的最短路并输出路径
思路:floyd,path[i][j]表示i到j路径上的第一个点
/*
ID: LinKArftc
PROG: 1385.cpp
LANG: C++
*/ #include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-8
#define randin srand((unsigned int)time(NULL))
#define input freopen("input.txt","r",stdin)
#define debug(s) cout << "s = " << s << endl;
#define outstars cout << "*************" << endl;
const double PI = acos(-1.0);
const double e = exp(1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
typedef long long ll; const int maxn = ; int mp[maxn][maxn];
int path[maxn][maxn];//path[i][j]记录从i到j的下一个点
int tax[maxn];
int n, s, t; void floyd() {
for (int k = ; k <= n; k ++) {
for (int i = ; i <= n; i ++) {
for (int j = ; j <= n; j ++) {
if (i == k || j == k) continue;
int tmp = mp[i][k] + mp[k][j] + tax[k];
if (mp[i][j] > tmp) {
mp[i][j] = tmp;
path[i][j] = path[i][k];
} else if (mp[i][j] == tmp) {
path[i][j] = min(path[i][j], path[i][k]);
}
}
}
}
} void print_path() {
printf("Path: %d", s);
int cur = s;
while (cur != t) {
cur = path[cur][t];
printf("-->%d", cur);
}
printf("\n");
} int main() {
while (~scanf("%d", &n) && n) {
for (int i = ; i <= n; i ++) {
for (int j = ; j <= n; j ++) {
scanf("%d", &mp[i][j]);
if (mp[i][j] == -) mp[i][j] = inf;//最好用inf替换,用-1的话特判很容易错
else path[i][j] = j;
}
}
for (int i = ; i <= n; i ++) scanf("%d", &tax[i]);
floyd();
while (~scanf("%d %d", &s, &t)) {
if (s == - && t == -) break;
printf("From %d to %d :\n", s, t);
print_path();
printf("Total cost : %d\n\n", mp[s][t]);
}
} return ;
}

HDU1385 (Floyd记录路径)的更多相关文章

  1. ACM/ICPC 之 Floyd+记录路径后继(Hdu1385(ZOJ1456))

    需要处理好字典序最小的路径 HDU1385(ZOJ1456)-Minimum Transport //Hdu1385-ZOJ1456 //给定邻接矩阵,求给定起点到终点的最短路径,若有相同路长的路径按 ...

  2. HDU 1385 Minimum Transport Cost( Floyd + 记录路径 )

    链接:传送门 题意:有 n 个城市,从城市 i 到城市 j 需要话费 Aij ,当穿越城市 i 的时候还需要话费额外的 Bi ( 起点终点两个城市不算穿越 ),给出 n × n 大小的城市关系图,-1 ...

  3. hdu 1385 floyd记录路径

    可以用floyd 直接记录相应路径 太棒了! http://blog.csdn.net/ice_crazy/article/details/7785111 #include"stdio.h& ...

  4. Minimum Transport Cost(floyd+二维数组记录路径)

    Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  5. [Python] 弗洛伊德(Floyd)算法求图的直径并记录路径

    相关概念 对于一个图G=(V, E),求图中两点u, v间最短路径长度,称为图的最短路径问题.最短路径中最长的称为图的直径. 其中,求图中确定的某两点的最短路径算法,称为单源最短路径算法.求图中任意两 ...

  6. hdu 1385 Floyd 输出路径

    Floyd 输出路径 Sample Input50 3 22 -1 43 0 5 -1 -122 5 0 9 20-1 -1 9 0 44 -1 20 4 05 17 8 3 1 //收费1 3 // ...

  7. poj1417 带权并查集 + 背包 + 记录路径

    True Liars Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2713   Accepted: 868 Descrip ...

  8. POJ 3436:ACM Computer Factory(最大流记录路径)

    http://poj.org/problem?id=3436 题意:题意很难懂.给出P N.接下来N行代表N个机器,每一行有2*P+1个数字 第一个数代表容量,第2~P+1个数代表输入,第P+2到2* ...

  9. hdu 1026 Ignatius and the Princess I (bfs+记录路径)(priority_queue)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1026 Problem Description The Princess has been abducted ...

随机推荐

  1. 在Android上Kotlin的单元测试(KAD22)

    作者:Antonio Leiva 时间:Apr 25, 2017 原文链接:https://antonioleiva.com/unit-tests-android-kotlin/ 当然,Kotlin也 ...

  2. JAVA的文件操作【转】

    11.3 I/O类使用 由于在IO操作中,需要使用的数据源有很多,作为一个IO技术的初学者,从读写文件开始学习IO技术是一个比较好的选择.因为文件是一种常见的数据源,而且读写文件也是程序员进行IO编程 ...

  3. Week2 Teamework from Z.XML 软件分析与用户需求调查(三)必应助手体验评测

    评测人:毛宇 肖俊鹏 说明:言辞激烈,请勿介意 我花了2天的时间来试用这个软件<必应缤纷桌面手机助手>,有了很多体会,这里,我来谈一下这款软件在体验部分的表现情况. 体验部分主要分为三个部 ...

  4. java中使用POI+excel 实现数据的批量导入和导出

    java web中使用POI实现excel文件的导入和导出 文件导出 //导入excle表 public String exportXls() throws IOException{ //1.查询所有 ...

  5. C# 利用WMI对象获取物理内存和可用内存大小

    下面的代码演示的是使用WMI对象可获取取物理内存和可用内存大小,在使用WMI对象前,先要添加对System.Management的引用,然后就可以调用WMI对象,代码如下: //获取总物理内存大小 M ...

  6. AJAX基本演示使用

    Servlet配置 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="h ...

  7. 用WebService实现两个整数运算

    最近,项目开发中需要用到Web Service.自己在网上搜集资料.自己做了一个小例子,用来加深自己对Web Service理解. 概念:Web Service主要是为了使原来各孤立的站点之间的信息能 ...

  8. Luogu3960 NOIP2017列队(splay/线段树)

    令splay中的一个点表示一段区间,需要使用其中某个点时将区间分裂即可,剩下的都是splay的基本操作了.写的非常丑陋,注意细节.感觉考场上肯定只能靠部分分苟活了.想起来去年因为各种莫名其妙的原因50 ...

  9. BZOJ4531 && BJOI2014 trace

    #include<cstdio> #include<cctype> using namespace std ; struct state { int len ; int p ; ...

  10. http缓存知多少

    很久没有写博客了,趁现在也快过年,最近项目不是很忙,写一篇博客做为2018年的开始,重拾刚毕业的几年前写博客的冲动.http协议是每个程序猿应该需要知道的东西,不管是前端人员还是后端人员,以前在上家公 ...