hdu 1385 floyd字典序
Minimum Transport Cost
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11017 Accepted Submission(s):
3058
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.
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:
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<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ql(a,b) memset(a,b,sizeof(a))
int e[105][105],b[105];
int path[105][105];
void floyd(int n)
{
int i,j,k;
for(k=1;k<=n;++k)
for(i=1;i<=n;++i)
for(j=1;j<=n;++j){
if(e[i][j]>b[k]+e[i][k]+e[k][j]){
e[i][j]=b[k]+e[i][k]+e[k][j];
path[i][j]=path[i][k];
}
else if(e[i][j]==b[k]+e[i][k]+e[k][j]&&path[i][j]>path[i][k]){
path[i][j]=path[i][k];
}
}
}
void print(int u,int v)
{
int k;
if(u==v)
{
printf("%d",v);
return ;
}
k=path[u][v];
printf("%d-->",u);
print(k,v);
}
int main()
{
int n,m,i,j,k,a,c,t=1;
while(cin>>n&&n){
for(i=1;i<=n;++i)
for(j=1;j<=n;++j){
scanf("%d",&e[i][j]);
if(e[i][j]==-1) e[i][j]=inf;
path[i][j]=j;
}
for(i=1;i<=n;++i) cin>>b[i];
floyd(n);
while(cin>>a>>c&&(a+1||c+1)){
printf("From %d to %d :\nPath: ",a,c);
print(a,c);
printf("\nTotal cost : %d\n\n",e[a][c]);
}
}
return 0;
}
hdu 1385 floyd字典序的更多相关文章
- 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& ...
- hdu 5008 查找字典序第k小的子串
Boring String Problem Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Ot ...
- hdu 1385 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 ...
- HDU 1385 Minimum Transport Cost( Floyd + 记录路径 )
链接:传送门 题意:有 n 个城市,从城市 i 到城市 j 需要话费 Aij ,当穿越城市 i 的时候还需要话费额外的 Bi ( 起点终点两个城市不算穿越 ),给出 n × n 大小的城市关系图,-1 ...
- HDU 1385 Minimum Transport Cost (输出字典序最小路径)【最短路】
<题目链接> 题目大意:给你一张图,有n个点,每个点都有需要缴的税,两个直接相连点之间的道路也有需要花费的费用.现在进行多次询问,给定起点和终点,输出给定起点和终点之间最少花费是多少,并且 ...
- hdu 1385 Minimum Transport Cost (Floyd)
Minimum Transport CostTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- HDU 1385 Minimum Transport Cost (Dijstra 最短路)
Minimum Transport Cost http://acm.hdu.edu.cn/showproblem.php?pid=1385 Problem Description These are ...
随机推荐
- HDU 1103 Flo's Restaurant(模拟+优先队列)
Flo's Restaurant Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- win10配置的静态/动态IP和 DNS的方法
1.配置静态IP和DNS netsh interface ip set address name="以太网" source=static addr=192.168.9.145 ma ...
- yum localinstall 安装mysql8.0
MySQL :: MySQL 8.0 Reference Manual :: 2.5.1 Installing MySQL on Linux Using the MySQL Yum Repositor ...
- oracle(十一) scn
SCN(System Chang Number)作为oracle中的一个重要机制,在数据恢复.Data Guard.Streams复制.RAC节点间的同步等各个功能中起着重要作用. 理解SCN的运作机 ...
- Logback配置讲解
复制文件并粘贴到项目下: logback.xml: <?xml version="1.0" encoding="UTF-8"?> <confi ...
- linux安装Navicat,界面出现乱码解决方法
下载Navicat:navicat112_mariadb_cs_x64.tar.gz 点击" ./start_navicat"安装出现界面便面为乱码 解决办法:打开start_na ...
- Python实现支付宝在线支付
windows系统安装Python虚拟环境 首先保证你的系统已经安装好了Python 安装virtualenv C:\WINDOWS\system32>pip3 install virtuale ...
- PHP实现返回JSON和XML的类分享
PHP实现返回JSON和XML的类分享 <?php class Reponse{ //private $result = array('code'=null,'messa ...
- Winform开发之ComboBox和ComboBoxEdit控件绑定key/value数据
使用 ComboBox 控件绑定key/value值: 因为 ComboBox 是有 DataSource 属性的,所以它可以直接绑定数据源,如 DataTable.ListItem 等. 使用 Da ...
- hdu 5185 动态规划 分析降低复杂度
这题说的是 x[1]+x[2]+x[3]+…+x[n]=n, 这里 0 <= x[i] <= n && 1 <= i <= n x[i] <= x[i+1 ...