zoukankan      html  css  js  c++  java
  • #POJ整理 P1734 Sightseeing trip

    POJ P1734 Sightseeing trip

    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.

    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 (SPJ)
    

    思路

    意思就是找图里面的最小环 (我就是想装逼)。回顾一下(Floyed)最短路算法:

    [d[i][j] = min(d[i][k] + d[k][j]) ]

    也就是说在循环的时候,第k次更新之前,数组里面保存着以前k-1个点贼转移路径,从i到j的最短路。那么下式

    [ans = min(d[i][j] + a[j][k] + a[k][i]) ]

    就可以用来表示只通过前k个点,且一定通过ijk三个点的最小环的大小。已经保证了ij之间的距离是最小的,就只需要枚举ij两个节点,分别和进行运算,将结果进行比较就可。

    另外,题目要求输出方案。代码实现的时候需要用一个数组pos[i][j]表示在d[i][j]中存储的最小值的路径中间点。

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <vector>
    using namespace std;
    long long n,m;
    long long a[200][200],d[200][200],pos[200][200] = {};
    long long ans = 2147483647;
    vector <long long> path;
    void get_path(long long x,long long y){
        if(pos[x][y] == 0) return;
        get_path(x , pos[x][y]);
        path.push_back(pos[x][y]);//先从x走到pos[x][y],再把pos[x][y]入队,最后从pos[x][y]走到y
        get_path(pos[x][y] , y);
    }
    int main(){
        cin >> n >> m;
        for(int i = 1;i <= n; i++)for(int j = 1;j <= n; j++) a[i][j] = d[i][j] = 2147483647;
        for(long long i = 1;i <= m; i++){
            long long x,y,z;
            cin >> x >> y >> z;
            // a[x][y] = z;
            // d[x][y] = z;
            a[x][y] = a[y][x] = min(a[x][y],z);
            d[x][y] = d[y][x] = min(d[x][y],z); 
        }
        for(long long k = 1;k <= n; k++){
            for(long long i = 1;i < k; i++)
                for(long long j = i+1;j < k; j++){
                    if(ans > d[i][j] + a[j][k] + a[k][i]){
                        ans = d[i][j] + a[j][k] + a[k][i];
                        path.clear();
                        path.push_back(i);
                        get_path(i,j);
                        path.push_back(j);
                        path.push_back(k);
                    }
                }
            for(long long i = 1;i <= n; i++)
                for(long long j = 1;j <= n; j++)
                    if(d[i][j] > d[i][k] + d[k][j]){
                        d[i][j] = d[i][k] + d[k][j];
                        pos[i][j] = k;
                    }
        }
        if(!path.size()){//如果没有答案
            printf("No solution.
    ");
            return 0;
        }
        for(long long i = 0;i < path.size(); i++) cout << path[i] << ' ';
        cout << endl;
        return 0;
    }
    
  • 相关阅读:
    许家骏
    平均得分 【杭州电-HDOJ-2023】 附加题+详细说明
    百度之星的第二个问题
    kendo ui 单击取消编辑数据grid减少的原因和治疗方法的数据
    2013年第36周准备考下半年的项目管理师
    2013年第36周三杂记
    2013第36周二小结
    2013第36周星期一
    2013年9月1日下午
    2013年8月最后一天晚上
  • 原文地址:https://www.cnblogs.com/Cao-Yucong/p/13158752.html
Copyright © 2011-2022 走看看