Ural 1997-Those are not the droids you're looking for 二分图匹配

挺水的一道模板题让我硬生生错了10发,不想说什么了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int a,b;
int n;
int t,d;
int count1,count2;
bool visit[1111];
int p[1111],q[1111];
int link[1111];
bool map[1111][1111];
bool dfs(int k)//寻找从k出发的可增广路 
{
	int i,j;
	for(int i=1;i<=count2;i++)
	{
		if(map[k][i]&&!visit[i])//和cap关联的点,且不在增广路上 
		{
			visit[i]=1;//加入增广路 
			j=link[i];
			if(j==-1||dfs(j))//j是未盖点 或者 从j的对应项出发有可增广路) 
			{
				link[i]=k;
				return true;//k有可增广路,返回true;
			}
		}
	}
	return false;//则从k没有可增广路,返回false;
}
int hungary()
{
	int num=0;
	memset(link,-1,sizeof(link));//初始化 
	for(int i=1;i<=count1;i++)
	{
		memset(visit,0,sizeof(visit));
		if(dfs(i))//有增广路 
		{
			num++;//匹配数++ 
		}
	}
	return num;
}
int main()
{
	   count1=0;count2=0;
	    scanf("%d%d",&a,&b);
		scanf("%d",&n);
		memset(p,0,sizeof(p));
		memset(q,0,sizeof(q));
		memset(map,0,sizeof(map));
		for(int i=1;i<=n;i++)
		{
			scanf("%d%d",&t,&d);
			if(d==0)
			{
				count1++;
				p[count1]=t;
			}
			else
			{
				count2++;
				q[count2]=t;
			}
		}
		for(int i=1;i<=count1;i++)
		{
			for(int j=1;j<=count2;j++)
			{
				if(p[i]>=q[j]) continue;//进去时间必须小于出去的时间 
				int cnt=q[j]-p[i];
				if(cnt<=b||cnt>=a)//不加上面那个判断存在cnt<0的情况 
				{
					map[i][j]=1;
				}
			}
		}
		int ans=hungary();
		if(ans*2!=n)
		{
			printf("Liar\n");
		}
		else
		{
			 printf("No reason\n");
			for(int i=1;i<=count2;i++)
			{
				printf("%d %d\n",p[link[i]],q[i]);
			}
		}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用代码解决这个问题The program committee of the school programming contests, which are often held at the Ural State University, is a big, joyful, and united team. In fact, they are so united that the time spent together at the university is not enough for them, so they often visit each other at their homes. In addition, they are quite athletic and like walking. Once the guardian of the traditions of the sports programming at the Ural State University decided that the members of the program committee spent too much time walking from home to home. They could have spent that time inventing and preparing new problems instead. To prove that, he wanted to calculate the average distance that the members of the program committee walked when they visited each other. The guardian took a map of Yekaterinburg, marked the houses of all the members of the program committee there, and wrote down their coordinates. However, there were so many coordinates that he wasn't able to solve that problem and asked for your help. The city of Yekaterinburg is a rectangle with the sides parallel to the coordinate axes. All the streets stretch from east to west or from north to south through the whole city, from one end to the other. The house of each member of the program committee is located strictly at the intersection of two orthogonal streets. It is known that all the members of the program committee walk only along the streets, because it is more pleasant to walk on sidewalks than on small courtyard paths. Of course, when walking from one house to another, they always choose the shortest way. All the members of the program committee visit each other equally often. Input The first line contains the number n of members of the program committee (2 ≤ n ≤ 105). The i-th of the following n lines contains space-separated coordinates xi, yi of the house of the i-th member of the program committee (1 ≤ xi, yi ≤ 106). All coordinates are integers. Output Output the average distance, rounded down to an integer, that a member of the program committee walks from his house to the house of his colleague.
最新发布
05-26

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值