• SPOJ 287 Smart Network Administrator


    SPOJ_287

        YY了一下颜色的数量取决于网络流中各条边中最大的流量,于是二分边的容量并做网络流就可以了。

        但至于为什么颜色的数量取决于网络流中各条边中最大的流量,暂时没有细加证明……

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #define MAXD 510
    #define MAXM 501010
    #define INF 0x3f3f3f3f
    int N, M, K, first[MAXD], e, next[MAXM], v[MAXM], flow[MAXM], list[MAXD];
    int S, T, d[MAXD], q[MAXD], work[MAXD];
    struct Edge
    {
        int x, y;
    }edge[MAXM];
    void init()
    {
        int i;
        scanf("%d%d%d", &N, &M, &K);
        for(i = 0; i < K; i ++)
            scanf("%d", &list[i]);
        for(i = 0; i < M; i ++)
            scanf("%d%d", &edge[i].x, &edge[i].y);
    }
    void add(int x, int y, int z)
    {
        v[e] = y, flow[e] = z;
        next[e] = first[x], first[x] = e ++;    
    }
    void build(int c)
    {
        int i;
        memset(first, -1, sizeof(first[0]) * (T + 1));
        e = 0;
        add(S, 1, K), add(1, S, 0);
        for(i = 0; i < K; i ++)
            add(list[i], T, 1), add(T, list[i], 0);
        for(i = 0; i < M; i ++)
            add(edge[i].x, edge[i].y, c), add(edge[i].y, edge[i].x, c);
    }
    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 min, max, mid;
        S = 0, T = N + 1;
        min = -1, max = K;
        for(;;)
        {
            mid = min + max + 1 >> 1;
            if(max == mid) break;
            build(mid);
            if(dinic() == K)
                max = mid;
            else
                min = mid;
        }
        printf("%d\n", mid);
    }
    int main()
    {
        int t;
        scanf("%d", &t);
        while(t --)
        {
            init();
            solve();    
        }
        return 0;
    }
  • 相关阅读:
    BM求递推式模板
    主席树浅谈
    DSU on Tree浅谈
    树链剖分浅谈
    省选模拟八 题解
    提答题 总结
    交互题 总结
    省选模拟七 题解
    省选模拟六 题解
    省选模拟五 题解
  • 原文地址:https://www.cnblogs.com/staginner/p/2627828.html
Copyright © 2020-2023  润新知