码迷,mamicode.com
首页 > 其他好文 > 详细

UVALive - 2957 Bring Them There(最大流 图论建模)

时间:2015-08-31 01:12:56      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:

题目大意:有n个星球,你的任务是用最短的时间把k个超级计算机从S星球运送到T星球。每个超级计算机需要一艘飞船来运输,行星之间有m条双向隧道,每条隧道需要一天时间来通过,且不能有两艘飞船同时使用同一条隧道,隧道不会连接两个相同的行星,且每一对行星之间最多只有一条隧道

解题思路:按照大白书上的思路是拆点
比如运送的时间为T,那么就把每个点u拆成T + 1个,分别为u0, u1 … uT,分别对应的是第i天的u星球
对于所给的每条边(u,v),假设是第i天,就连边ui –>vi+1和vi–>ui+1,两条边的容量都为1
还得添加一个点ui—>ui+1,容量为INF,表示飞船该天没有前行
计算过程的话,就逐步扩大图,一天一天的枚举,一天一天的扩大,直到流量为k为止
这里的话得注意一点,在某时刻a–>bi+1和bi–>ai+1同时有流量是不允许的,这里的话,如果出现了两个方向都有流量了,那我们就相当于把这两个飞船分别停留在a星球和b星球上各一天,这样就不会矛盾了

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

#define M 1000010
#define N 10010 
#define INF 0x3f3f3f3f
struct Edge{
    int u, v, cap, flow, next;
}E[M];

struct Dinic{
    int head[N], d[N];
    int tot, sink, source;

    void init() {
        memset(head, -1, sizeof(head));
        tot = 0;
    }

    inline void AddEdge(int u, int v, int cap) {
        E[tot].u = u; E[tot].v = v; E[tot].cap = cap; E[tot].flow = 0; E[tot].next = head[u]; head[u] = tot++;
        u = u ^ v; v = u ^ v; u = u ^ v;
        E[tot].u = u; E[tot].v = v; E[tot].cap = 0; E[tot].flow = 0; E[tot].next = head[u]; head[u] = tot++;
    }

    inline bool bfs(int s) {
        int u, v;
        memset(d, 0, sizeof(d));
        queue<int> Q;
        Q.push(s);
        d[s] = 1;
        while (!Q.empty()) {
            u = Q.front(); Q.pop();
            if (u == sink) return true;
            for (int i = head[u]; ~i; i = E[i].next) {
                v = E[i].v;
                if (!d[v] && E[i].cap - E[i].flow > 0) {
                    d[v] = d[u] + 1;
                    Q.push(v);
                }
            }
        }
        return false;
    }

    int dfs(int x, int a) {
        if (x == sink || a == 0)
            return a;
        int f, flow = 0;
        for (int i = head[x]; ~i; i = E[i].next) {
            int v = E[i].v;
            if (d[v] == d[x] + 1 && E[i].cap - E[i].flow > 0) {
                f = dfs(v, min(a, E[i].cap - E[i].flow));
                E[i].flow += f;
                E[i^1].flow -= f;
                flow += f;
                a -= f;
                if (!a) break;
            }
        }
        if (flow == 0) d[x] = 0;
        return flow;
    }

    int Maxflow(int source, int sink, int need) {
        int flow = 0;
        this->sink = sink;
        while (bfs(source)) {
            flow += dfs(source, need - flow);
            if (flow >= need) return flow;
        }
        return flow;
    }
};
Dinic dinic;

struct Node {
    int x, y;
}node[N];
int n, m, k, s, t;
int pos[N];
bool vis[N];

void print(int cnt) {
    for (int i = 1; i <= k; i++) pos[i] = s;

    printf("%d\n", cnt);
    for (int i = 1; i <= cnt; i++) {
        vector<int> u, v;
        memset(vis, 0, sizeof(vis));
        for (int j = 0; j < 4 * m; j += 4) {
            int p = j + (i - 1) * 4 * m + n * i * 2;
            //正向有流量,反向没流量,也就是u-->v
            if (E[p].flow && !E[p+2].flow) {
                u.push_back(E[p ^ 1].v - (i - 1) * n);
                v.push_back(E[p].v - i * n);
            }//反向有流量,正向没流量,也就是v-->u,这样的话就排除了两边都有流量的情况,相当于两点交换了,也就是两艘飞船待在原地了
            else if (!E[p].flow && E[p + 2].flow) {
                u.push_back(E[(p + 2) ^ 1].v - (i - 1) * n);
                v.push_back(E[p + 2].v - i * n);
            }
        }
        printf("%d", u.size());
        for (int p = 0; p < u.size(); p++)
            for (int j = 1; j <= k; j++) {
                if (!vis[j] && pos[j] == u[p]) {
                    vis[j] = 1;
                    printf(" %d %d", j, v[p]);
                    pos[j] = v[p];
                    break;
                }
            }
        printf("\n");
    }
}

void solve() {
    for (int i = 0; i < m; i++) scanf("%d%d", &node[i].x, &node[i].y);
    int cnt = 0, Maxflow = 0, sink = t;
    dinic.init();
    while (Maxflow < k) {
        ++cnt;
        //待在原地
        for (int i = 1; i <= n; i++) dinic.AddEdge(i + (cnt - 1) * n,  i + cnt * n, INF);
        //边
        for (int i = 0; i < m; i++) {
            dinic.AddEdge(node[i].x + (cnt - 1) * n, node[i].y + cnt * n, 1);
            dinic.AddEdge(node[i].y + (cnt - 1) * n, node[i].x + cnt * n, 1);
        }
        sink += n;
        Maxflow += dinic.Maxflow(s, sink, k - Maxflow);
    }
    print(cnt);
}

int main() {
    while (scanf("%d%d%d%d%d", &n, &m, &k, &s, &t) != EOF) solve();
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

UVALive - 2957 Bring Them There(最大流 图论建模)

标签:

原文地址:http://blog.csdn.net/l123012013048/article/details/48117755

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!