hdu 4975 A simple Gaussian elimination problem.(网络流 + 判多解)

A simple Gaussian elimination problem.

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1149    Accepted Submission(s): 368


Problem Description
Dragon is studying math. One day, he drew a table with several rows and columns, randomly wrote numbers on each elements of the table. Then he counted the sum of each row and column. Since he thought the map will be useless after he got the sums, he destroyed the table after that.

However Dragon's mom came back and found what he had done. She would give dragon a feast if Dragon could reconstruct the table, otherwise keep Dragon hungry. Dragon is so young and so simple so that the original numbers in the table are one-digit number (e.g. 0-9).

Could you help Dragon to do that?
 

Input
The first line of input contains only one integer, T(<=30), the number of test cases. Following T blocks, each block describes one test case.

There are three lines for each block. The first line contains two integers N(<=500) and M(<=500), showing the number of rows and columns.

The second line contains N integer show the sum of each row.

The third line contains M integer show the sum of each column.
 

Output
Each output should occupy one line. Each line should start with "Case #i: ", with i implying the case number. For each case, if we cannot get the original table, just output: "So naive!", else if we can reconstruct the table by more than one ways, you should output one line contains only: "So young!", otherwise (only one way to reconstruct the table) you should output: "So simple!".
 

Sample Input

3 1 1 5 5 2 2 0 10 0 10 2 2 2 2 2 2
 

Sample Output

Case #1: So simple! Case #2: So naive! Case #3: So young!
 

Author
BJTU


题意:给定一个n * m的网格,要求在每个格子里面填0~9的数字,使其满足每行的和 与没列的和是给定的数
题解:网络流 + dfs在残余网络上判环 来判定是否有多解,保存个代码


#include 
#include 

using namespace std;

typedef int typec;
const int inf = 0x3f3f3f3f;
const int maxn = 1388;

struct edge{
	int x, y, nxt;
	typec c;
}bf[maxn * maxn];

int cur[maxn], ps[maxn], dep[maxn];
int ne, head[maxn];

void addedge(int x, int y, typec c){
	bf[ne].x = x; bf[ne].y = y; bf[ne].c = c;
	bf[ne].nxt = head[x]; head[x] = ne++;
	bf[ne].x = y; bf[ne].y = x; bf[ne].c = 0;
	bf[ne].nxt = head[y]; head[y] = ne++;
}

int flow(int n, int s, int t){
	int i, j, k, f, r, top;
	typec tr, res = 0;

	while(1){
		memset(dep, -1, n * sizeof(int));
		for(f = dep[ps[0] = s] = 0, r = 1; f != r; ){
			for(i = ps[f++], j = head[i]; j; j = bf[j].nxt){
				if(bf[j].c && -1 == dep[k = bf[j].y]){
					dep[k] = dep[i] + 1; ps[r++] = k;
					if(k == t){ f = r; break; }
				}
			}
		}
		if(-1 == dep[t]) break;
		memcpy(cur, head, n * sizeof(int));
		for(i = s, top = 0; ; ){
			if(i == t){
				for(k = 0, tr = inf; k < top; ++k){
					if(bf[ps[k]].c < tr)
						tr = bf[ps[f = k]].c;
				}
				for(k = 0; k < top; ++k){
					bf[ps[k]].c -= tr;
					bf[ps[k] ^ 1].c += tr;
				}
				res += tr; i = bf[ps[top = f]].x;
			}
			for(j = cur[i]; cur[i]; j = cur[i] = bf[cur[i]].nxt){
				if(bf[j].c && dep[i] + 1 == dep[bf[j].y])
					break;
			}
			if(cur[i]){
				ps[top++] = cur[i];
				i = bf[cur[i]].y;
			}
			else{
				if(0 == top) break;
				dep[i] = -1; i = bf[ps[--top]].x;
			}
		}
	}
	return res;
}

void init(){
	ne = 2;
	memset(head, 0, sizeof(head));
}

int row[maxn], col[maxn];
int mark[maxn];

int dfs(int x, int pre){
	int flag = 1;
	mark[x] = 1;
	for(int i = head[x]; i; i = bf[i].nxt){
		if(bf[i].y == pre) continue;
		if(bf[i].c > 0){
			if(mark[bf[i].y]) return 1;
			if(dfs(bf[i].y, x)) return 1;
		}
		if(flag) head[x] = bf[i].nxt;
		else bf[flag].nxt = bf[i].nxt;
		flag = i;
	}
	mark[x] = 0;
	return 0;
}

int judge_multiple_solution(int n, int r, int s, int t){
	memset(mark, 0, n * sizeof(int));
	for(int i = 2; i <= r+1; i++){
		if(dfs(i, -1)) return 1;
	}
	return 0;
}

int main(){
	int t, n, m, cas = 1;

	freopen("testin", "r", stdin);
	scanf("%d", &t);
	while(t--){
		scanf("%d%d", &n, &m);
		int sumr = 0, sumc = 0;
		for(int i = 1; i <= n; i++){
			scanf("%d", row+i);
			sumr += row[i];
		}
		for(int i = 1; i <= m; i++){
			scanf("%d", col+i);
			sumc += col[i];
		}
		if(sumr != sumc){
			printf("Case #%d: So naive!\n", cas++);
			continue;
		}
		// s = 0, t = 1, row = 2 ~ n+1, col = n+2 ~ n+m+1
		init();
		for(int i = 1; i <= n; i++) addedge(0, i+1, row[i]);
		for(int i = 1; i <= m; i++) addedge(n+1+i, 1, col[i]);
		for(int i = 1; i <= n; i++){
			for(int j = 1; j <= m; j++){
				addedge(i+1, n+1+j, 9);
			}
		}
		//printf("cc\n");
		int res = flow(n+m+2, 0, 1);
		//printf("%d   %d\n", res, sumr);
		if(res != sumr){
			printf("Case #%d: So naive!\n", cas++);
		}
		else{
			if(judge_multiple_solution(n+m+2, n, 0, 1)){
				printf("Case #%d: So young!\n", cas++);
			}
			else{
				printf("Case #%d: So simple!\n", cas++);
			}
		}
	}

	return 0;
}


你可能感兴趣的:(ACM之路(c/c++),图论,ACM,网络流,hdu,4975)