Floyd求字典序最小的路径
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
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.
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.
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
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求字典序最小的路径的更多相关文章
- HDU 5915 The Fastest Runner Ms. Zhang (CCPC2016 长春 E题,分类讨论 + 求字典序最小的直径 + 数据结构寻找最小值)
题目链接 CCPC2016 Changchun Problem E 题意 给定一个$n$个点$n$条边的无向图,现在从某一点$s$出发,每个点都经过一遍,最后在$t$点停止,经过的边数为$l$ ...
- POJ 2337 Catenyms (有向图欧拉路径,求字典序最小的解)
Catenyms Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8756 Accepted: 2306 Descript ...
- [poj2337]求字典序最小欧拉回路
注意:找出一条欧拉回路,与判定这个图能不能一笔联通...是不同的概念 c++奇怪的编译规则...生不如死啊... string怎么用啊...cincout来救? 可以直接.length()我也是长见识 ...
- hdu1599 find the mincost route floyd求出最小权值的环
find the mincost route Time Limit: 1000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- hdu 1814 Peaceful Commission (2-sat 输出字典序最小的路径)
Peaceful Commission Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HDU 2296 Ring -----------AC自动机,其实我想说的是怎么快速打印字典序最小的路径
大冥神的代码,以后能贴的机会估计就更少了....所以本着有就贴的好习惯,= =....直接贴 #include <bits/stdc++.h> using LL = long long ; ...
- 图论--2-SAT--暴力染色法求字典序最小模版
#include <cstdio> #include <cstring> #include <stack> #include <queue> #incl ...
- hdu1385 Minimum Transport Cost 字典序最小的最短路径 Floyd
求最短路的算法最有名的是Dijkstra.所以一般拿到题目第一反应就是使用Dijkstra算法.但是此题要求的好几对起点和终点的最短路径.所以用Floyd是最好的选择.因为其他三种最短路的算法都是单源 ...
- ZOJ-1456 Minimum Transport Cost---Floyd变形+路径输出字典序最小
题目链接: https://vjudge.net/problem/ZOJ-1456 题目大意: Spring国家有N个城市,每队城市之间也许有运输路线,也可能没有.现在有一些货物要从一个城市运到另一个 ...
随机推荐
- json拼接转义符
//转义符替 function escapeReplace(Str) { var newStrJson = Str.replace(/\%/g, "%25"); newStrJso ...
- 【MySQL】MySQL 常用语法之锁表与解锁表
mysql 锁表语句: Lock锁整张表: 写锁定: LOCK TABLES products WRITE: 写锁,锁定之后,只有当前线程可以进行读操作和写操作,其他线程读操作和写操作均被堵塞.... ...
- 用C结构体来实现面向对象编程,ti xDAIS标准算法就这么搞的(1)
用C结构体来实现面向对象编程,ti xDAIS标准算法就这么搞的. 测试代码如下: #include <stdio.h> #include <stdlib.h> #includ ...
- 美化VC界面(用户登录界面)
源代码:下载 VC开发程序单调的界面相信大家都是深有感触,提到界面美化编程,人们都会说做界面不要用VC写,太难了.一句俗语:难者不会,会者不难.VC的美化界面编程并没有人们想像的那么难.这篇文章是我写 ...
- 让Android 变身回一台真正的Linux系统吧!!!
在Android上开发也有两年的时间了,虽然一直都知道Android是构建在Linux Kernel上的手机操作系统,但在此之前一直没有实感. 直到第一次买了Android的手机,并请人帮我Root后 ...
- R语言boxplot绘图函数
boxplot 用于绘制箱线图,我们都知道boxplot 用于展示一组数据的总体分布,在R语言中,支持两种输入数据的方式 第一种:x , 这个参数指定用于绘制箱线图所用的数据,是一个向量 代码示例: ...
- vnc 多用户登录
1, 创建新用户: $ useradd tom $ passwd tom 2, 登录到tom账户,创建vnc实例: $ su tom$ vncserver 这时可以看看~/.vnc/目录下,有一些如 ...
- UIScrollView 的代理方法简单注解
//减速停止了时执行,手触摸时执行执行 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView; //只要滚动了就会触发 ...
- linux nfs
linux(十四)之linux NFS服务管理 学到这里差不多就结束了linux的基础学习了,其实linux的内容并不难,我们要经常的反复的去操作它,多多和它去联络感情才能很好的掌握这个linux. ...
- ubuntu12.04 修改登陆用户 为root
Ubuntu 12.04默认是不允许root登录的,在登录窗口只能看到普通用户和访客登录.以普通身份登陆Ubuntu后我们需要做一些修改,普通用户登录后,修改系统配置文件需要切换到超级用户模式,在终端 ...