[Codeforces 666B] World Tour

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Codeforces 666B] World Tour相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/its' title='its'>its   res   wap   澶嶆潅   info   getchar   inline   int   namespace   

[棰樼洰閾炬帴]

         https://codeforces.com/contest/666/problem/B

[绠楁硶]

       棣栧厛 锛?鐢˙FS姹傚嚭浠绘剰涓ょ偣鐨勬渶鐭矾寰?/p>

       鐒跺悗 锛?鎴戜滑鐢╢[i][0-2]琛ㄧず浠巌鍑哄彂鍒拌揪鐨勬渶杩滀笁鐐?锛?g[i][0-2]琛ㄧず鍒癷璺濈鏈€杩滅殑涓変釜鐐?/p>

       鏋氫妇b鍜宑 锛?鐒跺悗鍦ㄦ灇涓? * 3涓偣瀵?锛?浠庝腑閫夊嚭鏈€浼樼殑a鍜宒鍗冲彲

       鏃堕棿澶嶆潅搴?锛?O(N^2)

[浠g爜]

         

#include<bits/stdc++.h>
using namespace std;
#define MAXN 3010
#define MAXM 5010
const int inf = 2e9;

int tot , n , m;
int head[MAXN];
int f[MAXN][3],g[MAXN][3],dist[MAXN][MAXN];

struct info
{
        int a , b , c , d;
} res;
struct edge
{
        int to , nxt;
} e[MAXM];

template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
    T f = 1; x = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == 鈥?/span>-鈥?/span>) f = -f;
    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - 鈥?/span>0鈥?/span>;
    x *= f;
}
inline void addedge(int u,int v)
{
        tot++;
        e[tot] = (edge){v,head[u]};
        head[u] = tot;
}

int main()
{
        
        read(n); read(m);
        for (int i = 1; i <= m; i++)
        {
                int u , v;
                read(u); read(v);
                addedge(u,v);
        }
        for (int i = 1; i <= n; i++)
        {
                for (int j = 1; j <= n; j++) dist[i][j] = inf;
                queue< int > q;
                q.push(i);        
                dist[i][i] = 0;
                while (!q.empty())
                {
                        int cur = q.front();
                        q.pop();
                        for (int j = head[cur]; j; j = e[j].nxt)
                        {
                                int v = e[j].to;
                                if (dist[i][cur] + 1 < dist[i][v])
                                {
                                        dist[i][v] = dist[i][cur] + 1;
                                        q.push(v);
                                }
                        }
                }
        }
        for (int i = 1; i <= n; i++)
        {
                for (int j = 1; j <= n; j++)
                {
                        if (i == j || dist[i][j] == inf) continue;
                        int pos = j;
                        for (int k = 0; k < 3; k++)
                        {
                                if (f[i][k] == 0 || dist[i][pos] > dist[i][f[i][k]])
                                        swap(pos,f[i][k]);
                        }
                }
                for (int j = 1; j <= n; j++)
                {
                        if (i == j || dist[j][i] == inf) continue;
                        int pos = j;
                        for (int k = 0; k < 3; k++)
                        {
                                if (g[i][k] == 0 || dist[pos][i] > dist[g[i][k]][i])
                                        swap(pos,g[i][k]);
                        }
                }
        }
        int ans = 0;
        for (int b = 1; b <= n; b++)
        {
                for (int c = 1; c <= n; c++)
                {
                        if (b == c || dist[b][c] == inf) continue;
                        for (int t1 = 0; t1 < 3; t1++)
                        {
                                int a = g[b][t1];
                                if (a == 0 || a == c) continue;
                                for (int t2 = 0; t2 < 3; t2++)
                                {
                                        int d = f[c][t2];
                                        if (d == 0 || d == b || d == a) continue;
                                        if (dist[a][b] + dist[b][c] + dist[c][d] > ans)
                                        {
                                                ans = dist[a][b] + dist[b][c] + dist[c][d];
                                                res = (info){a,b,c,d};
                                        }
                                }    
                        }        
                }
        }
        printf("%d %d %d %d
",res.a,res.b,res.c,res.d);
        
        return 0;
    
}

 

以上是关于[Codeforces 666B] World Tour的主要内容,如果未能解决你的问题,请参考以下文章

[Codeforces 464D]World of Darkraft(期望DP)

CodeForces - 131C The World is a Theatre(组合数)

Codeforces 949DShake It! 动态规划

Codeforces 543 B. World Tour

CodeForces 990B Micro-World(思维STL)

Codeforces 849B Tell Your World (数学题)