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. POI 海量数据

    http://blog.csdn.net/Little_Stars/article/details/8266262

  2. poi读取合并单元格

    poi读取合并单元格 学习了:http://blog.csdn.net/ycb1689/article/details/9764191 进行了列合并单元格的修正:原来是我自己找错了地方: import ...

  3. Mybatis分页插件2.0版本号公布

    项目地址:http://git.oschina.net/free/Mybatis_PageHelper 软件介绍:http://www.oschina.net/p/mybatis_pagehelper ...

  4. itunes connect上传截图提示无法加载文件问题

    解决的方法: 图片名字中不能包括汉字,要英文字母或数字.

  5. VMware虚拟机无法识别U盘解决方式

    1. 本机情况: Winxp操作系统(相同应该适用于win7),VMware虚拟机.虚拟机版本号:VMware 10.安装Ubuntu14.04.现要求在主机上插入U盘.在虚拟机中显示. 2. 遇到问 ...

  6. 英语发音规则---R字母

    英语发音规则---R字母 一.总结 一句话总结: 1.在词首和词中时,字母r常读作摩擦辅音/r/? red /red/ n. 红色 ruler /'ruːlə/ n. 尺:统治者 rub /rʌb/ ...

  7. phpmyadmin客户端多服务器配置

    修改libraries/config.default.php 545行,添加 $cfg['Servers']['2'] = $cfg['Servers'][$i];$cfg['Servers']['2 ...

  8. 比较不错的spring学习博客

    http://blog.csdn.net/tangl_99/article/details/1176141

  9. python初始面向对象

    阅读目录 楔子 面向过程vs面向对象 初识面向对象 类的相关知识 对象的相关知识 对象之间的交互 类命名空间与对象.实例的命名空间 类的组合用法 初识面向对象小结 面向对象的三大特性 继承 多态 封装 ...

  10. 转:如何在Ubuntu 14.04中安装最新版Eclipse

    想必很多开发人员都知道,Ubuntu 软件源中提供的并不是最新版本的 Eclipse,本教程就教大家如何在 Ubuntu 14.04 中快速安装 Eclipse 官方发布的最新版本. 到目前为止,Ec ...