SGU 326 Perspective ★(网络流经典构图の竞赛问题)

题意】有n(<=20)只队伍比赛, 队伍i初始得分w[i], 剩余比赛场数r[i](包括与这n只队伍以外的队伍比赛), remain[i][j]表示队伍i与队伍j剩余比赛场数, 没有平局, 问队伍1有没有可能获得这n队中的第一名(可以有并列第一). 【构图方法源点向队伍节点连流量为X的边表示该队伍最多赢X场;两队间比赛节点向汇点连流量为Y的边表示这两队间要进行Y场比赛,两队伍节点向对应比赛节点各连一条流量为Z的边表示每个队最多赢对方Z场思路】 按照上面的思路建图求出最大流,如果是满流则表示比赛可以安排,便为YES. 注意: 一、队伍同其他分区队伍的比赛可以不用管,认为他们全都输掉就可以了. 二、第一个队伍让他全赢就可以了,网络流中只需要建其他N-1个队伍的节点,比赛也是N-1个队伍之间的比赛,不需要管第一支队伍了.  
#include 
#include 
#include 
#include 
#include 
#include 
#define MID(x,y) ((x+y)/2)
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int MAXV = 505;
const int MAXE = 10005;
const int oo = 0x3fffffff;
struct node{
    int u, v, flow;
    int opp;
    int next;
};
struct Dinic{
    node arc[MAXE];
    int vn, en, head[MAXV];     //vn点个数(包括源点汇点),en边个数
    int cur[MAXV];              //当前弧
    int q[MAXV];                //bfs建层次图时的队列
    int path[MAXE], top;        //存dfs当前最短路径的栈
    int dep[MAXV];              //各节点层次
    void init(int n){
        vn = n;
        en = 0;
        mem(head, -1);
    }
    void insert_flow(int u, int v, int flow){
        arc[en].u = u;
        arc[en].v = v;
        arc[en].flow = flow;
        arc[en].opp = en + 1;
        arc[en].next = head[u];
        head[u] = en ++;

        arc[en].u = v;
        arc[en].v = u;
        arc[en].flow = 0;       //反向弧
        arc[en].opp = en - 1;
        arc[en].next = head[v];
        head[v] = en ++;
    }
    bool bfs(int s, int t){
        mem(dep, -1);
        int lq = 0, rq = 1;
        dep[s] = 0;
        q[lq] = s;
        while(lq < rq){
            int u = q[lq ++];
            if (u == t){
                return true;
            }
            for (int i = head[u]; i != -1; i = arc[i].next){
                int v = arc[i].v;
                if (dep[v] == -1 && arc[i].flow > 0){
                    dep[v] = dep[u] + 1;
                    q[rq ++] = v;
                }
            }
        }
        return false;
    }
    int solve(int s, int t){
        int maxflow = 0;
        while(bfs(s, t)){
            int i, j;
            for (i = 1; i <= vn; i ++)  cur[i] = head[i];
            for (i = s, top = 0;;){
                if (i == t){
                    int mink;
                    int minflow = 0x3fffffff;
                    for (int k = 0; k < top; k ++)
                        if (minflow > arc[path[k]].flow){
                            minflow = arc[path[k]].flow;
                            mink = k;
                        }
                    for (int k = 0; k < top; k ++)
                        arc[path[k]].flow -= minflow, arc[arc[path[k]].opp].flow += minflow;
                    maxflow += minflow;
                    top = mink;		//arc[mink]这条边流量变为0, 则直接回溯到该边的起点即可(这条边将不再包含在增广路内).
                    i = arc[path[top]].u;
                }
                for (j = cur[i]; j != -1; cur[i] = j = arc[j].next){
                    int v = arc[j].v;
                    if (arc[j].flow && dep[v] == dep[i] + 1)
                        break;
                }
                if (j != -1){
                    path[top ++] = j;
                    i = arc[j].v;
                }
                else{
                    if (top == 0)   break;
                    dep[i] = -1;
                    i = arc[path[-- top]].u;
                }
            }
        }
        return maxflow;
    }
}dinic;
int win[25];
int remain[25][25];
int contest;
int con[MAXV][MAXV];        //记录i和j的比赛节点号
int main(){
	//freopen("test.in", "r", stdin);
	//freopen("test.out", "w", stdout);
    int n;
    scanf("%d", &n);
    int max_win = 0;
    for (int i = 1; i <= n; i ++){
        scanf("%d", &win[i]);
        max_win = max(max_win, win[i]);
    }
    for (int i = 1; i <= n; i ++){
        int tmp;
        scanf("%d", &tmp);
        if (i == 1){
            win[1] += tmp;
        }
    }
    if (win[1] < max_win){
        puts("NO");
        return 0;
    }
    contest = 1;
    for (int i = 1; i <= n; i ++){
        for (int j = 1; j <= n; j ++){
            if (i > j){
                con[i][j] = con[j][i] = contest ++;
            }
            scanf("%d", &remain[i][j]);
        }
    }
    int node_num = n + n*(n-1)/2;
    int sum = 0;
    dinic.init(node_num+2);
    for (int i = 2; i <= n; i ++){
        dinic.insert_flow(node_num+1, i, win[1] - win[i]);              //源点向N-1个队伍连一条边表示每支队伍最多能赢几场
    }
    for (int i = 2; i <= n; i ++){
        for (int j = i+1; j <= n; j ++){
            dinic.insert_flow(n+con[i][j], node_num+2, remain[i][j]);     //两两队伍间的比赛建一个节点,向汇点连一条边表示两个队伍将进行几场比赛
            dinic.insert_flow(i, n+con[i][j], remain[i][j]);              //i向该比赛连一条边表示i能赢几场
            dinic.insert_flow(j, n+con[i][j], remain[i][j]);              //j向该比赛连一条边表示i能赢几场
            sum += remain[i][j];
        }
    }
    if (dinic.solve(node_num+1, node_num+2) == sum){
        puts("YES");
    }
    else{
        puts("NO");
    }
	return 0;
}
 
posted @ 2013-07-19 14:48  AbandonZHANG  阅读(135)  评论(0编辑  收藏  举报