Ural 1004 Sightseeing Trip
Sightseeing Trip
This problem will be judged on Ural. Original ID: 1004
64-bit integer IO format: %lld Java class name: (Any)
Input
Output
Sample Input
5 7
1 4 1
1 3 300
3 1 10
1 2 16
2 3 100
2 5 15
5 3 20
4 3
1 2 10
1 3 20
1 4 30
-1
Sample Output
1 3 5 2
No solution.
Source
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = ;
int n,m,d[maxn][maxn],w[maxn][maxn],fa[maxn][maxn];
vector<int>cycle;
int Floyd() {
int minCycle = INF;
for(int k = ; k <= n; ++k) {
for(int i = ; i < k; ++i)
for(int j = i + ; j < k && w[i][k] < INF; ++j) {
int tmp = d[i][j] + w[i][k] + w[k][j];
if(tmp < minCycle) {
minCycle = tmp;
cycle.clear();
int p = j;
while(p != i) {
cycle.push_back(p);
p = fa[i][p];
}
cycle.push_back(i);
cycle.push_back(k);
}
}
for(int i = ; i <= n; ++i)
for(int j = ; j <= n && d[i][k] < INF; ++j) {
int tmp = d[i][k] + d[k][j];
if(tmp < d[i][j]) {
d[i][j] = tmp;
fa[i][j] = fa[k][j];
}
}
}
return minCycle;
}
int main() {
int u,v,ww;
while(~scanf("%d",&n)) {
if(n == -) return ;
scanf("%d",&m);
for(int i = ; i < maxn; ++i)
for(int j = ; j < maxn; ++j) {
d[i][j] = w[i][j] = INF;
fa[i][j] = i;
}
while(m--) {
scanf("%d%d%d",&u,&v,&ww);
ww = min(ww,w[u][v]);
w[u][v] = w[v][u] = d[u][v] = d[v][u] = ww;
}
if(Floyd() == INF) puts("No solution.");
else {
printf("%d",cycle[]);
for(int i = ; i < cycle.size(); ++i)
printf(" %d",cycle[i]);
putchar('\n');
}
}
return ;
}
Ural 1004 Sightseeing Trip的更多相关文章
- URAL 1004 Sightseeing Trip(最小环)
Sightseeing Trip Time limit: 0.5 secondMemory limit: 64 MB There is a travel agency in Adelton town ...
- URAL 1004 Sightseeing Trip(floyd求最小环+路径输出)
https://vjudge.net/problem/URAL-1004 题意:求路径最小的环(至少三个点),并且输出路径. 思路: 一开始INF开大了...无限wa,原来相加时会爆int... 路径 ...
- poj1734 Sightseeing trip【最小环】
Sightseeing trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions:8588 Accepted:3224 ...
- 「LOJ#10072」「一本通 3.2 例 1」Sightseeing Trip(无向图最小环问题)(Floyd
题目描述 原题来自:CEOI 1999 给定一张无向图,求图中一个至少包含 333 个点的环,环上的节点不重复,并且环上的边的长度之和最小.该问题称为无向图的最小环问题.在本题中,你需要输出最小环的方 ...
- poj 1734 Sightseeing trip判断最短长度的环
Sightseeing trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5590 Accepted: 2151 ...
- 【poj1734】Sightseeing trip
Sightseeing trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8520 Accepted: 3200 ...
- POJ 1734:Sightseeing trip
Sightseeing trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Special Judge ...
- [CEOI1999]Sightseeing trip(Floyed)
[CEOI1999]Sightseeing trip Description There is a travel agency in Adelton town on Zanzibar island. ...
- 「POJ1734」Sightseeing trip
「POJ1734」Sightseeing trip 传送门 这题就是要我们求一个最小环并且按顺序输出一组解. 考虑 \(O(n^3)\) 地用 \(\text{Floyd}\) 求最小环: 考虑 \( ...
随机推荐
- 2015 Multi-University Training Contest 6 hdu 5357 Easy Sequence
Easy Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- C#中的Attribute Property区别
Attribute 一般译作"特性",Property 仍然译为"属性". Attribute 是一种可由用户自由定义的修饰符(Modifier),可以用来修饰 ...
- POJ 2470 Ambiguous permutations(简单题 理解题意)
[题目简述]:事实上就是依据题目描写叙述:A permutation of the integers 1 to n is an ordering of these integers. So the n ...
- 0x07 贪心
被虐爆了...贪心这种玄学东西还可以证吗??除了范围缩放算是可以想想比较经典(倍增第一题?)... poj3614:这道题想了很久,并没有想到是把minSPF按大到小排序,一直的思想是小的就小到大排序 ...
- [jzoj 6101] [GDOI2019模拟2019.4.2] Path 解题报告 (期望)
题目链接: https://jzoj.net/senior/#main/show/6101 题目: 题解: 设$f_i$表示从节点$i$到节点$n$的期望时间,$f_n=0$ 最优策略就是如果从$i, ...
- POJ 3150 循环矩阵的应用
思路: 首先 先普及一个性质: 循环矩阵*循环矩阵=循环矩阵 由于此题是距离小于d的都加上一个数. 那么 构造矩阵的时候 我们发现 诶呦 这是个循环矩阵 看看数据范围 n^2log(k)可以过. 那就 ...
- mybatis的sql语句导致索引失效,使得查询超时
mybaitis书写sql需要特别注意where条件中的语句,否则将会导致索引失效,使得查询总是超时.如下语句会出现导致索引失效的情况: with test1 as (select count(C_F ...
- week3_notebooke1
今日内容:编码集合深浅cpoy文件操作函数初始函数函数的返回值函数的传参 初识: # == 数值比较 # is 比较的是内存地址 # id 测试的是内存地址 # 小数据池 str int # int: ...
- CentOS 安装 PHP 扩展
下载地址:https://pecl.php.net/package/redis 上传目录:/usr/local/src //安装依赖 yum install php-devel -y //进入安装包目 ...
- python包管理(distutils、easy_install、pip、setup.py/requirements.txt、wheel)
distutils.distutils2 distutils是 python 标准库的一部分,2000年发布.使用它能够进行 python 模块的 安装 和 发布. distutils2 被设计为 d ...