程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDOJ 4888 Redraw Beautiful Drawings

HDOJ 4888 Redraw Beautiful Drawings

編輯:C++入門知識

HDOJ 4888 Redraw Beautiful Drawings



最大流判斷多解

建圖:

源點連接到每一個代表行的節點容量為行總和,每一個代表列的節點連接到匯點容量為列總和,行和列之間互相連接容量為Limit

多解:

做一遍ISAP後,在殘量圖上DFS看能否找到點數大於2的環即可

Redraw Beautiful Drawings

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


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 2014 Multi-University Training Contest 3

#include 
#include 
#include 
#include 

using namespace std;

const int maxn=1000;
const int maxm=1001000;
const int INF=0x3f3f3f3f;

struct Edge
{
	int to,next,cap,flow;
}edge[maxm];

int Size,Adj[maxn];
int gap[maxn],dep[maxn],pre[maxn],cur[maxn];

void init()
{
	Size=0; memset(Adj,-1,sizeof(Adj));
}

void addedge(int u,int v,int w,int rw=0)
{
	edge[Size].to=v; edge[Size].cap=w; edge[Size].next=Adj[u];
	edge[Size].flow=0;Adj[u]=Size++;
	edge[Size].to=u; edge[Size].cap=rw; edge[Size].next=Adj[v];
	edge[Size].flow=0;Adj[v]=Size++;
}

int sap(int start,int end,int N)
{
	memset(gap,0,sizeof(gap));
	memset(dep,0,sizeof(dep));
	memcpy(cur,Adj,sizeof(Adj));
	
	int u=start;
	pre[u]=-1; gap[0]=N;
	int ans=0;

	while(dep[start]edge[i].cap-edge[i].flow)
					Min=edge[i].cap-edge[i].flow;
			for(int i=pre[u];~i;i=pre[edge[i^1].to])
			{
				edge[i].flow+=Min;
				edge[i^1].flow-=Min;
			}
			u=start;
			ans+=Min;
			continue;
		}
		bool flag=false;
		int v;
		for(int i=cur[u];~i;i=edge[i].next)
		{
			v=edge[i].to;
			if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u])
			{
				flag=true;
				cur[u]=pre[v]=i;
				break;
			}
		}
		if(flag)
		{
			u=v; continue;
		}
		int Min=N;
		for(int i=Adj[u];~i;i=edge[i].next)
			if(edge[i].cap-edge[i].flow&&dep[edge[i].to]=edge[i].cap) continue;
		if(v==pre) continue;
		if(dfs(v,u)) return true;
	}
	vis[u]=false;
	return false;
}

int main()
{
	while(scanf("%d%d%d",&n,&m,&limit)!=EOF)
	{
		init();
		int sum1=0,sum2=0;
		for(int i=1;i<=n;i++) scanf("%d",a+i),sum1+=a[i];
		for(int i=1;i<=m;i++) scanf("%d",b+i),sum2+=b[i];

		if(sum1!=sum2)
		{
			puts("Impossible");
			continue;
		}

		/**************build graph *****************/
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++)
			{
				id[i][j]=Size;
				addedge(i,n+j,limit);
			}
		for(int i=1;i<=n;i++)
			addedge(0,i,a[i]);
		for(int i=1;i<=m;i++)
			addedge(n+i,n+m+1,b[i]);
		/**************build graph *****************/

		int MaxFlow=sap(0,n+m+1,n+m+1+2);
		//cout<<"MaxFlow: "<


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved