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 ...
随机推荐
- MySQL:BlackHole
MySQL:BlackHole 顾名思义BlackHole就是黑洞,只有写入没有输出.现在就来实验一下吧 首先查看一下MySQL支持的存储引擎 mysql> show engines;+---- ...
- POJ2253:Frogger(改造Dijkstra)
Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 64864 Accepted: 20127 题目链接:ht ...
- 获取系统内RAR安装路径
RegistryKey the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVers ...
- C#弱引用
加菲猫 Just have a little faith. C#弱引用 .NET框架提供了另一有趣的特色,被用于实现多样的高速缓存.在.NET中弱引用通过System.WeakReference类实现 ...
- CORS服务端跨域
跨域,通常情况下是说在两个不通过的域名下面无法进行正常的通信,或者说是无法获取其他域名下面的数据,这个主要的原因是,浏览器出于安全问题的考虑,采用了同源策略,通过浏览器对JS的限制,防止恶意用户获取非 ...
- COGS2642 / Bzoj4590 [Shoi2015]自动刷题机
Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 906 Solved: 321 Description 曾经发明了信号增幅仪的发明家SHTSC又公开了 ...
- CentOS 7 主机加固手册-中
CentOS 7 主机加固手册-上 CentOS 7 主机加固手册-中 CentOS 7 主机加固手册-下 0x0c 设置/boot/grub2/grub.cfg权限 Set grub.conf ...
- LeetCode 4 :Majority Element
problem:Given an array of size n, find the majority element. The majority element is the element tha ...
- [Leetcode Week5]Word Ladder II
Word Ladder II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder-ii/description/ Descripti ...
- 24式太极拳:3D动画演示(图文)
http://blog.sina.com.cn/s/blog_4be33b740102e9ae.html 24式太极拳:3D动画演示(图文) (2013-03-10 18:45:55) 转载▼ 标签: ...