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

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
 disguss里给的某些数据根本就是错的吧。。我的错误一开始是变量写错结果对着他们的test改半天ccc。一直死循环
floyd不必多数,字典序得话,肯定有最前面的字母决定总体的大小,所以path[i][j]表示i-->j中间经过的第一个点
还有就是单向图这个我倒是没写错。

#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字典序的更多相关文章

  1. 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 // ...

  2. hdu 1385 floyd记录路径

    可以用floyd 直接记录相应路径 太棒了! http://blog.csdn.net/ice_crazy/article/details/7785111 #include"stdio.h& ...

  3. hdu 5008 查找字典序第k小的子串

    Boring String Problem Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot ...

  4. hdu 1385 Minimum Transport Cost (floyd算法)

    貌似···················· 这个算法深的东西还是很不熟悉!继续学习!!!! ++++++++++++++++++++++++++++ ======================== ...

  5. hdu 1385 Minimum Transport Cost(floyd &amp;&amp; 记录路径)

    Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  6. HDU 1385 Minimum Transport Cost( Floyd + 记录路径 )

    链接:传送门 题意:有 n 个城市,从城市 i 到城市 j 需要话费 Aij ,当穿越城市 i 的时候还需要话费额外的 Bi ( 起点终点两个城市不算穿越 ),给出 n × n 大小的城市关系图,-1 ...

  7. HDU 1385 Minimum Transport Cost (输出字典序最小路径)【最短路】

    <题目链接> 题目大意:给你一张图,有n个点,每个点都有需要缴的税,两个直接相连点之间的道路也有需要花费的费用.现在进行多次询问,给定起点和终点,输出给定起点和终点之间最少花费是多少,并且 ...

  8. hdu 1385 Minimum Transport Cost (Floyd)

    Minimum Transport CostTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...

  9. HDU 1385 Minimum Transport Cost (Dijstra 最短路)

    Minimum Transport Cost http://acm.hdu.edu.cn/showproblem.php?pid=1385 Problem Description These are ...

随机推荐

  1. SQL Fundamentals:替代变量(&,&&)以及DEFINE,UNDEFINE,ACCEPT指令

    替代变量 利用替代变量可以实现数据操作的交互性.替代变量的操作类似于键盘输入操作. 所谓的替代变量,指的就是在进行查询或更新操作时,某些数据是由用户所输入的,而这些数据前可以使用“&”标记. ...

  2. pandas的Categorical方法

    对于数据样本的标签,如果我们事先不知道这个样本有多少类别,那么可以对数据集的类别列进行统计,这时我们用pandas的Categorical方法就非常快的实现. 1.说明: 你的数据最好是一个serie ...

  3. 各大互联网企业Java面试题汇总,看我如何成功拿到百度的offer

    前言 本人Java开发,5年经验,7月初来到帝都,开启面试经历,前后20天左右,主面互联网公司,一二线大公司或者是融资中的创业公司都面试过,拿了一些offer,其中包括奇虎360,最后综合决定还是去百 ...

  4. mongdb ---shard

    http://blog.fens.me/mongodb-shard/ https://segmentfault.com/a/1190000004263332 1. 和用户管理相关的操作基本都要在adm ...

  5. tow sum

    今天面试好打脸!!! 解决方案方法一:暴力法暴力法很简单.遍历每个元素 xx,并查找是否存在一个值与 target−x 相等的目标元素. public int[] twoSum(int[] nums, ...

  6. PostgreSQL数据库的安装与PostGIS的安装(转)

    原文:http://lovewinner.iteye.com/blog/1490915 安装postgresql sudo apt-get install postgresql-9.1 postgre ...

  7. Day22 文件上传下载和javaMail

    day22总结 文件上传概述   1 文件上传的作用 例如网络硬盘!就是用来上传下载文件的. 在智联招聘上填写一个完整的简历还需要上传照片呢.   2 文件上传对页面的要求 上传文件的要求比较多,需要 ...

  8. 离线安装部署zabbix

    一. 安装好CentOS安装过程中添加php,mariadb等所需要的依赖 二. 准备好所有所需的rpm压缩文件包在centos中解压,这里放在根目录下zabbix_rpms文件夹下 三. 安装所需r ...

  9. POJ2891:Strange Way to Express Integers(解一元线性同余方程组)

    写一下自己的理解,下面附上转载的:若a==b(modk);//这里的==指的是同余,我用=表示相等(a%k=b)a-b=kt(t为整数)以前理解的错误思想:以前认为上面的形式+(a-tb=k)也是成立 ...

  10. 使用Github发布自己的网站

    1.编写好自己的index.html 2.在github上新建一个分支,分支名需要按xxx.github.com(xxx为github账号名): 3.进入分支的setting界面,自动生成网页,会在分 ...