标签: 图论

[CF666B]World Tour 题解

[CF666B]World Tour 题解

题目地址:Codeforces:Problem – 666B – Codeforces、洛谷:【CF666B】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: 1., 2, 3, 4, 5., 4 ,3, 2., 3, 4.. 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.
一张n个点m条边的有向图,每条边的权值相同.你要找4个点a,b,c,d,使得a->b->c->d的最短路最长(a,b,c,d之间要有路),输出一组解.

输入输出格式

输入格式:
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 ui and vi are not required to be distinct. Moreover, it can be several one-way roads between the same pair of cities.

输出格式:
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.

输入输出样例

输入样例#1:

8 9
1 2
2 3
3 4
4 1
4 5
5 6
6 7
7 8
8 5

输出样例#1:

2 1 8 7

题解

既然是4个点,我们就考虑枚举的方法来做。枚举4个点复杂度太大,那么我们只枚举中间两个点,然后再处理出从其中一个点出发最远能到达的点以及从一个点出发最远能到这个点的点。但是万一前面的两个最远会与之前已经枚举了的点重复了怎么办?那我们考虑记下最远、次远、第三远三个点,这样总不会重了吧。因为A→B→C→D如果ABC的枚举已经完成,D只可能跟AB重,也就是说,最多可以和2个点重复,那么需要记下的点最少就是三个。
说白了就是个大力BFS大力枚举。

代码

// Code by KSkun, 2018/3
#include <cstdio>
#include <cstring>

#include <vector>
#include <queue>

typedef long long LL;

inline char fgc() {
    static char buf[100000], *p1 = buf, *p2 = buf;
    return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++;
}

inline LL readint() {
    register LL res = 0, neg = 1;
    char c = fgc();
    while(c < '0' || c > '9') {
        if(c == '-') neg = -1;
        c = fgc();
    }
    while(c >= '0' && c <= '9') {
        res = res * 10 + c - '0';
        c = fgc();
    }
    return res * neg;
}

const int MAXN = 3005;

std::vector<int> gra[MAXN];
std::queue<int> que;
int n, m, ut, vt, dis[MAXN][MAXN], lf[MAXN][3], lt[MAXN][3];
bool vis[MAXN];

inline void updatef(int u, int v) {
    if(dis[u][v] == 0) return;
    if(dis[u][v] > dis[u][lf[u][0]]) {
        lf[u][2] = lf[u][1];
        lf[u][1] = lf[u][0];
        lf[u][0] = v;
    } else if(dis[u][v] > dis[u][lf[u][1]]) {
        lf[u][2] = lf[u][1];
        lf[u][1] = v;
    } else if(dis[u][v] > dis[u][lf[u][2]]) {
        lf[u][2] = v;
    }
}

inline void updatet(int u, int v) {
    if(dis[u][v] == 0) return;
    if(dis[u][v] > dis[lt[v][0]][v]) {
        lt[v][2] = lt[v][1];
        lt[v][1] = lt[v][0];
        lt[v][0] = u;
    } else if(dis[u][v] > dis[lt[v][1]][v]) {
        lt[v][2] = lt[v][1];
        lt[v][1] = u;
    } else if(dis[u][v] > dis[lt[v][2]][v]) {
        lt[v][2] = u;
    }
}

inline void bfs(int st) {
    memset(vis, 0, sizeof vis);
    dis[st][st] = 0;
    que.push(st);
    vis[st] = true;
    while(!que.empty()) {
        int u = que.front();
        que.pop();
        for(int v : gra[u]) {
            if(!vis[v]) {
                dis[st][v] = dis[st][u] + 1;
                vis[v] = true;
                updatef(st, v);
                updatet(st, v);
                que.push(v);
            }
        }
    }
}

inline bool check(int a, int b, int c, int d) {
    return a != b && a != c && a != d && b != c && b != d && c != d && a != 0 && d != 0;
}

int main() {
    n = readint();
    m = readint();
    for(int i = 1; i <= m; i++) {
        ut = readint();
        vt = readint();
        gra[ut].push_back(vt);
    }
    for(int i = 1; i <= n; i++) bfs(i);
    int ans = 0, ansc[4];
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            if(i == j || dis[i][j] == 0) continue;
            for(int k1 = 0; k1 < 3; k1++) {
                for(int k2 = 0; k2 < 3; k2++) {
                    if(!check(lt[i][k1], i, j, lf[j][k2])) continue;
                    if(dis[lt[i][k1]][i] + dis[i][j] + dis[j][lf[j][k2]] > ans) {
                        ans = dis[lt[i][k1]][i] + dis[i][j] + dis[j][lf[j][k2]];
                        ansc[0] = lt[i][k1];
                        ansc[1] = i;
                        ansc[2] = j;
                        ansc[3] = lf[j][k2];
                    }
                }
            }
        }
    }
    for(int i = 0; i < 4; i++) {
        printf("%d ", ansc[i]);
    }
    return 0;
}
二分图匹配的一种算法:匈牙利算法

二分图匹配的一种算法:匈牙利算法

这里不介绍算法,只提供模板代码。所有代码基于【P3386】【模板】二分图匹配 – 洛谷一题。

匈牙利算法

// Code by KSkun, 2017/12
#include <cstdio>
#include <cstring>
#include <vector>

struct io {
    char buf[1 << 26], *s;

    io() {
        fread(s = buf, 1, 1 << 26, stdin);
    }

    inline int read() {
        register int res = 0;
        while(*s < '0' || *s > '9') s++;
        while(*s >= '0' && *s <= '9') res = res * 10 + *s++ - '0';
        return res;
    }
};

io ip;

#define read ip.read 

std::vector<int> vec[2005]; 

int n, m, e;
bool vis[2005];
int match[2005];

inline bool dfs(int u) {
    for(int i = 0; i < vec[u].size(); i++) {
        int v = vec[u][i];
        if(!vis[v]) {
            vis[v] = true;
            if(match[v] == -1 || dfs(match[v])) {
                match[v] = u;
                match[u] = v;
                return true;
            }
        }
    }
    return false;
} 

inline int hungarian() {
    int ans = 0;
    memset(match, -1, sizeof match);
    for(int i = 1; i <= n; i++) {
        if(match[i] == -1) {
            memset(vis, 0, sizeof vis);
            if(dfs(i)) ans++;
        }
    }
    return ans;
}

int ut, vt;

int main() {
    n = read();
    m = read();
    e = read();
    for(int i = 0; i < e; i++) {
        ut = read();
        vt = read();
        if(ut > n || vt > m) continue;
        vec[ut].push_back(n + vt);
        vec[n + vt].push_back(ut);
    }
    printf("%d", hungarian());
    return 0;
}
最大流的三种算法:Ford-Fulkson、Edmons-Karp、Dinic

最大流的三种算法:Ford-Fulkson、Edmons-Karp、Dinic

这里不介绍算法,只提供模板代码。所有代码基于【P3376】【模板】网络最大流 – 洛谷一题。

Ford-Fulkson

// Code by KSkun, 2017/11
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>

struct io {
    char buf[1 << 26], *s;

    io() {
        fread(s = buf, 1, 1 << 26, stdin);
    }

    inline int read() {
        register int res = 0;
        while(*s < '0' || *s > '9') s++;
        while(*s >= '0' && *s <= '9') res = res * 10 + *s++ - '0';
        return res;
    }
} ip;

#define read ip.read

const int MAXN = 100005;
const int INF = 1e9;

struct Edge {
    int to, cap, rev;
    Edge(int to, int cap, int rev): to(to), cap(cap), rev(rev) {}
};

std::vector<Edge> vec[MAXN];
bool vis[MAXN];

inline void addedge(int u, int v, int cap) {
    vec[u].push_back(Edge(v, cap, vec[v].size()));
    vec[v].push_back(Edge(u, 0, vec[u].size() - 1));
}

// Ford-Fulkerson DFS
inline int dfs(int u, int t, int f) {
    if(u == t) return f;
    vis[u] = true;
    for(int i = 0; i < vec[u].size(); i++) {
        int v = vec[u][i].to;
        if(!vis[v] && vec[u][i].cap > 0) {
            int nxt = dfs(v, t, std::min(f, vec[u][i].cap));
            if(nxt > 0) {
                vec[u][i].cap -= nxt;
                vec[v][vec[u][i].rev].cap += nxt;
                return nxt;
            }
        } 
    }
    return 0;
}

inline int max_flow(int s, int t) {
    int flow = 0;
    for(;;) {
        memset(vis, 0, sizeof vis);
        int f = dfs(s, t, INF);
        if(f == 0) return flow;
        flow += f;
    }
}

int n, m, s, t, ut, vt, wt;

int main() {
    n = read();
    m = read();
    s = read();
    t = read();
    for(int i = 0; i < m; i++) {
        ut = read();
        vt = read();
        wt = read();
        addedge(ut, vt, wt);
    }
    printf("%d", max_flow(s, t));
    return 0;
}

Edmons-Karp

// Code by KSkun, 2017/11
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#include <utility> 
typedef std::pair<int, int> pairi;

struct io {
    char buf[1 << 26], *s;

