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. 循环时自动打开url

    'systemutil.Run "C:\Program Files (x86)\HP\QuickTest Professional\samples\flight\app\flight4a.e ...

  2. HDU6010 Daylight Saving Time

    /* HDU6010 Daylight Saving Time http://acm.hdu.edu.cn/showproblem.php?pid=6010 模拟 题意:算当前时间是否是夏令时 */ ...

  3. 页面安装Jre

    <object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" width="0" height ...

  4. [using_microsoft_infopath_2010]Chapter13 SharePoint视图和控制板

    本章概要: 1.规划站点的外观,使用View和web部件 2.为登录进来的用户展示相关的表单. 3.为管理员创建视图 4.从表单中收集到的数据中创建报表

  5. .NET平台开源JSON库LitJSON的使用方法

    下载地址:LitJson.dll下载 一个简单示例: String str = "{'name':'cyf','id':10,'items':[{'itemid':1001,'itemnam ...

  6. six.moves的用法

    six是用来兼容python 2 和 3的,我猜名字就是用的2和3的最小公倍数. six.moves 是用来处理那些在2 和 3里面函数的位置有变化的,直接用six.moves就可以屏蔽掉这些变化 P ...

  7. 不可靠信号SIGCHLD丢失的问题

    如果采用 void sig_chld(int signo) {        pid_t pid;        int stat;                while((pid = waitp ...

  8. svn 服务器的搭建

     SVN服务器运行模式:模式1:svn服务器单独运行  监听: 3690端口    访问: svn://IP模式2: svn 服务器+ apache   : 80 端口  访问: http://IP  ...

  9. WebService CXF学习:复杂对象传递(List,Map)

    转自:https://blog.csdn.net/z69183787/article/details/35988335 第一步:创建存储复杂对象的类(因为WebServices的复杂对象的传递,一定要 ...

  10. [Tomcat]Tomcat6和Tomcat7的区别

    Tomcat 7最大的改进是对Servlet 3.0和Java EE 6的支持.◆Tomcat 7完全支持Servlet 3.0规范◆Tomcat 7新增了对Java注释的支持◆Tomcat 7通过w ...