ZOJ 1456 Minimum Transport Cost(floyd+后继路径记录)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1456
题意:
求最短路并且输出字典序最小的答案。
思路:
如果用dijkstra来做的话,会比较麻烦,这里直接用floyd会简单的多,只需要记录好后继路径即可。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn = + ; int n, m; int val[maxn];
int mp[maxn][maxn];
int path[maxn][maxn]; void floyd()
{
for(int k=;k<=n;k++)
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(mp[i][k]==INF || mp[k][j]==INF) continue;
int sum=mp[i][k]+mp[k][j]+val[k];
if(mp[i][j]>sum)
{
mp[i][j]=sum;
path[i][j]=path[i][k];
}
else if(mp[i][j]==sum && path[i][j]>path[i][k])
path[i][j]=path[i][k];
}
}
}
} int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d",&n) && n)
{
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;
path[i][j]=j; //i的后继路径为j
}
for(int i=;i<=n;i++) scanf("%d",&val[i]);
floyd(); while(true)
{
int s,t;
scanf("%d%d",&s,&t);
if(s==- && t==-) break;
printf("From %d to %d :\n",s,t);
printf("Path: %d",s);
int tmp=s;
while(tmp!=t)
{
printf("-->%d",path[tmp][t]);
tmp=path[tmp][t];
}
printf("\n");
printf("Total cost : %d\n\n",mp[s][t]);
}
}
return ;
}
ZOJ 1456 Minimum Transport Cost(floyd+后继路径记录)的更多相关文章
- 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+二维数组记录路径)
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 ...
- 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 && 记录路径)
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 (Dijstra 最短路)
Minimum Transport Cost http://acm.hdu.edu.cn/showproblem.php?pid=1385 Problem Description These are ...
- NSOJ Minimum Transport Cost
These are N cities in Spring country. Between each pair of cities there may be one transportation tr ...
- HDU 1385 Minimum Transport Cost( Floyd + 记录路径 )
链接:传送门 题意:有 n 个城市,从城市 i 到城市 j 需要话费 Aij ,当穿越城市 i 的时候还需要话费额外的 Bi ( 起点终点两个城市不算穿越 ),给出 n × n 大小的城市关系图,-1 ...
随机推荐
- 【Cocos2dx 3.3 Lua】导出Cocos2dx API文档
一.Doxygen导出Cocos2dx html doc 1.1 打开Doxygen软件,选择 File-->Open打开Cocos2dx docs目录下的doxyge ...
- mysql 开启慢查询
linux启用MySQL慢查询 vim /etc/my.cnf [mysqld] slow-query-log = on slow_query_log_file = /var/log/slow_que ...
- Django之分页功能
Django提供了一个新的类来帮助你管理分页数据,这个类存放在django/core/paginator.py.它可以接收列表.元组或其它可迭代的对象. 基本语法 class Paginator(ob ...
- Qt 布局管理器
在一个颜值当道的今天,无论买衣服,买车还是追星,颜值的高低已经变成了大家最看重的(不管男性女性都一样,千万别和我说你不是):而对于程序猿来说,开发一款软件,不再只注重逻辑和稳定性,美观和用户友好性也是 ...
- 后台维护常用SQL
OU.库存组织与子库存 select hou.organization_id ou_org_id, --org_id hou.name ou_name, --ou名称 ood.organization ...
- yii2redis安装
yii2 – redis 配置 转自:http://www.fancyecommerce.com/2016/05/03/yii2-redis-%E9%85%8D%E7%BD%AE/ 安装redis w ...
- Python实现Json结构对比的小工具兼谈编程求解问题
摘要: 通过使用Python编写一个解析Json结构对比的小工具,来提炼编程求解的通用步骤和技巧. 难度: 初级 先上代码. jsondiff.py #!/usr/bin/python #_*_enc ...
- centos7.3安装redis
yum install epel-release yum install redis 如果支持从其他机器能访问,需要修改配置文件 /etc/redis.conf,注释掉 bin 127.0.0.1 如 ...
- python之路----模块与序列化模块
认识模块 什么是模块 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用pyt ...
- SNMP学习笔记之iReasoning MIB Browser
0x00 MIB Browser iReasoning MIB浏览器是一个强大和易于使用的工具由iReasoning SNMP API提供支持. MIB浏览器是工程师管理启用SNMP的网络设备和应用程 ...