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. Qt HUD(平显)演示程序绿色版

    把一个黑底白字的程序改成黑底绿字 上对比图,左侧是原本,右侧是仿造,有些地方比例还是有问题 其实这个程序没有啥技术含量,就是画 #include "mainwindow.h" #i ...

  2. ThinkPHP5项目目录规划实践

    ThinkPHP5安装后(或者下载后的压缩文件解压后)可以看到下面的目录结构: tp5├─application     应用目录 ├─extend          扩展类库目录(可定义) ├─pu ...

  3. SQLAlchemy 学习笔记(一):Engine 与 SQL 表达式语言

    个人笔记,如有错误烦请指正. SQLAlchemy 是一个用 Python 实现的 ORM (Object Relational Mapping)框架,它由多个组件构成,这些组件可以单独使用,也能独立 ...

  4. 使用HashOperations操作redis

    方法 c参数 s说明 Long delete(H key, Object... hashKeys); H key:集合key Object... hashKeys:key对应hashkey  删除ma ...

  5. arm单板上移植gdb

    虚拟机 : vmware 12 image: ubuntukylin 14.04.1 系统版本:Linux dancy 3.13.0-32-generic #57-Ubuntu SMP Tue Jul ...

  6. vs2017切换设计、拆分假死的解决

    今天安装了vs2017版把vs2015卸载了,原因就是vs2015运行webform后ajaxpro总是会出现time out现象.太难受了,所以决定体验一下新版本. 安装了vs2017后ajaxpr ...

  7. Jlink 软件断点和硬件断点

    调试2440 RAM拷贝至SDRAM遇到的问题 汇编代码主要是初始化一些寄存器,关狗,初始化时钟,初始化存储管理器以便访问内存,然后将SoC上4k RAM数据拷贝至SDRAM,然后在SRAM里面运行, ...

  8. sometimes-ever js中创建数组,并往数组里添加元素

    数组的创建 var arrayObj = new Array(); //创建一个数组 var arrayObj = new Array([size]); //创建一个数组并指定长度,注意不是上限,是长 ...

  9. 匿名内存 | shm

    page_is_file_cache是和swapbacked互斥的,所以说对于匿名页来说,分配的时候就就会把PageSwapBacked给设置上,page->mapping_address = ...

  10. JavaScript获取访问设备信息

    <html xmlns=http://www.w3.org/1999/xhtml> <head> <title>JavaScript获取访问设备信息</tit ...