zoukankan      html  css  js  c++  java
  • hdu 4888(判断最大流是否唯一)

    题目链接

    Redraw Beautiful Drawings

    Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 4862    Accepted Submission(s): 1415


    Problem Description
    Alice and Bob are playing together. Alice is crazy about art and she has visited many museums around the world. She has a good memory and she can remember all drawings she has seen.

    Today Alice designs a game using these drawings in her memory. First, she matches K+1 colors appears in the picture to K+1 different integers(from 0 to K). After that, she slices the drawing into grids and there are N rows and M columns. Each grid has an integer on it(from 0 to K) representing the color on the corresponding position in the original drawing. Alice wants to share the wonderful drawings with Bob and she tells Bob the size of the drawing, the number of different colors, and the sum of integers on each row and each column. Bob has to redraw the drawing with Alice's information. Unfortunately, somtimes, the information Alice offers is wrong because of Alice's poor math. And sometimes, Bob can work out multiple different drawings using the information Alice provides. Bob gets confused and he needs your help. You have to tell Bob if Alice's information is right and if her information is right you should also tell Bob whether he can get a unique drawing.
     
    Input
    The input contains mutiple testcases.

    For each testcase, the first line contains three integers N(1 ≤ N ≤ 400) , M(1 ≤ M ≤ 400) and K(1 ≤ K ≤ 40).
    N integers are given in the second line representing the sum of N rows.
    M integers are given in the third line representing the sum of M columns.

    The input is terminated by EOF.
     
    Output
    For each testcase, if there is no solution for Bob, output "Impossible" in one line(without the quotation mark); if there is only one solution for Bob, output "Unique" in one line(without the quotation mark) and output an N * M matrix in the following N lines representing Bob's unique solution; if there are many ways for Bob to redraw the drawing, output "Not Unique" in one line(without the quotation mark).
     
    Sample Input
    2 2 4 4 2 4 2 4 2 2 2 2 5 0 5 4 1 4 3 9 1 2 3 3
     
    Sample Output
    Not Unique Impossible Unique 1 2 3 3
     
    Author
    Fudan University
     
    Source

    题意:给一个n*m的矩阵,每个位置有一个0~K的数,给出每一行及每一列的数的和,判断是否有解,并输出方案。

    题解:求最大流,从源点向行点连容量为行总和的边,从列点向汇点连容量为列总和的边,对于每个格子,连从该格子所在行点->该格子所在列点连容量为K的边。这样跑出最大流,如果是满流就有解。判断多解就是判断最大流是否有多解。

    判断最大流是否唯一的方法:在残留网络中,如果能找到一个环,那么流量就可以从这个环绕一圈,而不改变最大流。这个dfs一下就好了。

    注意:1.此处残流网络是指还有容量的边,包括建图时加的反向边,因此形成的是一个有向图

       2.此处的环的大小必须是大于2,因为对于一条没有满流的边,那么这个条边的两个端点就会形成一个二元环(因为有反向边),那么dfs的时候记录每个点的前驱节点就行了。

        3.有向图的判环,有一种“通用写法”(见代码2),可是这一题不能用那个通用写法,因为此处不考虑二元环,那么访问一个点的时候并没有全部访问完它连出去的所有边,因此不能使用那种写法

    代码1:ac代码

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<vector>
    #include<queue>
    #include<stack>
    using namespace std;
    #define rep(i,a,n) for (int i=a;i<n;i++)
    #define per(i,a,n) for (int i=n-1;i>=a;i--)
    #define pb push_back
    #define fi first
    #define se second
    typedef vector<int> VI;
    typedef long long ll;
    typedef pair<int,int> PII;
    const int inf=0x3fffffff;
    const ll mod=1000000007;
    const int maxn=810;//点数的最大值
    const int maxm=5e5+100;//边数的最大值
    struct Node
    {
        int from,to,next;
        int cap;
    }edge[maxm];
    int tol;
    int dep[maxn];//dep为点的层次
    int head[maxn];
    void init()
    {
        tol=0;
        //memset(head,-1,sizeof(head));
    }
    void addedge(int u,int v,int w)//第一条边下标必须为偶数
    {
        edge[tol].from=u;
        edge[tol].to=v;
        edge[tol].cap=w;
        edge[tol].next=head[u];
        head[u]=tol++;
        edge[tol].from=v;
        edge[tol].to=u;
        edge[tol].cap=0;
        edge[tol].next=head[v];
        head[v]=tol++;
    }
    int BFS(int start,int end)
    {
        int que[maxn];
        int front,rear;
        front=rear=0;
        memset(dep,-1,sizeof(dep));
        que[rear++]=start;
        dep[start]=0;
        while(front!=rear)
        {
            int u=que[front++];
            if(front==maxn)front=0;
            for(int i=head[u];i!=-1;i=edge[i].next)
            {
                int v=edge[i].to;
                if(edge[i].cap>0&&dep[v]==-1)
                {
                    dep[v]=dep[u]+1;
                    que[rear++]=v;
                    if(rear>=maxn)rear=0;
                    if(v==end)return 1;
                }
            }
        }
        return 0;
    }
    int dinic(int start,int end)
    {
        int res=0;
        int top;
        int stack[maxn];//stack为栈,存储当前增广路
        int cur[maxn];//存储当前点的后继
        while(BFS(start,end))
        {
            memcpy(cur,head,sizeof(head));
            int u=start;
            top=0;
            while(1)
            {
                if(u==end)
                {
                    int min=inf;
                    int loc;
                    for(int i=0;i<top;i++)
                        if(min>edge[stack[i]].cap)
                        {
                            min=edge[stack[i]].cap;
                            loc=i;
                        }
                    for(int i=0;i<top;i++)
                    {
                        edge[stack[i]].cap-=min;
                        edge[stack[i]^1].cap+=min;
                    }
                    res+=min;
                    top=loc;
                    u=edge[stack[top]].from;
                }
                for(int i=cur[u];i!=-1;cur[u]=i=edge[i].next)
                    if(edge[i].cap!=0&&dep[u]+1==dep[edge[i].to])
                        break;
                if(cur[u]!=-1)
                {
                    stack[top++]=cur[u];
                    u=edge[cur[u]].to;
                }
                else
                {
                    if(top==0)break;
                    dep[u]=-1;
                    u=edge[stack[--top]].from;
                }
            }
        }
        return res;
    }
    int vis[maxn];
    bool dfs(int u,int f)
    {
        vis[u]=1;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(v==f) continue;
            if(!edge[i].cap) continue;
            if(vis[v]) return true;
            if(dfs(v,u)) return true;
        }
        vis[u]=0;
        return false;
    }
    int main()
    {
        int n,m,k;
        while(~scanf("%d%d%d",&n,&m,&k))
        {
            int st=0,ed=n+m+1;
            init();
            rep(i,st,ed+1) head[i]=-1,vis[i]=0;
            ll sum1=0,sum2=0;
            bool f1=false;
            rep(i,1,n+1)
            {
                ll v;
                scanf("%lld",&v);
                addedge(st,i,(int)v);
                if(v>1ll*k*m)
                {
                    f1=true;
                }
                sum1+=v;
            }
            rep(i,1,m+1)
            {
                ll v;
                scanf("%lld",&v);
                addedge(n+i,ed,(int)v);
                if(v>1ll*k*n)
                {
                    f1=true;
                }
                sum2+=v;
            }
            if(sum1!=sum2||f1)
            {
                puts("Impossible");
                continue;
            }
            rep(i,1,n+1) rep(j,1,m+1) addedge(i,n+j,k);
            if(dinic(st,ed)!=sum1)
            {
                puts("Impossible");
                continue;
            }
            else
            {
                bool f=false;
                rep(i,1,n+1) if(dfs(i,-1))
                {
                    f=true;
                    break;
                }
                if(f) puts("Not Unique");
                else
                {
                    puts("Unique");
                    int t=0;
                    while(edge[t].from!=1||edge[t].to!=n+1) t++;
                    int cnt=0;
                    for(int i=t;i<tol;i+=2)
                    {
                        cnt++;
                        printf("%d%c",k-edge[i].cap,cnt%m==0? '
    ':' ');
                    }
                }
            }
        }
        return 0;
    }

    代码2:(与前一个代码不同之处已经标记出来了)

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<vector>
    #include<queue>
    #include<stack>
    using namespace std;
    #define rep(i,a,n) for (int i=a;i<n;i++)
    #define per(i,a,n) for (int i=n-1;i>=a;i--)
    #define pb push_back
    #define fi first
    #define se second
    typedef vector<int> VI;
    typedef long long ll;
    typedef pair<int,int> PII;
    const int inf=0x3fffffff;
    const ll mod=1000000007;
    const int maxn=810;//点数的最大值
    const int maxm=5e5+100;//边数的最大值
    struct Node
    {
        int from,to,next;
        int cap;
    }edge[maxm];
    int tol;
    int dep[maxn];//dep为点的层次
    int head[maxn];
    void init()
    {
        tol=0;
        //memset(head,-1,sizeof(head));
    }
    void addedge(int u,int v,int w)//第一条边下标必须为偶数
    {
        edge[tol].from=u;
        edge[tol].to=v;
        edge[tol].cap=w;
        edge[tol].next=head[u];
        head[u]=tol++;
        edge[tol].from=v;
        edge[tol].to=u;
        edge[tol].cap=0;
        edge[tol].next=head[v];
        head[v]=tol++;
    }
    int BFS(int start,int end)
    {
        int que[maxn];
        int front,rear;
        front=rear=0;
        memset(dep,-1,sizeof(dep));
        que[rear++]=start;
        dep[start]=0;
        while(front!=rear)
        {
            int u=que[front++];
            if(front==maxn)front=0;
            for(int i=head[u];i!=-1;i=edge[i].next)
            {
                int v=edge[i].to;
                if(edge[i].cap>0&&dep[v]==-1)
                {
                    dep[v]=dep[u]+1;
                    que[rear++]=v;
                    if(rear>=maxn)rear=0;
                    if(v==end)return 1;
                }
            }
        }
        return 0;
    }
    int dinic(int start,int end)
    {
        int res=0;
        int top;
        int stack[maxn];//stack为栈,存储当前增广路
        int cur[maxn];//存储当前点的后继
        while(BFS(start,end))
        {
            memcpy(cur,head,sizeof(head));
            int u=start;
            top=0;
            while(1)
            {
                if(u==end)
                {
                    int min=inf;
                    int loc;
                    for(int i=0;i<top;i++)
                        if(min>edge[stack[i]].cap)
                        {
                            min=edge[stack[i]].cap;
                            loc=i;
                        }
                    for(int i=0;i<top;i++)
                    {
                        edge[stack[i]].cap-=min;
                        edge[stack[i]^1].cap+=min;
                    }
                    res+=min;
                    top=loc;
                    u=edge[stack[top]].from;
                }
                for(int i=cur[u];i!=-1;cur[u]=i=edge[i].next)
                    if(edge[i].cap!=0&&dep[u]+1==dep[edge[i].to])
                        break;
                if(cur[u]!=-1)
                {
                    stack[top++]=cur[u];
                    u=edge[cur[u]].to;
                }
                else
                {
                    if(top==0)break;
                    dep[u]=-1;
                    u=edge[stack[--top]].from;
                }
            }
        }
        return res;
    }
    int vis[maxn];
    /**********************************************/
    bool dfs(int u,int f)
    {
        vis[u]=1;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(v==f) continue;
            if(!edge[i].cap) continue;
            if(vis[v])
            {
                if(vis[v]==1)
                return true;
            }
            else if(dfs(v,u)) return true;
        }
        vis[u]=2;
        return false;
    }
    /*********************************************/
    int main()
    {
        int n,m,k;
        while(~scanf("%d%d%d",&n,&m,&k))
        {
            int st=0,ed=n+m+1;
            init();
            rep(i,st,ed+1) head[i]=-1,vis[i]=0;
            ll sum1=0,sum2=0;
            bool f1=false;
            rep(i,1,n+1)
            {
                ll v;
                scanf("%lld",&v);
                addedge(st,i,(int)v);
                if(v>1ll*k*m)
                {
                    f1=true;
                }
                sum1+=v;
            }
            rep(i,1,m+1)
            {
                ll v;
                scanf("%lld",&v);
                addedge(n+i,ed,(int)v);
                if(v>1ll*k*n)
                {
                    f1=true;
                }
                sum2+=v;
            }
            if(sum1!=sum2||f1)
            {
                puts("Impossible");
                continue;
            }
            rep(i,1,n+1) rep(j,1,m+1) addedge(i,n+j,k);
            if(dinic(st,ed)!=sum1)
            {
                puts("Impossible");
                continue;
            }
            else
            {
                bool f=false;
                rep(i,1,n+1) if(!vis[i]&&dfs(i,-1))  /********/
                {
                    f=true;
                    break;
                }
                if(f) puts("Not Unique");
                else
                {
                    puts("Unique");
                    int t=0;
                    while(edge[t].from!=1||edge[t].to!=n+1) t++;
                    int cnt=0;
                    for(int i=t;i<tol;i+=2)
                    {
                        cnt++;
                        printf("%d%c",k-edge[i].cap,cnt%m==0? '
    ':' ');
                    }
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    BZOJ1556 墓地秘密
    [NOI2006]网络收费
    UVA11401 Triangle Counting
    UVA11538 Chess Queen
    BZOJ2560 串珠子
    BZOJ4057 [Cerc2012]Kingdoms
    [HNOI2012] 集合选数
    [Haoi2016]字符合并
    [Snoi2013]Quare
    洛谷平衡树模板总结
  • 原文地址:https://www.cnblogs.com/tarjan/p/7219291.html
Copyright © 2011-2022 走看看