zoukankan      html  css  js  c++  java
  • WHU 1124 Football Coach

    WHU_1124

        首先对于有N参加的比赛,肯定是赢了最好,暂时先不考虑其他的比赛,这是如果各队的积分有大于或等于第N队的,那么肯定是输出NO的。

        接下来考虑其他的比赛,对于任意队伍i而言,得分是不等等于或者超过score[N]的,因此可以将源点和i连一条容量为score[N]-score[i]-1的边,由于比赛的总积分是2,自然将比赛和汇点连一条容量为2的边,之后就是谁参加哪场比赛就对应连一条容量为2的边即可,这样做最大流,如果最后能够满流就说明所有的比赛都能安排妥当。

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #define MAXN 110
    #define MAXD 1110
    #define MAXM 6210
    #define INF 0x3f3f3f3f
    struct Point
    {
        int x, y;    
    }p[MAXM];
    int a[MAXN], N, M, P, first[MAXD], e, next[MAXM], v[MAXM], flow[MAXM];
    int S, T, d[MAXD], q[MAXD], work[MAXD];
    void init()
    {
        int i;
        for(i = 1; i <= N; i ++)
            scanf("%d", &a[i]);
        P = 0;
        for(i = 0; i < M; i ++)
        {
            scanf("%d%d", &p[P].x, &p[P].y);
            if(p[P].x == N || p[P].y == N)
                a[N] += 2;
            else
                ++ P;
        }
    }
    void add(int x, int y, int z)
    {
        v[e] = y, flow[e] = z;
        next[e] = first[x], first[x] = e ++;
    }
    void build()
    {
        int i;
        S = 0, T = N + P;
        memset(first, -1, sizeof(first[0]) * (T + 1));
        e = 0;
        for(i = 0; i < P; i ++)
        {
            add(p[i].x, N + i, 2), add(N + i, p[i].x, 0);    
            add(p[i].y, N + i, 2), add(N + i, p[i].y, 0);
            add(N + i, T, 2), add(T, N + i, 0);
        }
        for(i = 1; i < N; i ++)
            add(S, i, a[N] - a[i] - 1), add(i, S, 0);
    }
    int bfs()
    {
        int i, j, rear = 0;
        memset(d, -1, sizeof(d[0]) * (T + 1));
        d[S] = 0, q[rear ++] = S;
        for(i = 0; i < rear; i ++)
            for(j = first[q[i]]; j != -1; j = next[j])
                if(flow[j] && d[v[j]] == -1)
                {
                    d[v[j]] = d[q[i]] + 1, q[rear ++] = v[j];
                    if(v[j] == T)
                        return 1;
                }
        return 0;
    }
    int dfs(int cur, int a)
    {
        if(cur == T)
            return a;
        int t;
        for(int &i = work[cur]; i != -1; i = next[i])
            if(flow[i] && d[v[i]] == d[cur] + 1)
                if(t = dfs(v[i], std::min(a, flow[i])))
                {
                    flow[i] -= t, flow[i ^ 1] += t;
                    return t;    
                }    
        return 0;
    }
    int dinic()
    {
        int ans = 0, t;
        while(bfs())
        {
            memcpy(work, first, sizeof(first[0]) * (T + 1));
            while(t = dfs(S, INF))
                ans += t;    
        }
        return ans;
    }
    void solve()
    {
        int i;
        for(i = 1; i < N; i ++)
            if(a[i] >= a[N])
            {
                printf("NO\n");
                return ;    
            }
        build();
        if(dinic() == P * 2)
            printf("YES\n");
        else
            printf("NO\n");
    }
    int main()
    {
        while(scanf("%d%d", &N, &M) == 2)
        {
            init();
            solve();    
        }
        return 0;    
    }
  • 相关阅读:
    笔试题系列001
    算法系列001---dfs|多叉|解空间树理解
    leetcode--014 Gas station
    leetcode--012 single number I
    leetcode--011 copy list with random pointer
    leetcode--010 Linked List Cycle II
    leetcode--009 Linked List Cycle I
    leetcode--007 word break I
    leetcode-006 detect cycle
    alex鸡汤回信
  • 原文地址:https://www.cnblogs.com/staginner/p/2626317.html
Copyright © 2011-2022 走看看