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}\) 求最小环: 考虑 \( ...
随机推荐
- nmon分析文件各sheet含义
sheet名称sheet含义 SYS_SUMM系统汇总,蓝线为cpu占有率变化情况,粉线为磁盘IO的变化情况: AAA关于操作系统以及nmon本身的一些信息: BBBB系统外挂存储容量以及存储类型: ...
- linux网络监控脚本
http://www.51testing.com/html/92/77492-828434.html
- POJ 2137
水,dp[i][j][k],设为当前为i个牛,在它喜欢的j个位置,而第一个牛在k个位置,很明显了,其实不过是枚举. #include <iostream> #include <cst ...
- Mysql 奇怪的连接错误
今天,碰到了一个数据库连接问题: 不像之前在linux上mysql连接问题,那是权限设置.而这次问题的起源是: 我想要往mysql导入csv文件,可是因为文件比較大.有88M数据:使用navicatc ...
- [Angular] Getting to Know the @Attribute Decorator in Angular
So when you using input binding in Angular, it will always check for update. If you want to improve ...
- 使用fatjar来实现将包括第三方jar包的项目到处成一个jar包供其它程序使用
一.在线安装fat jar 在线安装步骤: eclipse菜单条 help >software updates >Search for new features to install> ...
- springMVC3.0(文件上传,@RequestMapping加參数,@SessionAttributes,@ModelAttribute,转发,重定向,数值获取,传參,ajax,拦截器)
1.项目包结构例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsiz ...
- 常用的Linux 命令
来源于面试 求一条linux命令:查找当前目录下所有含有字符串type="json",文件名以.xml的所有文件 find . -name "*.xml"|xa ...
- kentico在使用局域网ip访问的时候提示Missing license或者Invalid website
Missing license Requested URL: http://172.31.212.20/kentico10/ License status: Missing license If yo ...
- Oracle的awr和ash
1. 10g之前 用户的连接将产生会话,当前会话记录保存在v$session中:处于等待状态的会话会被复制一份放在v$session_wait中.当该连接断开后,其原来的连接信息在v$sess ...