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): 10029 Accepted Submission(s): 2716
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.
题目大意:
有N个城市,然后直接给出这些城市之间的邻接矩阵,矩阵中-1代表那两个城市无道路相连,其他值代表路径长度。
如果一辆汽车经过某个城市,必须要交一定的钱(可能是过路费)。
现在要从a城到b城,花费为路径长度之和,再加上除起点与终点外所有城市的过路费之和。
求最小花费,如果有多条路经符合,则输出字典序最小的路径。
感觉这一题就是为Floyd算法而生的,用Floyd很简单。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int N=;
int n;
int w[N][N];
int path[N][N];
int charge[N];
void init()
{
for(int i=; i<=n; ++i)
{
for(int j=; j<=n; ++j)
{
if(i!=j)w[i][j]=inf;
else w[i][j]=;
path[i][j] = j; // path[i][j]表示点i到j经过的第一个点`
}
}
}
void Floyd()
{
for(int k=; k<=n; ++k)
for(int i=; i<=n; ++i)
for(int j=; j<=n; ++j)
if(w[i][k]!=inf && w[k][j]!=inf)
{
int tmp = w[i][k]+w[k][j]+charge[k];
if(w[i][j] > tmp)
{
w[i][j] = tmp;
path[i][j] = path[i][k];
}
else if(w[i][j] == tmp && path[i][j]>path[i][k])//选择字典序小的
{
path[i][j] = path[i][k];
}
}
}
int main()
{ int i,j,src,des;
while(scanf("%d",&n),n)
{
init();
for(i=; i<=n; ++i)
{
for(j=; j<=n; ++j)
{
scanf("%d",&w[i][j]);
if(w[i][j]==-) w[i][j]=inf;
}
}
for(i=; i<=n; ++i)
scanf("%d",&charge[i]);
Floyd();
while(scanf("%d%d",&src,&des))
{
if(src==-&&des==-) break;
printf("From %d to %d :\n",src,des);
printf("Path: ");
int u = src;
printf("%d",u);
while(u != des)
{
printf("-->%d",path[u][des]);
u = path[u][des];
}
puts("");
printf("Total cost : %d\n\n", w[src][des]);
}
}
return ;
}
HDU1385 Minimum Transport Cost (Floyd)的更多相关文章
- hdu 1385 Minimum Transport Cost(floyd && 记录路径)
Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- ZOJ 1456 Minimum Transport Cost(Floyd算法求解最短路径并输出最小字典序路径)
题目链接: https://vjudge.net/problem/ZOJ-1456 These are N cities in Spring country. Between each pair of ...
- HDU 1385 Minimum Transport Cost( Floyd + 记录路径 )
链接:传送门 题意:有 n 个城市,从城市 i 到城市 j 需要话费 Aij ,当穿越城市 i 的时候还需要话费额外的 Bi ( 起点终点两个城市不算穿越 ),给出 n × n 大小的城市关系图,-1 ...
- hdu 1385 Minimum Transport Cost (floyd算法)
貌似···················· 这个算法深的东西还是很不熟悉!继续学习!!!! ++++++++++++++++++++++++++++ ======================== ...
- ZOJ 1456 Minimum Transport Cost(floyd+后继路径记录)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1456 题意:求最短路并且输出字典序最小的答案. 思路:如果用dijkstr ...
- HD1385Minimum Transport Cost(Floyd + 输出路径)
Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- HDU 1385 Minimum Transport Cost (最短路,并输出路径)
题意:给你n个城市,一些城市之间会有一些道路,有边权.并且每个城市都会有一些费用. 然后你一些起点和终点,问你从起点到终点最少需要多少路途. 除了起点和终点,最短路的图中的每个城市的费用都要加上. 思 ...
- hdu1385 Minimum Transport Cost 字典序最小的最短路径 Floyd
求最短路的算法最有名的是Dijkstra.所以一般拿到题目第一反应就是使用Dijkstra算法.但是此题要求的好几对起点和终点的最短路径.所以用Floyd是最好的选择.因为其他三种最短路的算法都是单源 ...
- ZOJ1655 Transport Goods(Floyd)
利用Floyd的DP状态转移方程. #include<cstdio> #include<cstring> #include<queue> #include<a ...
随机推荐
- angular的一些问题
引入第三方类库 1.安装依赖 npm install jquey --save 2.引入项目 在angular-cli.json "scripts": [ "../nod ...
- oracle的rownum使用
对于rownum来说它是Oracle系统顺序分配为从查询返回的行的编号,返回的第一行分配的是1,第二行是2,依此类推,这个伪字段可以用于限制查询返回的总行数,且rownum不能以任何表的名称作为前缀. ...
- (转)用python获取页面返回的cookie
网址如下: crifan:http://www.crifan.com/get_cookie_from_web_response_in_python/ . . . .
- Java并发(10)- 简单聊聊JDK中的七大阻塞队列
引言 JDK中除了上文提到的各种并发容器,还提供了丰富的阻塞队列.阻塞队列统一实现了BlockingQueue接口,BlockingQueue接口在java.util包Queue接口的基础上提供了pu ...
- 转:安装成功的nginx如何添加未编译安装模块
原已经安装好的nginx,现在需要添加一个未被编译安装的模块 举例说明:安装第三方的ngx_cache_purge模块(用于清除指定URL的缓存) nginx的模块是需要重新编译nginx,而不是像a ...
- WEB-INF 有关的目录路径问题总结
1.资源文件只能放在WebContent下面,如 CSS,JS,image等.放在WEB-INF下引用不了. 2.页面放在WEB-INF目录下面,这样可以限制访问,提高安全性.如JSP,html 3. ...
- [洛谷P2730] 魔板 Magic Squares
洛谷题目链接:魔板 题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 我们知道魔板的每一个方格都 ...
- CentOS7 Tomcat 启动过程很慢,JVM上的随机数与熵池策略
1. CentOS7 Tomcat 启动过程很慢 在centos启动官方的tomcat时,启动过程很慢,需要几分钟,经过查看日志,发现耗时在这里:是session引起的随机数问题导致的: <co ...
- 汕头市队赛 SRM 07 B 好玩的麻将
B 好玩的麻将 SRM 07 背景&&描述 天才麻将少女KPM立志要在日麻界闯出一番名堂. KPM上周又打了n场麻将,又控了分使得自己的排名是1..n的一个排列. 但她 ...
- jqueryDateTable.js排序
{% block js %} <script type="text/javascript"> $('#datatable').dataTable( { "or ...