HDU Redraw Beautiful Drawings 判斷最大流是否唯一解,hduredraw

來源:互聯網
上載者:User

HDU Redraw Beautiful Drawings 判斷最大流是否唯一解,hduredraw
點擊開啟連結
Redraw Beautiful DrawingsTime Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1660    Accepted Submission(s): 357


Problem DescriptionAlice 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. 
InputThe 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. 
OutputFor 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 44 24 2  4 2 22 2 5 05 41 4 391 2 3 3
 
Sample Output
Not UniqueImpossibleUnique1 2 3 3
 
AuthorFudan University 
給你一個n*m的矩陣,然後個格子裡面有一個小於k的數,並且告訴你每行和每列的和,讓你求是否存在解,並判斷是否唯一。 建圖:源點和每行連邊,容量為每行的和。每列和匯點連邊,容量為每列的和。每行和每列連邊,容量為k。 判斷是否存在唯一解,在殘留網路裡面能否找到一個環。
//515MS7304K#include<stdio.h>#include<string.h>#define M 1007int s,t,n,m,k,sum1,sum2;int row[M],col[M],vis[M],g[M][M];const int MAXN=20010;//點數的最大值const int MAXM=880010;//邊數的最大值const int INF=0x3f3f3f3f;struct Node{    int from,to,next;    int cap;}edge[MAXM];int tol;int head[MAXN];int dis[MAXN];int gap[MAXN];//gap[x]=y :說明殘留網路中dis[i]==x的個數為yvoid 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++;}void BFS(int start,int end){    memset(dis,-1,sizeof(dis));    memset(gap,0,sizeof(gap));    gap[0]=1;    int que[MAXN];    int front,rear;    front=rear=0;    dis[end]=0;    que[rear++]=end;    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(dis[v]!=-1)continue;            que[rear++]=v;            if(rear==MAXN)rear=0;            dis[v]=dis[u]+1;            ++gap[dis[v]];        }    }}int SAP(int start,int end){    int res=0,nn=end+1;    BFS(start,end);    int cur[MAXN];    int S[MAXN];    int top=0;    memcpy(cur,head,sizeof(head));    int u=start;    int i;    while(dis[start]<nn)    {        if(u==end)        {            int temp=INF;            int inser;            for(i=0;i<top;i++)               if(temp>edge[S[i]].cap)               {                   temp=edge[S[i]].cap;                   inser=i;               }            for(i=0;i<top;i++)            {                edge[S[i]].cap-=temp;                edge[S[i]^1].cap+=temp;            }            res+=temp;            top=inser;            u=edge[S[top]].from;        }        if(u!=end&&gap[dis[u]-1]==0)//出現斷層,無增廣路          break;        for(i=cur[u];i!=-1;i=edge[i].next)           if(edge[i].cap!=0&&dis[u]==dis[edge[i].to]+1)             break;        if(i!=-1)        {            cur[u]=i;            S[top++]=i;            u=edge[i].to;        }        else        {            int min=nn;            for(i=head[u];i!=-1;i=edge[i].next)            {                if(edge[i].cap==0)continue;                if(min>dis[edge[i].to])                {                    min=dis[edge[i].to];                    cur[u]=i;                }            }            --gap[dis[u]];            dis[u]=min+1;            ++gap[dis[u]];            if(u!=start)u=edge[S[--top]].from;        }    }    return res;}void build(){    sum1=0,sum2=0;    for(int i=1;i<=n;i++)    {        scanf("%d",&row[i]);        sum1+=row[i];        addedge(s,i,row[i]);    }    for(int i=1;i<=m;i++)    {         scanf("%d",&col[i]);         sum2+=col[i];         addedge(n+i,t,col[i]);    }    for(int i=1;i<=n;i++)        for(int j=1;j<=m;j++)            addedge(i,n+j,k);}bool iscycle(int u,int fa){    for(int i=head[u];i!=-1;i=edge[i].next)    {        if(i==(fa^1))continue;        if(edge[i].cap)        {            if(vis[edge[i].to])return true;            vis[edge[i].to]=true;            if(iscycle(edge[i].to,i))return true;            vis[edge[i].to]=false;        }    }    return false;}void solve(){    if(sum1!=sum2){printf("Impossible\n");return;}    int anss=SAP(s,t);    //printf("anss=%d\n",anss);    if(anss!=sum1){printf("Impossible\n");return;}    memset(vis,false,sizeof(vis));    int flag=0;    for(int i=1;i<=n;i++)//判斷殘留網路是否存在環        if(iscycle(i,-1))            {flag=1;break;}    if(flag)printf("Not Unique\n");    else    {        printf("Unique\n");        for(int u=1;u<=n;u++)            for(int i=head[u];i!=-1;i=edge[i].next)            {                int v=edge[i].to;                if(v>n&&v<=n+m)                    g[u][v-n]=k-edge[i].cap;            }        for(int i=1;i<=n;i++)        {            for(int j=1;j<m;j++)                printf("%d ",g[i][j]);            printf("%d\n",g[i][m]);        }    }}int main(){    while(scanf("%d%d%d",&n,&m,&k)!=EOF)    {        s=0,t=n+m+1;        init();        build();        solve();    }}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.