Codeforces #349 div1 B. World Tour 模拟 技巧

题目

题目链接:http://codeforces.com/contest/666/problem/B

题目来源:Codeforces #349 div1

题解

非常神奇的一道题,要利用一些结论然后用比较神奇的方法来储存利用数据。

首先要预处理出任意两点之间的距离,Floyd时间还不够, n 次bfs即可。

假如固定中间两点则两边都只需要取距离最大的三个点即可。

因为最后一点不会和与之相连的相同,而剩下最多与两个点发生冲突,三点即可。

求出以某点为终点最大的三条最短路的起点,一某点为起点最大的三条最短路的终点。

固定中间两点枚举两边3×3,总体复杂度为 Θ(n2)

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
// head
const int N = 3e3+5;
const int M = 5e3+5;
const int INF = -1;

struct Gragh {
    struct Edge {
        int to, nxt;
        Edge(int to, int nxt) : to(to), nxt(nxt) {}
        Edge() {}
    };

    int dis[N][N], head[N], fw[N][3], bk[N][3], ec;
    Edge e[M];

    void addEdge(int from, int to) {
        e[ec] = Edge(to, head[from]);
        head[from] = ec++;
    }
    void init(int n) {
        ec = 0;
        for (int i = 0; i <= n; i++) head[i] = -1;
        memset(dis, INF, sizeof dis);
    }
    void bfs(int st) {
        queue<int> q;
        dis[st][st] = 0;
        q.push(st);
        while (!q.empty()) {
            int cur = q.front();
            q.pop();
            for (int i = head[cur]; ~i; i = e[i].nxt) {
                int to = e[i].to;
                if (dis[st][to] == INF) {
                    dis[st][to] = dis[st][cur] + 1;
                    q.push(to);
                }
            }
        }
    }


    void solve(int n) {
        for (int i = 0; i < n; i++) {
            bfs(i);
        }
        memset(fw, INF, sizeof fw);
        memset(bk, INF, sizeof bk);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (i == j || dis[i][j] == INF) continue;
                int cur = j;
                for (int k = 0; k < 3; k++) {
                    if (fw[i][k] == INF || dis[i][cur] > dis[i][fw[i][k]]) {
                        swap(cur, fw[i][k]);
                    }
                }
            }
            for (int j = 0; j < n; j++) {
                if (i == j || dis[j][i] == INF) continue;
                int cur = j;
                for (int k = 0; k < 3; k++) {
                    if (bk[i][k] == INF || dis[cur][i] > dis[bk[i][k]][i]) {
                        swap(cur, bk[i][k]);
                    }
                }
            }
        }
        int ans = -1, res[4];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (i == j || dis[i][j] == INF) continue;
                for (int ii = 0; ii < 3; ii++) {
                    int k = fw[j][ii];
                    if (k == INF || dis[j][k] == INF) continue;
                    if (k == i || k == j) continue;
                    for (int jj = 0; jj < 3; jj++) {
                        int l = bk[i][jj];
                        if (l == INF || dis[l][i] == INF) continue;
                        if (l == i || l == j || l == k) continue;
                        int sum = dis[l][i] + dis[i][j] + dis[j][k];
                        if (ans < sum) {
                            ans = sum;
                            res[0] = l, res[1] = i, res[2] = j, res[3] = k;
                        }
                    }
                }
            }
        }
        for (int i = 0; i < 4; i++) {
            printf("%d%c", res[i]+1, i==3 ? '\n' : ' ');
        }
    }
};

Gragh g;
int main() {
    int n, m, u, v;
    scanf("%d%d", &n, &m);
    g.init(n);
    for (int i = 0; i < m; i++) {
        scanf("%d%d", &u, &v);
        g.addEdge(u-1, v-1);
    }
    g.solve(n);
    return 0;
}
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页