poj1734Sightseeing trip

Sightseeing trip

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6811   Accepted: 2602   Special Judge

Description

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

Source

CEOI 1999

题意:给出你一个无向图,要你求一个最小环,最小环的最少有3个节点,如果有环则顺序输出节点上的点,如果没有环,输出NO solution.

分析:其实用dfs就可以AC了,但是我发现其他dalao都用floyd做,学习了一下。

经过分析可以得到,包含i和j的最小环其实就是i到j的最短路和i到j的次短路组成的,至于为什么,很好想,因为组成了一个环,那么从i到j就有两条不同的路可以走,而要求最小环,所以就是i到j的最短路和i到j的次短路组成的。

其实我们就是要求一点不在i到j的最短路上的k,那么最小环=min{d[i][j] + a[i][k] + a[k][j]},根据floyd算法,我们先枚举k,然后枚举i和j,不超过k即可(因为k不在i到j的最短路上),然后记录一下路径即可.路径记录可以递归,也可以就用数组记录前驱。

#include <cstring>
#include <cstdio>
#include <iostream>
#include <algorithm>

#define inf 0x7ffffff  

using namespace std;

int n, m,a[110][110],d[110][110],head[110][110],res,tot,ans[10010];

int main()
{
    while (~scanf("%d%d", &n, &m))
    {
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
            {
            a[i][j] = d[i][j] = inf;
            head[i][j] = i;
            }
        for (int i = 1; i <= m; i++)
        {
            int x, y, z;
            scanf("%d%d%d", &x, &y, &z);
            a[x][y] = a[y][x] = d[x][y] = d[y][x] = min(z, a[x][y]);
        }
        res = inf;
        for (int k = 1; k <= n; k++)
        {
            for (int i = 1; i < k; i++)
            {
                for (int j = i + 1; j < k; j++)
                {
                    int temp = d[i][j] + a[i][k] + a[k][j];
                    if (temp < res)
                    {
                        res = temp;
                        tot = 0;
                        int p = j;
                        while (p != i)
                        {
                            ans[++tot] = p;
                            p = head[i][p];
                        }
                        ans[++tot] = i;
                        ans[++tot] = k;
                    }
                }
            }
            for (int i = 1; i <= n; i++)
                for (int j = 1; j <= n; j++)
                    if (d[i][j] > d[i][k] + d[k][j])
                    {
                d[i][j] = d[i][k] + d[k][j];
                head[i][j] = head[k][j];
                    }
        }
        if (res == inf)
            puts("No solution.\n");
        else
        {
            printf("%d", ans[1]);
            for (int i = 2; i <= tot; i++)
                printf(" %d", ans[i]);
            printf("\n");
        }
    }

    return 0;
}
时间: 2024-08-02 11:04:05

poj1734Sightseeing trip的相关文章

生成树&amp;最短路总结篇

1.模板题  我是用prim搞得 给出每点坐标求最小生成树 hdu1162Eddy's picture 最小生成树 #include <iostream> #include<cstdio> #include<cstring> #include<cmath> using namespace std; int flag1=0; double sum; double arr_list[110][110]; struct Edge { int point; doub

POJ 1041 John&#39;s trip 无向图的【欧拉回路】路径输出

欧拉回路第一题TVT 本题的一个小技巧在于: [建立一个存放点与边关系的邻接矩阵] 1.先判断是否存在欧拉路径 无向图: 欧拉回路:连通 + 所有定点的度为偶数 欧拉路径:连通 + 除源点和终点外都为偶数 有向图: 欧拉回路:连通 + 所有点的入度 == 出度 欧拉路径:连通 + 源点 出度-入度=1 && 终点 入度 - 出度 = 1 && 其余点 入度 == 出度: 2.求欧拉路径 : step 1:选取起点(如果是点的度数全为偶数任意点为S如果有两个点的度数位奇数取一

poj1734 Sightseeing trip

Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6919   Accepted: 2646   Special Judge Description There is a travel agency in Adelton town on Zanzibar island. It has decided to offer its clients, besides many other attra

HDU 1038[Biker&#39;s Trip Odometer]简单计算

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1038 题目大意:给轮子直径,转数,时间.要求输出走过的距离和时速(mile为单位) 关键思想:纯算 代码如下: #include <iostream> using namespace std; #define pi 3.1415927 int main(){ double d,r,t,l; int cnt=1; while(cin>>d>>r>>t&&a

暑假练习赛 003 F Mishka and trip

F - Mishka and trip Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description Input Output Sample Input Sample Output Hint Description Peter Parker wants to play a g

1999 Central European Olympiad in Informatics - Sightseeing Trip

算法提示 最小环问题 题目大意 在一张带权无向图上,找出至少含 3 个点且权值和最小的环,并按环上的循序输出环上的点.存在重边,无自环. 做法分析 参考最小环问题,在更新 dist[i][j] 时,记录更新其的点 k,便于回溯路径. 参考代码 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <queue> 5 #include <algorithm> 6

三分 --- POJ 3301 Texas Trip

Texas Trip Problem's Link:   http://poj.org/problem?id=3301 Mean: 给定n(n <= 30)个点,求出包含这些点的面积最小的正方形的面积. analyse: 首先要确定的是旋转的角度在0到180度之间即可,超过180度是和前面的相同的. 坐标轴旋转后,坐标变换为: X’ = x * cosa - y * sina; y’ = y * cosa + x * sina; Time complexity: O(n) Source code

杭电 HDU 1038 Biker&#39;s Trip Odometer

Biker's Trip Odometer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4745    Accepted Submission(s): 3144 Problem Description Most bicycle speedometers work by using a Hall Effect sensor faste

POJ 1041 John&#39;s trip (无向图欧拉回路)

John's trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7433   Accepted: 2465   Special Judge Description Little Johnny has got a new car. He decided to drive around the town to visit his friends. Johnny wanted to visit all his frien