zoukankan      html  css  js  c++  java
  • Codeforces Round #349 (Div. 2) D. World Tour 暴力最短路

    D. World Tour
     

    A famous sculptor Cicasso goes to a world tour!

    Well, it is not actually a world-wide. But not everyone should have the opportunity to see works of sculptor, shouldn't he? Otherwise there will be no any exclusivity. So Cicasso will entirely hold the world tour in his native country — Berland.

    Cicasso is very devoted to his work and he wants to be distracted as little as possible. Therefore he will visit only four cities. These cities will be different, so no one could think that he has "favourites". Of course, to save money, he will chose the shortest paths between these cities. But as you have probably guessed, Cicasso is a weird person. Although he doesn't like to organize exhibitions, he likes to travel around the country and enjoy its scenery. So he wants the total distance which he will travel to be as large as possible. However, the sculptor is bad in planning, so he asks you for help.

    There are n cities and m one-way roads in Berland. You have to choose four different cities, which Cicasso will visit and also determine the order in which he will visit them. So that the total distance he will travel, if he visits cities in your order, starting from the first city in your list, and ending in the last, choosing each time the shortest route between a pair of cities — will be the largest.

    Note that intermediate routes may pass through the cities, which are assigned to the tour, as well as pass twice through the same city. For example, the tour can look like that: . Four cities in the order of visiting marked as overlines:[1, 5, 2, 4].

    Note that Berland is a high-tech country. So using nanotechnologies all roads were altered so that they have the same length. For the same reason moving using regular cars is not very popular in the country, and it can happen that there are such pairs of cities, one of which generally can not be reached by car from the other one. However, Cicasso is very conservative and cannot travel without the car. Choose cities so that the sculptor can make the tour using only the automobile. It is guaranteed that it is always possible to do.

    Input

    In the first line there is a pair of integers n and m (4 ≤ n ≤ 3000, 3 ≤ m ≤ 5000) — a number of cities and one-way roads in Berland.

    Each of the next m lines contains a pair of integers ui, vi (1 ≤ ui, vi ≤ n) — a one-way road from the city ui to the city vi. Note that uiand vi are not required to be distinct. Moreover, it can be several one-way roads between the same pair of cities.

    Output

    Print four integers — numbers of cities which Cicasso will visit according to optimal choice of the route. Numbers of cities should be printed in the order that Cicasso will visit them. If there are multiple solutions, print any of them.

    Example
    input
    8 9
    1 2
    2 3
    3 4
    4 1
    4 5
    5 6
    6 7
    7 8
    8 5
    output
    2 1 8 7
    Note

    Let d(x, y) be the shortest distance between cities x and y. Then in the example d(2, 1) = 3, d(1, 8) = 7, d(8, 7) = 3. The total distance equals 13.

    题意:

      给你一个有向图,有环,让你找出四个不同点,使得d[a,b]+d[b,c]+d[c,d]之值最大

    题解:

      

    #include<bits/stdc++.h>
    using namespace std;
    const int N = 3e3+10, M = 1e6+10, mod = 1e9+7, inf = 1e9+1000;
    typedef long long ll;
    
    int dist[N][N], vis[N], a , b ,n , m ,a1,a2,a3,a4;
    vector<int > G[N];
    vector<pair<int,int > > fd[N],d[N];
    void add(int x,int y) {G[x].push_back(y);}
    int main() {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++) scanf("%d%d",&a,&b), add(a,b);
        for(int i=1;i<=n;i++) {
            for(int j=1;j<=n;j++) dist[i][j] = inf, vis[j] = 0;
            queue<int >q;
            q.push(i);
            vis[i] = 1;
            dist[i][i] = 0;
            while(!q.empty()) {
                int k = q.front();
                q.pop();
                vis[k] = 0;
                for(int j=0;j<G[k].size();j++) {
                    int to  = G[k][j];
                    if(dist[i][to]>dist[i][k] + 1) {
                        dist[i][to]  = dist[i][k] + 1;
                        if(!vis[to]) {
                            q.push(to);
                            vis[to] = 1;
                        }
                    }
                }
            }
            for(int j=1;j<=n;j++) {
                if(dist[i][j]>=inf) continue;
                d[i].push_back(make_pair(dist[i][j],j));
                fd[j].push_back(make_pair(dist[i][j],i));
            }
            sort(d[i].begin(),d[i].end());
        }
    
        for(int i=1;i<=n;i++) sort(fd[i].begin(),fd[i].end());
        int sum = 0,ans1,ans2,ans3,ans4;
        for(int i=1;i<=n;i++) {
            for(int j=1;j<=n;j++) {
                if(i == j) continue;
                ans2 = i;
                ans3 = j;
                int tmp = dist[ans2][ans3];//cout<<1<<endl;
                if(tmp>=inf) continue;
                int siz = d[ans3].size();
                if(siz==0) continue;
                if(d[ans3][siz-1].second == ans2||d[ans3][siz-1].second==ans3) {
                    if(siz==1) continue;
                    ans4 = d[ans3][siz-2].second;
                    tmp  +=  d[ans3][siz-2].first;
                }
                else {
                    ans4 = d[ans3][siz-1].second;
                    tmp += d[ans3][siz-1].first;
                }
                int f = 0;
                siz = fd[ans2].size();
                for(int k=siz-1;k>=0;k--) {
                    int now = fd[ans2][k].second;
                    int value = fd[ans2][k].first;
                    if(ans4!=now&&ans2!=now&&ans3!=now) {
                        ans1 = now;
                        tmp += value;f = 1;
                        break;
                    }
                }
    
                if(tmp>sum&&f) {
                    a1 = ans1;
                    a2 = ans2;
                    a3 = ans3;
                    a4 = ans4;
                    sum = tmp;
                }
            }
        }
       // cout<<dist[2][1]<<" "<<dist[1][8]<<" "<<dist[8][7]<<endl;
       // cout<<dist[a1][a2]<<" "<<dist[a2][a3]<<" "<<dist[a3][a4]<<endl;
       // cout<<sum<<endl;
        cout<<a1<<" "<<a2<<" "<<a3<<" "<<a4<<endl;
        return 0;
    }
  • 相关阅读:
    只有程序员才懂这些黑色幽默!
    只有程序员才懂这些黑色幽默!
    程序员常访问的国外技术交流网站
    回归分析:非线性nlinfi
    Java设计模式(二十一):职责链模式
    Angular4——7.表单处理
    ubuntu 代理设置
    Qt 隐藏标题栏 窗口移动 鼠标事件
    Shevon's Blog
    Allenmind's Blog
  • 原文地址:https://www.cnblogs.com/zxhl/p/5452196.html
Copyright © 2011-2022 走看看