河南省第十二届大学生程序设计竞赛 F: Information Transmission-1(二分图最大匹配)

http://nyoj.top/problem/1656

题目描述:

Information resources are information producers, information and information technology organisms. The fundamental purpose of information management is to control the flow of information and realize the utility and value of information. The broad meaning of information transmission is the transfer of information between media. Strictly speaking, all information processing is the transfer of information within the organization, that is, the movement of information in its physical location.

 

Information transmission methods include one-way transmission, two-way transmission, half-two-way transmission (only one direction per transmission), multi-channel transmission (one channel through multiple signals), etc.

 

Now, a university has a urgent notice, hoping all teachers can see the content at the first time. The physical location of all teachers is known , Once a teacher (xi,yi ) receives information,  he or she immediately passes it to another teacher in the adjacent position (xi+1,yi ) or (xi-1,yi ) or (xi,yi+1 ) or (xi,yi -1).

 

 

You can tell the authority , at first which teachers should be notified at least to ensure that everyone can see the notice content at the first time.

输入描述:

The first line of the input contains one integer T, which is the number of  test cases (1<=T<=8).  Each test case specify:
* Line 1:    m  n                  ( 1 ≤ m ≤ 200, 1 ≤ n ≤ 500  )
* Line 2~m+1:  Each row has n numbers, ‘1’ indicates that there is a teacher in this position.

输出描述:

For each test case generate a single line:  minimum number of teachers to be notified first.

样例输入:

2
1 10
1110111111
7 9
000110000
110010001
010011011
000000000
111111100
010100100
111111100

样例输出:

5
17

题意分析:

有一个情报要让所有人知道,1代表有人,每个人只能把情报告诉他上、下、左、右的其中一个人,问最少应该把情报告诉多少人才能让所有人知道。

解题思路:

每个人可以和他周围的人匹配,求出最大匹配人数s,这s人中只用告诉s/2的人,另外s/2的人不用告诉,可以由相匹配的人告诉,人数总数为sum,剩下sum-s的人需要单独告诉,所有总的需要告诉的人为sum-s+s/2人。

需要注意的是book数组重置0很费时间,这里在dfs回溯是将book数组置为0。

#include <stdio.h>
#include <string.h>
#define N 510
char e[220][N];
int m, n, book[220][N], temp;
int next[4][2]={1,0, -1,0, 0,1, 0,-1};
struct data{
	int x;
	int y;
}a[N][N];
int dfs(int x, int y)
{
	int i, tx, ty;
	for(i=0; i<4; i++)
	{
		tx=x+next[i][0];
		ty=y+next[i][1];
		if(tx>m || ty>n || tx<=0 || ty<=0)
			continue;
		if(book[tx][ty]==0 && e[tx][ty]=='1')
		{
			book[tx][ty]=1;
			if(a[tx][ty].x==0&&a[tx][ty].y==0 || dfs(a[tx][ty].x, a[tx][ty].y))
			{
				a[tx][ty].x=x;
				a[tx][ty].y=y;
				temp=1;
			}
			book[tx][ty]=0;
			if(temp==1)
				return 1;
		}
	}
	return 0;
}
int main()
{
	int t, i, j, sum, ans;
	scanf("%d", &t);
	while(t--)
	{
		memset(a, 0, sizeof(a));
		scanf("%d%d", &m, &n);
		for(i=1; i<=m; i++)
			scanf("%s", e[i]+1);
		ans=0;
		sum=0;
		for(int q=1; q<=m; q++)
			for(int p=1; p<=n;p++)
				book[q][p]=0;
		for(i=1; i<=m; i++)
		{
			for(j=1; j<=n; j++)
			{
				temp=0;
				if(e[i][j]=='1')
					sum++;
				if(e[i][j]=='1' && dfs(i, j))
					ans++;
			}
		}
		ans=ans/2;
		printf("%d\n", ans+sum-2*ans);
	}
	return 0;
} 

 

posted @ 2019-05-30 17:43  宿星  阅读(92)  评论(0编辑  收藏  举报