whu 1124 Football Coach(最大流,判断满流)

来源:互联网 发布:java的输入流和输出流 编辑:程序博客网 时间:2024/05/20 17:07

Problem 1124 - Football Coach

Time Limit: 2000MS       Memory Limit: 65536KB      
Total Submit: 239      Accepted: 100      Special Judge: No  
Description
It is not an easy job to be a coach of a football team. The season is almost over, only a few matches are left to play. All of sudden the team 
manager comes to you and tells you bad news: the main sponsor of your club is not happy with your results and decided to stop sponsoring your 
team, which probably means the end of your club. The sponsor's decision is final and there is no way to change it unless... unless your team 
miraculously wins the league.
The manager left you in deep thought. If you increase the number of practices and offer players a generous bonus for each match, you may be 
able to win all the remaining matches. Is that enough? You also have to make sure that teams with many points lose against teams with few 
points so that in the end, your team will have more points than any other team. You know some of the referees and can bribe them to manipulate 
the result of each match. But first you need to figure out how to manipulate the results and whether it is possible at all.
There are N teams numbered 1 through N, your team has the number N. The current number of points of each team and the list of remaining 
matches are given. Your task is to find out whether it is possible to manipulate each remaining match so that the team N will finish with 
strictly more points than any other team. If it is possible, output "YES", otherwise, output "NO". In every match, the winning team gets 2 
points, the losing team gets 0. If the match ends with a draw, both teams get 1 point.
Input
There will be multiple test cases. Each test case has the following form: The first line contains two numbers N(1 <= N <= 100) and M(0 <= M <=
1000). The next line contains N numbers separated by spaces giving the current number of points of teams 1, 2, ..., N respectively. The
following M lines describe the remaining matches. Each line corresponds to one match and contains two numbers a and b (a not equal to b, 1 <=
a,b <= N) identifying the teams that will play in the given match. There is a blank line after each test case.
Output
For each test case, output "YES" or "NO" to denote whether it's possible to manipulate the remaining matches so that the team N would win
the league.
Sample Input
5 8
2 1 0 0 1
1 2
3 4
2 3
4 5
3 1
2 4
1 4
3 5
5 4
4 4 1 0 3
1 3
2 3
3 4
4 5
Sample Output
YES
NO
 
题意:有n个足球队,剩余m场比赛。给出每个队现有的分数,再给出m场比赛中的队伍。每场比赛,胜出者得2分,败者得0分,或打平时各得一分。你可以操纵全部m场比赛的结果,问最后是否能使队伍n的分数最高。
思路:首先,有队伍n的比赛,都使队伍n赢,累加n能得到的分数,设最后n的分数最多为sum。那么m场比赛前如果有足球队现有的分数超过sum - 1,显然不能使队伍n分数最高,如果不存在,那么就可以构图了。将事件看成点,即将每场比赛看成点,源点向每场比赛连边,容量为2,每场比赛向相应的队伍连边,容量都为2,然后每个队伍向汇点连边,容量为sum - 1 - 该队伍现有的分数。最后如果最大流是满流的,则可行。
 
AC代码:
#include <iostream>#include <cmath>#include <cstdlib>#include <cstring>#include <cstdio>#include <queue>#include <ctime>#include <vector>#include <algorithm>#define ll long long#define L(rt) (rt<<1)#define R(rt)  (rt<<1|1)#define eps 1e-6using namespace std;const int INF = 1e8;const int maxn = 2005;struct Edge{    int u, v, cap, flow, next;}et[maxn * maxn];int low[maxn], cnt[maxn], dis[maxn], pre[maxn], cur[maxn], eh[maxn];int p[maxn];int n, m, s, t, num;void init(){    memset(eh, -1, sizeof(eh));    num = 0;}void add(int u, int v, int cap, int flow){    Edge e = {u, v, cap, flow, eh[u]};    et[num] = e;    eh[u] = num++;}void addedge(int u, int v, int cap){    add(u, v, cap, 0);    add(v, u, 0, 0);}int isap(int s, int t, int nv){    int u, v, now, flow = 0;    memset(low, 0, sizeof(low));    memset(cnt, 0, sizeof(cnt));    memset(dis, 0, sizeof(dis));    for(u = 0; u <= nv; u++) cur[u] = eh[u];    low[s] = INF, cnt[0] = nv, u = s;    while(dis[s] < nv)    {        for(now = cur[u]; now != -1; now = et[now].next)        if(et[now].cap - et[now].flow && dis[u] == dis[v = et[now].v] + 1) break;        if(now != -1)        {            cur[u] = pre[v] = now;            low[v] = min(low[u], et[now].cap - et[now].flow);            u = v;            if(u == t)            {                for(; u != s; u = et[pre[u]].u)                {                    et[pre[u]].flow += low[t];                    et[pre[u]^1].flow -= low[t];                }                flow += low[t];                low[s] = INF;            }        }        else        {            if(--cnt[dis[u]] == 0) break;            dis[u] = nv, cur[u] = eh[u];            for(now = eh[u]; now != -1; now = et[now].next)            if(et[now].cap - et[now].flow && dis[u] > dis[et[now].v] + 1)            dis[u] = dis[et[now].v] + 1;            cnt[dis[u]]++;            if(u != s) u = et[pre[u]].u;        }    }    return flow;}int main(){    int a, b;    while(~scanf("%d%d", &n, &m))    {        init();        s = 0;        t = n + m + 1;        for(int i = 1; i <= n; i++)        scanf("%d", &p[i]);        int sum = p[n], sumflow = 0;        for(int i = 1; i <= m; i++)        {            scanf("%d%d", &a, &b);            if(a == n || b == n) sum += 2;            else            {                sumflow += 2;                addedge(s, i, 2);                addedge(i, a + m, 2);                addedge(i, b + m, 2);            }        }        bool flag = true;        for(int i = 1; i <= n; i++)        if(p[i] >= sum)        {            flag = false;            break;        }        if(!flag)        {            printf("NO\n");            continue;        }        for(int i = 1; i < n; i++)            addedge(i + m, t, sum - 1 - p[i]);        if(sumflow == isap(s, t, t + 1)) printf("YES\n");        else printf("NO\n");    }    return 0;}

0 0