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个城市,每队城市之间也许有运输路线,也可能没有.现在有一些货物要从一个城市运到另一个 ...
随机推荐
- HTML坦克大战学习01
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...
- (四)Qt实现自定义模型基于QAbstractTableModel (一般)
Qt实现自定义模型基于QAbstractTableModel 两个例子 例子1代码 Main.cpp #include <QtGui> #include "currencymod ...
- Kafka Tools
参考, https://cwiki.apache.org/confluence/display/KAFKA/System+Tools https://cwiki.apache.org/confluen ...
- div 点击展开
<script type="text/javascript" src="http://libs.baidu.com/jquery/2.1.1/jquery.min. ...
- 关于Struts2的action的execute方法
这个方法必须要有一个String类型的返回值,所以如果写很多if else的话,记得要在最后加一个else,就是无论如何就会放回一个字符串,否则编译会报错,在execute方法名字下面有红线.
- k近邻法(KNN)和KMeans算法
k近邻算法(KNN): 三要素:k值的选择,距离的度量和分类决策规则 KMeans算法,是一种无监督学习聚类方法: 通过上述过程可以看出,和EM算法非常类似.一个简单例子, k=2: 畸变函数(dis ...
- Extracting and composing robust features with denosing autoencoders 论文
这是一篇发表于2008年初的论文. 文章主要讲了利用 denosing autoencoder来学习 robust的中间特征..进上步,说明,利用这个方法,可以初始化神经网络的权值..这就相当于一种非 ...
- 第二百九十三,Memcached缓存
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度.Memcached ...
- e637. 向剪切板获取和粘贴文本
This examples defines methods for getting and setting text on the system clipboard. // If a string i ...
- 10篇写给Git初学者的最佳教程(转)
身为网页设计师或者网页开发者的你,可能已经听说过Git这个正快速成长的版本控制系统.它由GitHub维护:GitHub是一个开放性的.存储众人代码的网站.如果你想学习如何使用Git,请参考本文.在文章 ...