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. Andrew Ng机器学习公开课笔记 -- Generalized Linear Models

    网易公开课,第4课 notes,http://cs229.stanford.edu/notes/cs229-notes1.pdf 前面介绍一个线性回归问题,符合高斯分布 一个分类问题,logstic回 ...

  2. jQuery中通过$.browser来判断浏览器

    一.使用方法 语法:$.browser.["浏览器关键字"] $(function() { if($.browser.msie) { alert("this is IE& ...

  3. UEM用户行为了如指掌!

    “千呼万唤始出来”,万众期待的UEM正式与宝宝们见面啦~~~ 今天很多人来问小编,Web咋不见了,表急,Web并没有消失,而是重磅升级为UEM啦!!! 什么是UEM呢?UEM全称User Experi ...

  4. How Instagram Feeds Work: Celery and RabbitMQ(转)

    原文:http://blogs.vmware.com/vfabric/2013/04/how-instagram-feeds-work-celery-and-rabbitmq.html Instagr ...

  5. 002-and design-dva.js 知识导图-01JavaScript 语言,React Component

    一.概述 参看:https://github.com/dvajs/dva-knowledgemap react 或 dva 时会不会有这样的疑惑: es6 特性那么多,我需要全部学会吗? react ...

  6. DIY自己的GIS程序(1)——起航

    一个GIS系统最重要的功能是绘制图形和关联属性,这里研究二维点线面的绘制过程: 对于一个绘图系统设计,图形的绘制涉及两个重要的方面. 1.一个是绘制或者重新绘制,重绘过程出现在下面情况: a 界面初始 ...

  7. c primer plus(五版)编程练习-第八章编程练习

    1.设计一个程序,统计从输入到文件结尾为止的字符数. #include<stdio.h> int main(void){ int ch; int i; i=; while((ch = ge ...

  8. 单机部署PXC

    在一台机器上安装PXC Linux:centos 7     IP:192.168.30.221 PXC版本:Percona-XtraDB-Cluster-5.7.17-rel13-29.20.3.L ...

  9. Jquery 简明介绍

    http://www.cnblogs.com/luotianshuai/p/5196997.html http://www.cnblogs.com/liujianzuo888/articles/568 ...

  10. 画柱状图Java

    样例输入:THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG.THIS IS AN EXAMPLE TO TEST FOR YOURHISTOGRAM PROGR ...