    io() {
        fread(s = buf, 1, 1 << 26, stdin);
    }

    inline int read() {
        register int res = 0;
        while(*s < '0' || *s > '9') s++;
        while(*s >= '0' && *s <= '9') res = res * 10 + *s++ - '0';
        return res;
    }
} ip;

#define read ip.read

const int MAXN = 100005;
const int INF = 1e9;

struct Edge {
    int to, cap, rev;
    Edge(int to, int cap, int rev): to(to), cap(cap), rev(rev) {}
};

std::vector<Edge> vec[MAXN];
std::queue<int> que;
int f[MAXN];
pairi pre[MAXN];

inline void addedge(int u, int v, int cap) {
    vec[u].push_back(Edge(v, cap, vec[v].size()));
    vec[v].push_back(Edge(u, 0, vec[u].size() - 1));
}

// Edmons-Karp BFS

inline int max_flow(int s, int t) {
    int flow = 0;
    for(;;) {
        memset(f, 0, sizeof f);
        while(!que.empty()) que.pop();
        que.push(s);
        f[s] = INF;
        while(!que.empty()) {
            int u = que.front();
            que.pop();
            for(int i = 0; i < vec[u].size(); i++) {
                int v = vec[u][i].to;
                if(f[v] == 0 && vec[u][i].cap > 0) {
                    pre[v] = std::make_pair(u, i);
                    f[v] = std::min(f[u], vec[u][i].cap);
                    que.push(v);
                }
            }
            if(f[t] != 0) break;
        }
        if(f[t] == 0) break;
        for(int u = t; u != s; u = pre[u].first) {
            vec[pre[u].first][pre[u].second].cap -= f[t];
            vec[u][vec[pre[u].first][pre[u].second].rev].cap += f[t];
        }
        flow += f[t];
    }
    return flow;
}

int n, m, s, t, ut, vt, wt;

int main() {
    n = read();
    m = read();
    s = read();
    t = read();
    for(int i = 0; i < m; i++) {
        ut = read();
        vt = read();
        wt = read();
        addedge(ut, vt, wt);
    }
    printf("%d", max_flow(s, t));
    return 0;
}

Dinic

// Code by KSkun, 2017/11
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>

struct io {
    char buf[1 << 26], *s;

    io() {
        fread(s = buf, 1, 1 << 26, stdin);
    }

    inline int read() {
        register int res = 0;
        while(*s < '0' || *s > '9') s++;
        while(*s >= '0' && *s <= '9') res = res * 10 + *s++ - '0';
        return res;
    }
} ip;

#define read ip.read

const int MAXN = 100005;
const int INF = 1e9;

struct Edge {
    int to, cap, rev;
    Edge(int to, int cap, int rev): to(to), cap(cap), rev(rev) {}
};

std::vector<Edge> vec[MAXN];
std::queue<int> que;
int level[MAXN];

inline void addedge(int u, int v, int cap) {
    vec[u].push_back(Edge(v, cap, vec[v].size()));
    vec[v].push_back(Edge(u, 0, vec[u].size() - 1));
}

// Dinic 

inline bool bfs(int s, int t) {
    memset(level, -1, sizeof level);
    level[s] = 0;
    que.push(s);
    while(!que.empty()) {
        int u = que.front();
        que.pop();
        for(int i = 0; i < vec[u].size(); i++) {
            int v = vec[u][i].to;
            if(level[v] == -1 && vec[u][i].cap > 0) {
                level[v] = level[u] + 1;
                que.push(v);
            }
        } 
    }
    return level[t] != -1;
}

inline int dfs(int u, int t, int left) {
    if(u == t || left == 0) return left;
    int flow = 0;
    for(int i = 0; i < vec[u].size(); i++) {
        int v = vec[u][i].to;
        if(vec[u][i].cap > 0 && level[v] == level[u] + 1) {
            int d = dfs(v, t, std::min(left, vec[u][i].cap));
            if(d > 0) {
                vec[u][i].cap -= d;
                vec[v][vec[u][i].rev].cap += d;
                flow += d;
                left -= d;
                if(left == 0) break;
            }
        }
    }
    return flow;
}

inline int max_flow(int s, int t) {
    int flow = 0;
    while(bfs(s, t)) {
        flow += dfs(s, t, INF);
    }
    return flow;
}

int n, m, s, t, ut, vt, wt;

int main() {
    n = read();
    m = read();
    s = read();
    t = read();
    for(int i = 0; i < m; i++) {
        ut = read();
        vt = read();
        wt = read();
        addedge(ut, vt, wt);
    }
    printf("%d", max_flow(s, t));
    return 0;
}