NSOJ Minimum Transport Cost
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
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
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
3 5
2 4
-1 -1
0
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还是没有深刻的理解....用Floyd存路径很是不错...get....
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f
#define MAX 111 int cost[MAX];
int mp[MAX][MAX],path[MAX][MAX];
int n; void floyd()
{
for(int k=; k<=n; k++)
for(int i=; i<=n; i++)
for(int j=; j<=n; j++){
int ans=mp[i][k]+mp[k][j]+cost[k];
if(mp[i][j]>ans){
mp[i][j]=ans;
path[i][j]=path[i][k];
}
else if(mp[i][j]==ans && path[i][j]>path[i][k]){
path[i][j]=path[i][k];
}
}
} int main()
{
while(~scanf("%d",&n)&&n!=){
for(int i=; i<=n; i++){
for(int j=; j<=n; j++){
if(i!=j)
mp[i][j]=INF;
else
mp[i][j]=;
path[i][j]=j;
}
}
for(int i=; i<=n; i++){
for(int j=; j<=n; j++){
scanf("%d",&mp[i][j]);
if(mp[i][j]==-)
mp[i][j]=INF;
}
}
for(int i=; i<=n; i++){
scanf("%d",&cost[i]);
}
floyd();
int a,b;
while(~scanf("%d%d",&a,&b)){
if(a==-&&b==-)
break;
printf("From %d to %d :\n",a,b);
printf("Path: ");
int s=a;
while(s!=b){
printf("%d-->",s);
s=path[s][b];
}
printf("%d\n",b);
printf("Total cost : %d\n\n", mp[a][b]);
} }
}
NSOJ Minimum Transport Cost的更多相关文章
- HDU 1385 Minimum Transport Cost (Dijstra 最短路)
Minimum Transport Cost http://acm.hdu.edu.cn/showproblem.php?pid=1385 Problem Description These are ...
- Minimum Transport Cost(floyd+二维数组记录路径)
Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- HDU1385 Minimum 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(floyd && 记录路径)
Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- hdu 1385 Minimum Transport Cost (Floyd)
Minimum Transport CostTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- ZOJ 1456 Minimum Transport Cost(Floyd算法求解最短路径并输出最小字典序路径)
题目链接: https://vjudge.net/problem/ZOJ-1456 These are N cities in Spring country. Between each pair of ...
- Minimum Transport Cost Floyd 输出最短路
These are N cities in Spring country. Between each pair of cities there may be one transportation tr ...
- hdu 1385 Minimum Transport Cost (floyd算法)
貌似···················· 这个算法深的东西还是很不熟悉!继续学习!!!! ++++++++++++++++++++++++++++ ======================== ...
- HDU 1385 Minimum Transport Cost (最短路,并输出路径)
题意:给你n个城市,一些城市之间会有一些道路,有边权.并且每个城市都会有一些费用. 然后你一些起点和终点,问你从起点到终点最少需要多少路途. 除了起点和终点,最短路的图中的每个城市的费用都要加上. 思 ...
随机推荐
- fork与vfork详解
一.fork函数 要创建一个进程,最基本的系统调用是fork,系统调用fork用于派生一个进程,函数原型如下: pid_t fork(void) 若成功,父进程中返回子进程ID,子进程中返回0,若出 ...
- LinearLayout具体解释一:LinearLayout的简单介绍
LinearLayout,中文意思是线性布局.假设你是初学android的,肯定会非常困惑"啥叫布局",啥又叫"线性布局"呢. 有的时候,我尝试用官方的语言去解 ...
- Android 学习历程摘要(二)
1.资源文件命名仅仅能小写,否则会报错生成不了R.java文件 2.R文件导包时应该导入自己project的包,而不是android.R 3.数据库操作使用SqliteOpenHelper 4.val ...
- Zygote过程【3】——SystemServer诞生
欢迎转载.转载请注明:http://blog.csdn.net/zhgxhuaa 在ZygoteInit的main()方法中做了几件大事.当中一件便是启动Systemserver进程.代码例如以下: ...
- 集合hashCode()方法和equals()办法
1.哈希码: Object中的HashCode方法会返回该对象的的内存真实地址的整数化表示,这个形象的不是真正抵制的整数值就是哈希码. 2.利用哈希码向集合中插入数据的顺序? ...
- iOS开发- Xcode插件(一)-规范凝视生成器VVDocumenter
分享几个经常使用的Xcode插件. 第一个, 规范凝视生成器VVDocumenter. 顾名思义, 它能够非常方便的为你自己主动加入�凝视 使用效果例如以下: 下载链接:https://github. ...
- 在前端一定要了解的HTML,CSS知识
盒子模型 每个盒子都有4个属性:内容(content).填充(padding).边框(border).边界(margin) 每个属性都有四个部分:上.右.下.左 块级元素 内联元素 块级元素(bloc ...
- ASP.NET之AdRotator实现淘宝浏览页面的商品随机推荐功能
如今随便上个网都能够看到淘宝.京东等各大电商平台的双十一购物狂欢宣传,从2009年開始淘宝愣是把11.11这一天打造成了全民购物狂欢节.阿里巴巴的上市更是激发了阿里人的斗志,据说他们今年的目标是100 ...
- 开发随笔——NOT IN vs NOT EXISTS
原文:开发随笔--NOT IN vs NOT EXISTS 原文出处: http://blog.csdn.net/dba_huangzj/article/details/31374037 转载请引用 ...
- Java爬虫,信息抓取的实现(转)
转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/23272657 今天公司有个需求,需要做一些指定网站查询后的数据的抓取,于是花了点 ...