hdu 4888 Redraw Beautiful Drawings 网络流+搜索
来源:互联网 发布:买mac还是ipad 编辑:程序博客网 时间:2024/06/08 10:18
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.
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.
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 44 24 2 4 2 22 2 5 05 41 4 391 2 3 3
Sample Output
Not UniqueImpossibleUnique1 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 0
- hdu 4888 Redraw Beautiful Drawings 网络流+搜索
- Hdu-4888 Redraw Beautiful Drawings 网络流
- HDU 4888 Redraw Beautiful Drawings 网络流 建图
- hdu 4888 Redraw Beautiful Drawings 2014多校三补题 网络流
- HDU 4888 Redraw Beautiful Drawings 网络流(矩阵模型)
- 【网络流】 HDOJ 4888 Redraw Beautiful Drawings
- hdu 4888 Redraw Beautiful Drawings(最大流)
- hdu 4888 Redraw Beautiful Drawings 最大流
- hdu 4888 Redraw Beautiful Drawings(最大流)
- HDU 4888 Redraw Beautiful Drawings(最大流)
- hdu 4888 Redraw Beautiful Drawings
- HDU-4888-Redraw Beautiful Drawings
- hdu 4888 Redraw Beautiful Drawings
- HDUOJ--4888--Redraw Beautiful Drawings【isap】网络流+判环
- hdu 4888 Redraw Beautiful Drawings(最大流,判环)
- hdu 4888 Redraw Beautiful Drawings (最大流)
- hdu - 4888 - Redraw Beautiful Drawings(最大流)
- hdu 4888 Redraw Beautiful Drawings 最大流唯一性判断
- TCPIP协议头定义
- Android学习笔记之在图片特效
- JAVA利用HttpClient进行POST请求(HTTPS)
- 《激活你自己》
- ubuntu12.04下hadoop-0.20.1伪模式安装
- hdu 4888 Redraw Beautiful Drawings 网络流+搜索
- XAMPP环境访问非Web DocumentRoot下绝对路径
- Hough变换-直线检测
- Unity发射射线
- leetcode: Remove Element
- Java基础之多线程知识点总结
- mysql中设置id号自增
- 一.创建欢迎界面
- 简洁高效的原型图设计工具Mockplus