Codeforces 667D World Tour (最短路+枚举)

题意

另dis[u][v]表示u到v的最短路,求最长的dis[i][j]+dis[j][k]+dis[k][l],输出i, j, k, l。

思路

先n*nlogn预处理出来所有点的最短路,然后给所有的dis[i][j]排个序保存起来,给所有的dis[k][l]排个序保存起来,然后就可以枚举所有的j和k,从itoj找最大的4条,ktol找最大的4条加起来就行了,找4条是因为为了防止这4个点出现重合比如j找到k,k找到i什么的,不过题目本身没什么坑点,基本上思路对就能过了。

代码

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define LL long long
#define lowbit(x) ((x)&(-x))
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1|1
#define MP(a, b) make_pair(a, b)
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
const int maxn = 1e5 + 10;
const double eps = 1e-8;
const double PI = acos(-1.0);
typedef pair<int, int> pii;
struct node
{
    int d, to;
};
bool cmp(node a, node b)
{
    return a.d > b.d;
}
vector<int> G[3030];
vector<node> itoj[3030], ktol[3030];
int dis[3030][3030];
int vis[3030];
int n, m;

void spfa(int src)
{
    for (int i = 1; i <= n; i++)
        dis[src][i] = INF;
    memset(vis, 0, sizeof(vis));
    queue<int> q;
    q.push(src);
    vis[src] = 1;
    dis[src][src] = 0;
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        for (int i = 0; i < G[u].size(); i++)
        {
            int v = G[u][i];
            if (dis[src][v] > dis[src][u] + 1)
            {
                dis[src][v] = dis[src][u] + 1;
                if (!vis[v])
                {
                    vis[v] = 1;
                    q.push(v);
                }
            }
        }
    }
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    while (cin >> n >> m)
    {
        for (int i = 1; i <= n; i++)
            G[i].clear();
        for (int i = 0; i < m; i++)
        {
            int u, v;
            cin >> u >> v;
            G[u].push_back(v);
        }
        for (int i = 1; i <= n; i++)
            spfa(i);
        for (int i = 1; i <= n; i++)
            itoj[i].clear(), ktol[i].clear();
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
            {
                if (i == j) continue;
                if (dis[i][j] != INF)
                {
                    itoj[j].push_back((node){dis[i][j], i});
                    ktol[i].push_back((node){dis[i][j], j});
                }
            }
        for (int i = 1; i <= n; i++)
        {
            sort(itoj[i].begin(), itoj[i].end(), cmp);
            sort(ktol[i].begin(), ktol[i].end(), cmp);
        }
        int ans1, ans2, ans3, ans4;
        int mmax = -1;
        for (int j = 1; j <= n; j++)
            for (int k = 1; k <= n; k++)
            {
                if (j == k || dis[j][k] == INF) continue;
                for (int i = 0; i < 4 && i < itoj[j].size(); i++)
                {
                    if (itoj[j][i].to == k) continue;
                    for (int l = 0; l < 4 && l < ktol[k].size(); l++)
                    {
                        if (ktol[k][l].to == itoj[j][i].to || ktol[k][l].to == j) continue;
                        int dd = itoj[j][i].d + ktol[k][l].d + dis[j][k];
                        if (dd > mmax)
                        {
                            mmax = dd;
                            ans1 = itoj[j][i].to;
                            ans2 = j;
                            ans3 = k;
                            ans4 = ktol[k][l].to;
                        }
                    }
                }
            }
        cout << ans1 << " " << ans2 << " " << ans3 << " " << ans4 << endl;
    }
    return 0;
}

你可能感兴趣的:(codeforces)