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

博客围绕信息传递问题展开,已知大学要发紧急通知,老师位置已知,老师接收信息后会传给相邻位置老师。需确定最少通知多少老师能让所有人看到通知。解题思路是求出最大匹配人数,结合总人数计算出最少通知人数,还提到book数组重置的优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

张宜强

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

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

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

打赏作者

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

抵扣说明:

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

余额充值