hdu1384

Minimum Transport Cost

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 7011    Accepted Submission(s): 1793

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
#include"stdio.h"
#include"string.h"
#include"iostream"
#define M 111
#define inf 99999999
#define eps 1e-9
#include"math.h"
using namespace std;
int G[M][M],dis[M][M],path[M][M],n,b[M];
void floyd()
{
int i,j,k;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
dis[i][j]=G[i][j];
path[i][j]=j;
}
}
for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(dis[i][j]>dis[i][k]+dis[k][j]+b[k])
{
dis[i][j]=dis[i][k]+dis[k][j]+b[k];
path[i][j]=path[i][k];
}
else if(dis[i][j]==dis[i][k]+dis[k][j]+b[k])
{
if(path[i][j]>path[i][k]&&i!=k)
path[i][j]=path[i][k];
}
}
}
}
}
void solve(int i,int j)
{
printf("From %d to %d :\n",i,j);
printf("Path: ");
int k=i;
printf("%d",i);
while(k!=j)
{
printf("-->%d",path[k][j]);
k=path[k][j];
}
printf("\n");
printf("Total cost : %d\n\n",dis[i][j]);
}
int main()
{
int i,j;
while(scanf("%d",&n),n)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
G[i][j]=inf;
}
G[i][i]=0;
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
int a;
scanf("%d",&a);
if(a!=-1)
G[i][j]=a;
}
}
for(i=1;i<=n;i++)
scanf("%d",&b[i]);
floyd();
int s,e;
while(scanf("%d%d",&s,&e),s!=-1||e!=-1)
{
solve(s,e);
}
}
return 0;
}

3 52 4-1 -10

 
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

程序:

Floyd求字典序最小的路径的更多相关文章

  1. HDU 5915 The Fastest Runner Ms. Zhang (CCPC2016 长春 E题,分类讨论 + 求字典序最小的直径 + 数据结构寻找最小值)

    题目链接  CCPC2016 Changchun Problem E 题意  给定一个$n$个点$n$条边的无向图,现在从某一点$s$出发,每个点都经过一遍,最后在$t$点停止,经过的边数为$l$   ...

  2. POJ 2337 Catenyms (有向图欧拉路径,求字典序最小的解)

    Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8756   Accepted: 2306 Descript ...

  3. [poj2337]求字典序最小欧拉回路

    注意:找出一条欧拉回路,与判定这个图能不能一笔联通...是不同的概念 c++奇怪的编译规则...生不如死啊... string怎么用啊...cincout来救? 可以直接.length()我也是长见识 ...

  4. hdu1599 find the mincost route floyd求出最小权值的环

    find the mincost route Time Limit: 1000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  5. hdu 1814 Peaceful Commission (2-sat 输出字典序最小的路径)

    Peaceful Commission Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  6. HDU 2296 Ring -----------AC自动机,其实我想说的是怎么快速打印字典序最小的路径

    大冥神的代码,以后能贴的机会估计就更少了....所以本着有就贴的好习惯,= =....直接贴 #include <bits/stdc++.h> using LL = long long ; ...

  7. 图论--2-SAT--暴力染色法求字典序最小模版

    #include <cstdio> #include <cstring> #include <stack> #include <queue> #incl ...

  8. hdu1385 Minimum Transport Cost 字典序最小的最短路径 Floyd

    求最短路的算法最有名的是Dijkstra.所以一般拿到题目第一反应就是使用Dijkstra算法.但是此题要求的好几对起点和终点的最短路径.所以用Floyd是最好的选择.因为其他三种最短路的算法都是单源 ...

  9. ZOJ-1456 Minimum Transport Cost---Floyd变形+路径输出字典序最小

    题目链接: https://vjudge.net/problem/ZOJ-1456 题目大意: Spring国家有N个城市,每队城市之间也许有运输路线,也可能没有.现在有一些货物要从一个城市运到另一个 ...

随机推荐

  1. javascript时钟代码 DEMO-002

    转载自:http://www.cnblogs.com/Mygirl/archive/2012/03/30/2425832.html 正常时间显示 复制代码 <SCRIPT language=ja ...

  2. struts2中struts.xml和web.xml文件解析及工作原理

    web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp ...

  3. java_Observer Design Pattern

    摘自: http://www.ntu.edu.sg/home/ehchua/programming/java/J4a_GUI.html Creating Your Own Event, Source ...

  4. Qt Creater中Clang-format的使用

    起因在于习惯性的想格式化代码,发现Qt Creater默认居然是没有代码格式化的,只有一个缩进,搞毛线啊!!! 搜索了下,倒是很容易就搜到了,Qt Creater中有个插件:beautifier,在 ...

  5. 第三百零一节,python操作redis缓存-管道、发布订阅

    python操作redis缓存-管道.发布订阅 一.管道 redis-py默认在执行每次请求都会创建(连接池申请连接)和断开(归还连接池)一次连接操作,如果想要在一次请求中指定多个命令,则可以使用pi ...

  6. 【从0開始Tornado建站】整体设计

    Tornado是一个非堵塞的webserver,也是python的web框架中很优秀的一款.网上关于django的tutorial许多并且具体,关于tornado的使用就很少了.我想以我从0開始的方式 ...

  7. par函数cex参数-控制文字和点的大小

    cex参数用来控制图片中点和文字的大小,对于一副图片来说,有很多的文字部分,包括x轴标签(xlab), y轴标签(ylab), x轴刻度上的文字, y轴刻度上的文字,主标题(main), 副标题(su ...

  8. MVC4 Controller 与 WebApi 的 Session 传值问

    在MVC以后,Session方式可能已经不太常用,但偶尔还是会用到,比如页面验证码之类的.例如登录页面使用的验证码通过Controller提供一个View来实现,可以使用Session来存储这个值.但 ...

  9. unity3d多个版本共存

    用4.3打开两个低版本的unity工程,都报错.... 用低版本打开正常,希望Unity3D版本兼容性越来越好吧. 参考:http://blog.csdn.net/anyuanlzh/article/ ...

  10. 管理开机启动:systemd

    一.CentOS7 systemd 介绍 在 CentOS7 中,使用 systemd 来管理其他服务是否开机启动,systemctl 是 systemd 服务的命令行工具 [root@localho ...