Sightseeing Trip

Time Limit: 2000ms
Memory Limit: 16384KB

This problem will be judged on Ural. Original ID: 1004
64-bit integer IO format: %lld      Java class name: (Any)

 
There is a travel agency in Adelton town on Zanzibar island. It has decided to offer its clients, besides many other attractions, sightseeing the town. To earn as much as possible from this attraction, the agency has accepted a shrewd decision: it is necessary to find the shortest route which begins and ends at the same place.
Your task is to write a program which finds such a route. In the town there are N crossing points numbered from 1 to N and M two-way roads numbered from 1 to M. Two crossing points can be connected by multiple roads, but no road connects a crossing point with itself. Each sightseeing route is a sequence of road numbers y1, …, ykk > 2. The road yi(1 ≤ i ≤ k − 1) connects crossing points xi and xi+1, the road yk connects crossing points xk and x1. All the numbers x1, …, xk should be different. The length of the sightseeing route is the sum of the lengths of all roads on the sightseeing route, i.e. L(y1) + L(y2) + … + L(yk) where L(yi) is the length of the road yi (1 ≤ i ≤ k). Your program has to find such a sightseeing route, the length of which is minimal, or to specify that it is not possible, because there is no sightseeing route in the town.
 

Input

Input contains a series of tests. The first line of each test contains two positive integers: the number of crossing points N ≤ 100 and the number of roads M ≤ 10000. Each of the nextM lines describes one road. It contains 3 positive integers: the number of its first crossing point, the number of the second one, and the length of the road (a positive integer less than 500). Input is ended with a “−1” line.
 

Output

Each line of output is an answer. It contains either a string “No solution.” in case there isn't any sightseeing route, or it contains the numbers of all crossing points on the shortest sightseeing route in the order how to pass them (i.e. the numbers x1 to xk from our definition of a sightseeing route), separated by single spaces. If there are multiple sightseeing routes of the minimal length, you can output any one of them.
 

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

 
解题:Floyd 求最小环
 
 #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的更多相关文章

  1. URAL 1004 Sightseeing Trip(最小环)

    Sightseeing Trip Time limit: 0.5 secondMemory limit: 64 MB There is a travel agency in Adelton town ...

  2. URAL 1004 Sightseeing Trip(floyd求最小环+路径输出)

    https://vjudge.net/problem/URAL-1004 题意:求路径最小的环(至少三个点),并且输出路径. 思路: 一开始INF开大了...无限wa,原来相加时会爆int... 路径 ...

  3. poj1734 Sightseeing trip【最小环】

    Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:8588   Accepted:3224   ...

  4. 「LOJ#10072」「一本通 3.2 例 1」Sightseeing Trip(无向图最小环问题)(Floyd

    题目描述 原题来自:CEOI 1999 给定一张无向图,求图中一个至少包含 333 个点的环,环上的节点不重复,并且环上的边的长度之和最小.该问题称为无向图的最小环问题.在本题中,你需要输出最小环的方 ...

  5. poj 1734 Sightseeing trip判断最短长度的环

    Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5590   Accepted: 2151 ...

  6. 【poj1734】Sightseeing trip

    Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8520   Accepted: 3200 ...

  7. POJ 1734:Sightseeing trip

    Sightseeing trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Special Judge ...

  8. [CEOI1999]Sightseeing trip(Floyed)

    [CEOI1999]Sightseeing trip Description There is a travel agency in Adelton town on Zanzibar island. ...

  9. 「POJ1734」Sightseeing trip

    「POJ1734」Sightseeing trip 传送门 这题就是要我们求一个最小环并且按顺序输出一组解. 考虑 \(O(n^3)\) 地用 \(\text{Floyd}\) 求最小环: 考虑 \( ...

随机推荐

  1. nmon分析文件各sheet含义

    sheet名称sheet含义 SYS_SUMM系统汇总,蓝线为cpu占有率变化情况,粉线为磁盘IO的变化情况: AAA关于操作系统以及nmon本身的一些信息: BBBB系统外挂存储容量以及存储类型: ...

  2. linux网络监控脚本

    http://www.51testing.com/html/92/77492-828434.html

  3. POJ 2137

    水,dp[i][j][k],设为当前为i个牛,在它喜欢的j个位置,而第一个牛在k个位置,很明显了,其实不过是枚举. #include <iostream> #include <cst ...

  4. Mysql 奇怪的连接错误

    今天,碰到了一个数据库连接问题: 不像之前在linux上mysql连接问题,那是权限设置.而这次问题的起源是: 我想要往mysql导入csv文件,可是因为文件比較大.有88M数据:使用navicatc ...

  5. [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 ...

  6. 使用fatjar来实现将包括第三方jar包的项目到处成一个jar包供其它程序使用

    一.在线安装fat jar 在线安装步骤: eclipse菜单条 help >software updates >Search for new features to install> ...

  7. springMVC3.0(文件上传,@RequestMapping加參数,@SessionAttributes,@ModelAttribute,转发,重定向,数值获取,传參,ajax,拦截器)

    1.项目包结构例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsiz ...

  8. 常用的Linux 命令

    来源于面试 求一条linux命令:查找当前目录下所有含有字符串type="json",文件名以.xml的所有文件 find . -name "*.xml"|xa ...

  9. kentico在使用局域网ip访问的时候提示Missing license或者Invalid website

    Missing license Requested URL: http://172.31.212.20/kentico10/ License status: Missing license If yo ...

  10. Oracle的awr和ash

    1.     10g之前 用户的连接将产生会话,当前会话记录保存在v$session中:处于等待状态的会话会被复制一份放在v$session_wait中.当该连接断开后,其原来的连接信息在v$sess ...