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. 关于Cocos2d-x中增加暂停按钮的步骤

    1.在GameScene.cpp的init方法中先定义一个里面放着可变换并在变换的时候会响应事件的MenuItem的Menu,这个Menu里面的可变换MenuItem又由两个小MenuItem组成,每 ...

  2. e581. Animating an Array of Images in an Application

    This is the simplest application to animate an array of images. import java.awt.*; import javax.swin ...

  3. pyqt声音输入

    参考文档: http://blog.csdn.net/jdh99/article/details/39525451 http://blog.csdn.net/jdh99/article/details ...

  4. 【Java面试题】32 ArrayList和Vector的区别

    1. Vector & ArrayList  相同点: 1.ArrayList和Vector都是继承了相同的父类和实现了相同的接口 2.底层都是数组实现的 3.初始默认长度都为10. 不同点: ...

  5. Spring Cloud在国内中小型公司用的起来吗?

    转自:http://www.cnblogs.com/ityouknow/p/7508306.html 今天吃完饭休息的时候瞎逛知乎,突然看到这个一个问题Spring Cloud在国内中小型公司能用起来 ...

  6. date详解

    在linux环境中,不管是编程还是其他维护,时间是必不可少的,也经常会用到时间的运算,熟练运用date命令来表示自己想要表示的时间,肯定可以给自己的工作带来诸多方便. 1.命令格式: date [参数 ...

  7. 怎样用Javascript定义一个类

    其实Javascript中没有类这个定义,但是有类这个概念.很多人都写过这样的代码,对,没错,就是如下代码,清晰的不能再清晰了,就是一个关键字 function,然后定义一个方法名,方法名后紧跟一对括 ...

  8. jquery+json实现分页效果

    son作为一种轻量级的数据交换格式,由于其传输数据格式的方便性,今天偶然想将其应用于分页实现,分页做为web开发一个长久的话题,其应用的高效与重要性就不多说了本文主要技术:反射机制,Json数据格式, ...

  9. weblogic安装部署war包——windows

    ### weblogic安装部署war包——windows#### 下载weblogic安装包[csdn下载地址](https://download.csdn.net/download/luozhua ...

  10. oracle_存储过程_没有参数_根据配置自动创建申请单以及写日志事务回滚

    CREATE OR REPLACE PROCEDURE A_MEAS_MIINSP_PLAN_CREATEASvs_msg VARCHAR2(4000);p_PERIODTYPE number; -- ...