HDU 4888 Redraw Beautiful Drawings 最大流判满流

Redraw Beautiful Drawings

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


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
 


这位童鞋的讲解特别清楚好理解,膜拜  http://blog.csdn.net/cugbbaoge/article/details/38272845


学长的模板就是快。。。。orz  @fp_hzq


#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;

const int INF=1000000000;

int n,m,k;

const int mm=1000*1000;

const int mn=1000;

int node,s,t,edge;

int ver[mm],flow[mm],next[mm];

int head[mn],work[mn],dis[mn],q[mn];

int sumx[mn],sumy[mn];

int fl[444][444];
int vis[444][444];


void init(int _node,int _s,int _t)
{
    node=_node, s=_s, t=_t;
    for(int i=0;i<node;++i)
        head[i]=-1;
    edge=0;
}


void addedge(int u,int v,int c)
{
    ver[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++;
    ver[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++;
}


bool Dinic_bfs()
{
    int i,u,v,l,r=0;
    for(i=0;i<node;++i)  dis[i]=-1;
    dis[ q[r++]=s ] = 0;
    for(l=0;l<r;l++)
    {
        for(i=head[ u=q[l] ]; ~i ;i=next[i])
            if(flow[i] && dis[ v=ver[i] ]<0)
             {
                 dis[ q[r++]=v ]=dis[u]+1;
                 if(v==t) return 1;
             }
    }
    return 0;
}

int Dinic_dfs(int u,int exp)
{
    if(u==t) return exp;
    for(int &i=work[u],v,temp; ~i ;i=next[i])
    {
        if(flow[i] && dis[ v=ver[i] ]==dis[u]+1 && ( temp=Dinic_dfs(v,min(exp,flow[i])) )>0)
        {
            flow[i]-=temp;
            flow[i^1]+=temp;
            return temp;
        }
    }
    return 0;
}

int Dinic_flow()
{
    int ans=0,res,i;
    while(Dinic_bfs())
    {
        for(i=0;i<node;++i) work[i]=head[i];
        while( res=Dinic_dfs(s,INF) )  ans+=res;
    }
    return ans;
}

bool check()
{
    for(int u=1;u<=n;u++)
    {
        for(int i=head[u];~i;i=next[i])
        {
            int v=ver[i];
            if(v>n && v<=n+m)
            {
                fl[u][v-n]=k-flow[i];
            }
        }
    }
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            for(int p=j+1;p<=m;p++)
            {
                bool v1=0,v2=0;
                if(fl[i][j]!=k && fl[i][p]!=0)
                {
                    if(vis[p][j])
                        return true;
                    v1=1;
                }
                if(fl[i][j]!=0 && fl[i][p]!=k)
                {
                    if(vis[j][p])
                        return true;
                    v2=1;
                }
                if(v1) vis[j][p]=1;
                if(v2) vis[p][j]=1;
            }
        }
    }
    return false;
}


int main()
{
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
    {
        init(n+m+2,0,n+m+1);
        memset(sumx,0,sizeof(sumx));
        memset(sumy,0,sizeof(sumy));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&sumx[i]);
            sumx[0]+=sumx[i];
        }
        for(int i=1;i<=m;i++)
        {
            scanf("%d",&sumy[i]);
            sumy[0]+=sumy[i];
        }
        if(sumx[0]!=sumy[0])
        {
            printf("Impossible\n");
            continue;
        }

        for(int i=1;i<=n;i++)
        {
            addedge(s,i,sumx[i]);
            for(int j=1;j<=m;j++)
            {
                addedge(i,n+j,k);
            }
        }
        for(int j=1;j<=m;j++)
            addedge(n+j,t,sumy[j]);
        int ret=Dinic_flow();

        //cout<<ret<<endl;

        if(ret!=sumx[0] || ret!=sumy[0])
        {
            printf("Impossible\n");
            continue;
        }
        else
        {
            if(check())
            {
                printf("Not Unique\n");
                continue;
            }
            else
            {
                printf("Unique\n");
                for(int i=1;i<=n;i++)
                {
                   for(int j=1;j<=m;j++)
                   {
                     printf("%d",fl[i][j]);
                     if(j==m)
                        printf("\n");
                     else
                        printf(" ");
                   }
                }
            }
        }


    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值