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): 3760    Accepted Submission(s): 1118


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

网络流求是否解存在。

对每个点dfs,如果可以到达根节点,说明有环--不能走回头边。

无环说明解唯一


最大的问题在于:如果一条边的容量为0,我不建这条边,就wa了,建就ac 不理解~~~~~!!!!!!!!!!!!!!!!!!

!!!!1


#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=1000;//点数的最大值
const int maxm=800000;//边数的最大值
const int inf=20000000;
struct Edge{
    int from,to,next, cap;
    Edge(){}
    Edge(int from,int to,int next,int  cap):from(from),to(to),next(next),cap(cap){}
}edge[maxm];
int saptail;
int head[maxn];
int dep[maxn];//深度标记
int gap[maxn];//断层标记
int mark[maxn];//弧优化
int stack[maxn];//栈
void init(){
    saptail=0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int  w){
    edge[saptail]=Edge(u,v,head[u],w);
    head[u] = saptail++;//有向图反边cap为0
    edge[saptail]=Edge(v,u,head[v],0);
    head[v] = saptail++;
}
//预先分层处理优化
void BFS(int start,int end){
    memset(dep,-1,sizeof(dep));
    memset(gap,0,sizeof(gap));
    gap[0]=1;
    int front,rear;
    front=rear=0;
    dep[end]=0;
    stack[rear++]=end;
    while(front!=rear){
        int u=stack[front++];
        if(front==maxn)front=0;
        for(int i=head[u];i!=-1;i=edge[i].next){
            int v=edge[i].to;
            if(dep[v]!=-1)continue;
            stack[rear++]=v;
            if(rear==maxn)rear=0;
            dep[v]=dep[u]+1;
            ++gap[dep[v]];
        }
    }
}
//开始节点,结束,点数
int  SAP(int start,int end,int TNode){
    int top=0,i,u=start,inser;
    int res = 0,temp;
    BFS(start,end);
    memcpy(mark,head,sizeof(head));
    while(dep[start]<TNode){
        if(u==end){
            temp=inf;
            for(i=0;i<top;i++)
               if(temp>edge[stack[i]].cap){
                   temp=edge[stack[i]].cap;
                   inser=i;
               }
            for(i=0;i<top;i++){
                edge[stack[i]].cap-=temp;
                edge[stack[i]^1].cap+=temp;
            }
            res+=temp;
            top=inser;
            u=edge[stack[top]].from;
        }//出现断层,无增广路
         if(u!=end&&gap[dep[u]-1]==0)
          break;
 //寻找增广路
        for(i=mark[u];i!=-1;i=edge[i].next)
        if(edge[i].cap!=0&&dep[u]==dep[edge[i].to]+1)
             break;
        if(i!=-1){
            mark[u]=i;
            stack[top++]=i;
            u=edge[i].to;
        }
        else{
            int minn=TNode;
            for(i=head[u];i!=-1;i=edge[i].next){
           if(edge[i].cap==0)continue;
                if(minn>dep[edge[i].to]){
                    minn=dep[edge[i].to];
                    mark[u]=i;
                }
            }
            --gap[dep[u]];
            dep[u]=minn+1;
            ++gap[dep[u]];
            if(u!=start)u=edge[stack[--top]].from;
        }
    }
    return res;
}

int n,m,k;
int sumc[407],sumr[407],matri[407][407];
int floor[407];
int find(int u,int from){
    if(floor[u] == 1) return 1;
    if(floor[u] != 0) return 0;
    floor[u] = floor[from]+1;
    for(int i = head[u];i != -1; i=edge[i].next){
        int v = edge[i].to;
        if(v == from ) continue;
        if(edge[i].cap > 0){
            if(find(v,u))return 1;
        }
    }
    return 0;
}

int isok(){
    int u,v;
    memset(matri,0,sizeof(matri));
    for(int i = n+1;i <= n+m; i++){
        for(int j = head[i]; j != -1; j=edge[j].next){
            v = edge[j].to;
            if(v>0&&v<=n){
                matri[v-1][i-n-1] = edge[j].cap;
            }
        }
    }
    for(int i = 1;i <= n; i++){
        memset(floor,0,sizeof(floor));
        if(find(i,-1))return 0;
    }
    return 1;
}

int main(){
    while(scanf("%d%d%d",&n,&m,&k) != EOF){
        int tr=0,tc=0,flag=1;
        for(int i = 0;i < n; i++){
            scanf("%d",&sumr[i]);
            tr+=sumr[i];
        }
        for(int i = 0;i < m; i++){
            scanf("%d",&sumc[i]);
            tc +=sumc[i];
        }
        if(tc != tr ){
            printf("Impossible\n");
            continue;
        }
        int s = 0, e = m+n+1,u;
        init();

        for(int i = 0;i < n;i++)
            addedge(s,i+1,sumr[i]);
        for(int i = 0;i < n; i++){
            for(int j = 0;j < m; j++){
                u = min(sumr[i],min(sumc[j],k));
                addedge(i+1,j+1+n,u);
            }
        }
        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        for(int i = 0;i < m; i++)
            if(sumc[i] > 0) addedge(i+n+1,e,sumc[i]);




        int ans = SAP(s,e,n+m+2);
        if(ans != tr){
            printf("Impossible\n");
            continue;
        }
        ans = isok();
        if(ans == 1){
            printf("Unique\n");
            for(int i = 0;i < n; i++){
                for(int j = 0;j < m;j++){
                    if(j != 0) printf(" ");
                    printf("%d",matri[i][j]);
                }
                printf("\n");
            }
        }
        else printf("Not Unique\n");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GDRetop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值