hdu 1385(Floyed+打印路径好题)
Minimum Transport Cost
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9814 Accepted Submission(s): 2641
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.
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:
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.
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
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
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
const int N = ;
const int INF = ;
int graph[N][N];
int path[N][N];
int cost[N];
int main()
{
int n;
while(~scanf("%d",&n),n){
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
scanf("%d",&graph[i][j]);
if(graph[i][j]==-) graph[i][j] = INF;
path[i][j]=j;
}
}
for(int i=;i<=n;i++) scanf("%d",&cost[i]);
for(int k=;k<=n;k++){
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(graph[i][j]>graph[i][k]+graph[k][j]+cost[k]){
graph[i][j] = graph[i][k]+graph[k][j]+cost[k];
path[i][j]=path[i][k];
}
else if(graph[i][j]==graph[i][k]+graph[k][j]+cost[k]&&path[i][j] > path[i][k]){
path[i][j]=path[i][k];
}
}
}
}
int a,b;
while(~scanf("%d%d",&a,&b)){
if(a==-&&b==-) break;
printf("From %d to %d :\nPath: %d",a,b,a);
int u =a,v=b;
while(u!=v){
printf("-->%d",path[u][v]);
u = path[u][v];
}
printf("\nTotal cost : %d\n\n", graph[a][b]);
} }
return ;
}
hdu 1385(Floyed+打印路径好题)的更多相关文章
- *HDU 1385 最短路 路径
Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- 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 // ...
- hdu 1385 floyd记录路径
可以用floyd 直接记录相应路径 太棒了! http://blog.csdn.net/ice_crazy/article/details/7785111 #include"stdio.h& ...
- CodeForces 10D. LCIS 最长公共上升子序列模板题 + 打印路径
推荐一篇炒鸡赞的blog. 以下代码中有打印路径. #include <algorithm> #include <iostream> #include <cstring& ...
- hdu 1160 FatMouse's Speed (最长上升子序列+打印路径)
Problem Description FatMouse believes that the fatter a mouse is, the faster it runs. To disprove th ...
- UVA624 CD,01背包+打印路径,好题!
624 - CD 题意:一段n分钟的路程,磁带里有m首歌,每首歌有一个时间,求最多能听多少分钟的歌,并求出是拿几首歌. 思路:如果是求时常,直接用01背包即可,但设计到打印路径这里就用一个二维数组标记 ...
- HDU 1385 Minimum Transport Cost( Floyd + 记录路径 )
链接:传送门 题意:有 n 个城市,从城市 i 到城市 j 需要话费 Aij ,当穿越城市 i 的时候还需要话费额外的 Bi ( 起点终点两个城市不算穿越 ),给出 n × n 大小的城市关系图,-1 ...
- UVA 10054 The Necklace(欧拉回路,打印路径)
题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- AOE网上的关键路径(最长路径 + 打印路径)
题目描述 一个无环的有向图称为无环图(Directed Acyclic Graph),简称DAG图. AOE(Activity On Edge)网:顾名思义,用边表示活动的网,当然它也是DAG ...
随机推荐
- 最短路径(最基础,经典的模板和思想):HDU-2544最短路
题目: 最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- 笔记-算法-KMP算法
笔记-算法-KMP算法 1. KMP算法 KMP算法是一种改进的字符串匹配算法,KMP算法的关键是利用匹配失败后的信息,尽量减少模式串与主串的匹配次数以达到快速匹配的目的.具体实现就是实现一 ...
- [原]sencha touch之panel和tabpanel
最近在弄senchatouch的项目,所以边学习边开发,边记录,直接记录下test code如下: Panel: Ext.application({ name:'itKingApp', launch: ...
- C# Redis存Session Hash存对象
1.新建一个控制台程序,并新建一个类“UserInfo” 2.从github下载redis的windows服务 https://github.com/ServiceStack/redis-window ...
- OpenStack之虚机冷迁移代码简析
OpenStack之虚机冷迁移代码简析 前不久我们看了openstack的热迁移代码,并进行了简单的分析.真的,很简单的分析.现在天气凉了,为了应时令,再简析下虚机冷迁移的代码. 还是老样子,前端的H ...
- nsfwjs鉴黄识别最小化案例
3个月前,也就是2月份左右吧,Github上出现一个开源项目: Infinite Red, Inc.工作室宣布开源旗下基于tensorflow的tfjs的鉴黄小工具 据说是从15000张图片中 进行机 ...
- Android Spinner组件的使用方法
Spinner是什么呢,其实就是我们常见的下拉框,比如: 首先,我们要创建一个Spinner,才能在Spinner中添加我们想要的元素,在xml文件中: <Spinner android:id= ...
- Leetcode 594.最长和谐子序列
最长和谐子序列 和谐数组是指一个数组里元素的最大值和最小值之间的差别正好是1. 现在,给定一个整数数组,你需要在所有可能的子序列中找到最长的和谐子序列的长度. 示例 1: 输入: [1,3,2,2,5 ...
- [已解决]Argument list too long如何处理?
Argument list too long 本质是需要处理的长度超过系统的长度,因此无法执行相关命令. 经过搜索发现了两种方法,思想都是将参数切分成小的段落进行执行. 法一:通过xargs传递参数 ...
- Func<T, TResult> 委托
Func<T, TResult> 委托 Visual Studio 2008 命名空间: System程序集: System.Core(在 System.Core.dll 中) 语 ...