Sightseeing trip (POJ - 1734)

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 y_1, ..., y_k, k>2. The road y_i (1<=i<=k-1) connects crossing points x_i and x_{i+1}, the road y_k connects crossing points x_k and x_1. All the numbers x_1,...,x_k 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(y_1)+L(y_2)+...+L(y_k) where L(y_i) is the length of the road y_i (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

The first line of input contains two positive integers: the number of crossing points N<=100 and the number of roads M<=10000. Each of the next M 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).


Output

There is only one line in output. 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 x_1 to x_k 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

Sample Output

1 3 5 2


很好的一道 floyd 求最小环问题,因为够冉(无数次WA)

思路:

题还是挺裸的,求最小环我们要先更新“环”,再更新“最短路”。因为我们的思路是 从 i 直达 k ,再从 k 直达 j ,从 j 再回到 i因为要直达,所以在要在 k 更新最短路之前更新环

奉上代码
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int oo=0x3f3f3f3f;
int n,m,cnt,ans=oo;
int pre[101][101],path[101],mp[101][101],f[101][101];

void floyd()
{
    for(int k=1;k<=n;++k)
    {
        for(int i=1;i<k;++i)
            for(int j=i+1;j<k;++j)
            {
                if(mp[i][k] && mp[k][j] && f[i][j] && mp[i][k]+mp[k][j]+f[i][j]<ans)
                {
                    ans=mp[i][k]+mp[k][j]+f[i][j];
                    int t=j;
                    cnt=0;
                    while(t!=i)
                    {
                        path[++cnt]=t;
                        t=pre[i][t];
                    }
                    path[++cnt]=i;
                    path[++cnt]=k;
                }
            }

        for(int i=1;i<=n;++i)
            for(int j=1;j<=n;++j)
            {
                if(f[i][k] && f[k][j] && ((!f[i][j]) || f[i][k]+f[k][j]<f[i][j]))
                {
                    f[i][j]=f[i][k]+f[k][j];
                    pre[i][j]=pre[k][j];
                }
            }
    }
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;++i) {
        int from,to,val;
        scanf("%d%d%d",&from,&to,&val);
        if(!mp[from][to]) {
            mp[from][to]=mp[to][from]=f[from][to]=f[to][from]=val;
            pre[from][to]=from;
            pre[to][from]=to;
        }
        else mp[from][to]=mp[to][from]=f[from][to]=f[to][from]=min(val,mp[from][to]);
    }
    floyd();
    if(ans==oo) {
        printf("No solution.");
        return 0;
    }
    for(int i=1;i<=cnt;++i) printf("%d ",path[i]);
    return 0;
}

原文地址:https://www.cnblogs.com/qseer/p/9656820.html

时间: 2024-11-06 13:44:54

Sightseeing trip (POJ - 1734)的相关文章

URAL 1004 Sightseeing Trip(最小环)

Sightseeing Trip Time limit: 0.5 secondMemory limit: 64 MB 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 a

HDU 1325 Is It A Tree? (POJ 1308)

并查集问题... 这题以前做过-- 以前做过-- 做过-- 过-- 不过重做时候被吭得异常之爽-- 在判断 vis[i]的时候.我记得标准C++是非0 即为真. 而我用C++ 提交的时候 if(vis[i]) 去直接给我WA了. 用G++ 就AC了...然后改成if(vis[i]==1) 交C++ 就AC了. 特瞄的我每次初始化都把 vis[i] 都赋值为 0 了..都能出这种错? 求路过大神明示我的错误. 题意是判断是否是一棵树. 不能存在森林,用并查集合并,每个点的入度不能超过1. 比如 1

HDU 1535 Invitation Cards (POJ 1511)

两次SPFA.求 来 和 回 的最短路之和. 用Dijkstra+邻接矩阵确实好写+方便交换,但是这个有1000000个点,矩阵开不了. d1[]为 1~N 的最短路. 将所有边的 邻点 交换. d2[] 为 1~N 的最短路. 所有相加为 所要答案. 忧伤的是用SPFA  "HDU 1535"  AC了,但是POJ 一样的题 "POJ 1511" 就WA了. 然后强迫症犯了,不停的去测试. 题意中找到一句关键话 :Prices are positive integ

uva 1484 - Alice and Bob&#39;s Trip(树形dp)

题目链接:uva 1484 - Alice and Bob's Trip 题目大意:Alice和Bob小两口一起出去旅行,他们从0城市出发,Bob喜欢走比较远的路,因为他是个勤奋的好孩子,Alice喜欢走比较近的路,因为她是一个不勤奋的坏孩子,所以有了意见上的分歧,于是乎在出门前他们约法三章,要求说最后的距离值在[l,r]之间,并且由夫妻两轮流做决定,决定说下一个城市去哪里.现在给出n个城市,以及n-1条边,问说在不让Bob媳妇生气的情况下,Bob最远能走多远(不违反约定),如果无法做到不违反约

每日一dp(1)——Largest Rectangle in a Histogram(poj 2559)使用单调队列优化

Largest Rectangle in a Histogram 题目大意: 有数个宽为1,长不定的连续方格,求构成的矩形中最大面积 /************************************************************************/ /* 思路1. 当前为n的面积如何与n-1相联系,dp[i][j]=max(dp[i-1][k]) , 0<k<=j 描述:i为方块个数,j为高度 但是此题目的数据对于高度太变态,h,1000000000 ,n,1

Power string(poj 2406)

题目大意,给出一个字符串s,求最大的k,使得s能表示成a^k的形式,如 abab 可以表示成(ab)^2: 方法:首先 先求kmp算法求出next数组:如果 len mod (len-next[len])==0 ,答案就是 len /(len-next[len]),否则答案是1:证明如下: 如果s能表示成 a^k的形式且k>1,k尽可能大,即s可以表示成aaaaaa(k个a):那么next[len]就等于k-1个a的长度:aaaaaaa  aaaaaaa那么 (len-next[len])a的长

(多重背包+记录路径)Charlie&#39;s Change (poj 1787)

http://poj.org/problem?id=1787 描述 Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at coffee vending machines at motorests. Charlie hates change. That is basically the setup of your next task. Your

Feel Good (poj 2796)

题目大意就是在给出的串中找出一段连续数字,使得 这一段的和 乘上 这一段最小的数 的结果最大. 可以用rmq做.每次区间找当中最小的数,算出值并记录位置.然后再递推它的左右区间. 不过- -,一开始用深搜递推RE了.栈空间不够了,然后慢慢优化,最后还是ac了. 貌似这一题是用单调栈做的,还可以用查并集做...我也是醉了 具体看代码吧 1 /************************************************************************* 2 > F

Sightseeing tour (poj 1637 混合图的欧拉回路)

Language: Default Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7983   Accepted: 3346 Description The city executive board in Lund wants to construct a sightseeing tour by bus in Lund, so that tourists can see every co