HDU1385 Minimum Transport Cost (Floyd)
Minimum Transport Cost
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10029 Accepted Submission(s): 2716
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.
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.
题目大意:
有N个城市,然后直接给出这些城市之间的邻接矩阵,矩阵中-1代表那两个城市无道路相连,其他值代表路径长度。
如果一辆汽车经过某个城市,必须要交一定的钱(可能是过路费)。
现在要从a城到b城,花费为路径长度之和,再加上除起点与终点外所有城市的过路费之和。
求最小花费,如果有多条路经符合,则输出字典序最小的路径。
感觉这一题就是为Floyd算法而生的,用Floyd很简单。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int N=;
int n;
int w[N][N];
int path[N][N];
int charge[N];
void init()
{
for(int i=; i<=n; ++i)
{
for(int j=; j<=n; ++j)
{
if(i!=j)w[i][j]=inf;
else w[i][j]=;
path[i][j] = j; // path[i][j]表示点i到j经过的第一个点`
}
}
}
void Floyd()
{
for(int k=; k<=n; ++k)
for(int i=; i<=n; ++i)
for(int j=; j<=n; ++j)
if(w[i][k]!=inf && w[k][j]!=inf)
{
int tmp = w[i][k]+w[k][j]+charge[k];
if(w[i][j] > tmp)
{
w[i][j] = tmp;
path[i][j] = path[i][k];
}
else if(w[i][j] == tmp && path[i][j]>path[i][k])//选择字典序小的
{
path[i][j] = path[i][k];
}
}
}
int main()
{ int i,j,src,des;
while(scanf("%d",&n),n)
{
init();
for(i=; i<=n; ++i)
{
for(j=; j<=n; ++j)
{
scanf("%d",&w[i][j]);
if(w[i][j]==-) w[i][j]=inf;
}
}
for(i=; i<=n; ++i)
scanf("%d",&charge[i]);
Floyd();
while(scanf("%d%d",&src,&des))
{
if(src==-&&des==-) break;
printf("From %d to %d :\n",src,des);
printf("Path: ");
int u = src;
printf("%d",u);
while(u != des)
{
printf("-->%d",path[u][des]);
u = path[u][des];
}
puts("");
printf("Total cost : %d\n\n", w[src][des]);
}
}
return ;
}
HDU1385 Minimum Transport Cost (Floyd)的更多相关文章
- hdu 1385 Minimum Transport Cost(floyd && 记录路径)
Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- ZOJ 1456 Minimum Transport Cost(Floyd算法求解最短路径并输出最小字典序路径)
题目链接: https://vjudge.net/problem/ZOJ-1456 These are N cities in Spring country. Between each pair of ...
- HDU 1385 Minimum Transport Cost( Floyd + 记录路径 )
链接:传送门 题意:有 n 个城市,从城市 i 到城市 j 需要话费 Aij ,当穿越城市 i 的时候还需要话费额外的 Bi ( 起点终点两个城市不算穿越 ),给出 n × n 大小的城市关系图,-1 ...
- hdu 1385 Minimum Transport Cost (floyd算法)
貌似···················· 这个算法深的东西还是很不熟悉!继续学习!!!! ++++++++++++++++++++++++++++ ======================== ...
- ZOJ 1456 Minimum Transport Cost(floyd+后继路径记录)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1456 题意:求最短路并且输出字典序最小的答案. 思路:如果用dijkstr ...
- HD1385Minimum Transport Cost(Floyd + 输出路径)
Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- HDU 1385 Minimum Transport Cost (最短路,并输出路径)
题意:给你n个城市,一些城市之间会有一些道路,有边权.并且每个城市都会有一些费用. 然后你一些起点和终点,问你从起点到终点最少需要多少路途. 除了起点和终点,最短路的图中的每个城市的费用都要加上. 思 ...
- hdu1385 Minimum Transport Cost 字典序最小的最短路径 Floyd
求最短路的算法最有名的是Dijkstra.所以一般拿到题目第一反应就是使用Dijkstra算法.但是此题要求的好几对起点和终点的最短路径.所以用Floyd是最好的选择.因为其他三种最短路的算法都是单源 ...
- ZOJ1655 Transport Goods(Floyd)
利用Floyd的DP状态转移方程. #include<cstdio> #include<cstring> #include<queue> #include<a ...
随机推荐
- [CF912A]Tricky Alchemy
题意:你有a个黄水晶和b个蓝水晶,要求要x个黄水晶球(2黄),y个绿水晶球(1黄1蓝),z个蓝水晶球(3蓝),问还要多少水晶题解:模拟 C++ Code: #include<cstdio> ...
- 2018宁夏邀请赛K Vertex Covers
题目链接:https://nanti.jisuanke.com/t/28411 题意: 给出n(n<=36)个点的一个图.求点覆盖集数. 题解: 将n个点折半为L和R两部分.对于R内部的边,枚举 ...
- 【COGS 1873】 [国家集训队2011]happiness(吴确) 最小割
这是一种最小割模型,就是对称三角,中间双向边,我们必须满足其最小割就是满足题目条件的互斥关系的最小舍弃,在这道题里面我们S表示文T表示理,中间一排点是每个人,每个人向两边连其选文或者选理的价值,中间每 ...
- PAT团体程序设计大赛---(模拟)
L1-1 古风排版(20 分) 中国的古人写文字,是从右向左竖向排版的.本题就请你编写程序,把一段文字按古风排版. 输入格式: 输入在第一行给出一个正整数N(<100),是每一列的字符数.第二行 ...
- Linux内存 性能调优
内存是影响Linux性能的主要因素之一,内存资源的充足与否直接影响应用系统的使用性能. free命令:监控Linux内存使用状况. 由上图可知,空闲内存是free+buffers+cached=155 ...
- Bash 实例,第二部分
我们先看一下处理命令行自变量的简单技巧,然后再看看 bash 基本编程结构. 接收自变量 在 介绍性文章 中的样本程序中,我们使用环境变量 "$1" 来引用第一个命令行自变量.类似 ...
- c语言指针学习【转】
前言 近期俄罗斯的陨石.四月的血月.五月北京的飞雪以及天朝各种血腥和混乱,给人一种不详的预感.佛祖说的末法时期,五浊恶世 ,十恶之世,人再无心法约束,道德沦丧,和现在正好吻合.尤其是在天朝,空气,水, ...
- Security+考试通过心得
Security+ Security+ 认证是一种中立第三方认证,其发证机构为美国计算机行业协会CompTIA:是和CISSP.CISA等共同包含在内的国际IT业热门认证之一,和CISSP偏重信息安全 ...
- 爆破phpmyadmin小脚本
#!usr/bin/env python #encoding: utf-8 #by i3ekr import requests headers = {'Content-Type':'applicati ...
- UVA 10183 How Many Fibs?
高精度推出大概600项fabi数,就包含了题目的数据范围,对于每组a,b,从1到600枚举res[i]即可 可以直接JAVA大数.我自己时套了C++高精度的版 JAVA 复制别人的 import ja ...