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

UVALive - 2531 The K-League(最大流+枚举)

时间:2015-08-31 01:11:00      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:

题目大意:有n支队伍进行比赛,每支队伍需要打的比赛次数相同,每场比赛恰好有一支队伍胜,一支队伍败,给出每支队伍目前胜的场数和败的场数,以及每两支队伍还剩下的比赛场数,确定所有可能得冠军的队伍

解题思路:枚举每支队伍,然后让该队伍在接下来的所有比赛中都获胜。
建图的话,就比较简单了,源点连向每场比赛,容量为比赛次数
每场比赛连向比赛队伍,容量为比赛次数
接着每支队伍连向汇点,容量为枚举队伍的总胜场-该队伍的胜场

如果满流,表示该队伍可以得到冠军

#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 flow = 0;
        this->sink = sink;
        while (bfs(source)) flow += dfs(source, INF);
        return flow;
    }
};

Dinic dinic;
#define maxn 30
int n;
int win[maxn], fight[maxn][maxn], all, Max;
void init() {
    scanf("%d", &n);
    all = 0;
    Max = -INF;
    int t;

    for (int i = 1; i <= n; i++) {
        scanf("%d%d", &win[i], &t);
        Max = max(Max, win[i]);
        if (i == 1)
            all += win[i] + t;
    }

    for (int i = 1; i <= n; i++) 
        for (int j = 1; j <= n; j++) {
            scanf("%d", &fight[i][j]);
            if (i == 1)
                all += fight[i][j];
        }
}

void solve() {
    bool flag = false;
    int total, source = 0, sink =  N - 1, cnt;
    for (int i = 1; i <= n; i++) {
        total = win[i];
        for (int j = 1; j <= n; j++)
            total += fight[i][j];

        if (total < Max)
            continue;
        cnt = n + 1;
        dinic.init();
        int Sum = 0;
        for (int j = 1; j <= n; j++) {
            if (j != i) dinic.AddEdge(j, sink, total - win[j]);
            for (int k = j + 1; k <= n; k++) {
                if (j == i || k == i) 
                    continue;
                dinic.AddEdge(source, cnt, fight[j][k]);
                Sum += fight[j][k];
                dinic.AddEdge(cnt, j, fight[j][k]);
                dinic.AddEdge(cnt++, k, fight[j][k]);
            }
        }
        int MaxFlow = dinic.Maxflow(source, sink);
        if (MaxFlow == Sum) {
            if (flag)
                printf(" ");
            flag = true;
            printf("%d", i);
        }
    }
    printf("\n");
}

int main() {
    int test;
    scanf("%d", &test);
    while (test--) {
        init();
        solve();
    }
    return 0;
}

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

UVALive - 2531 The K-League(最大流+枚举)

标签:

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

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