博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 1422 Air Raid 匈牙利算法
阅读量:3906 次
发布时间:2019-05-23

本文共 3272 字,大约阅读时间需要 10 分钟。

题目:

Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an intersection and walking through town's streets you can never reach the same intersection i.e. the town's streets form no cycles.

With these assumptions your task is to write a program that finds the minimum number of paratroopers that can descend on the town and visit all the intersections of this town in such a way that more than one paratrooper visits no intersection. Each paratrooper lands at an intersection and can visit other intersections following the town streets. There are no restrictions about the starting intersection for each paratrooper.

Input

Your program should read sets of data. The first line of the input file contains the number of the data sets. Each data set specifies the structure of a town and has the format:

no_of_intersections
no_of_streets
S1 E1
S2 E2
......
Sno_of_streets Eno_of_streets
The first line of each data set contains a positive integer no_of_intersections (greater than 0 and less or equal to 120), which is the number of intersections in the town. The second line contains a positive integer no_of_streets, which is the number of streets in the town. The next no_of_streets lines, one for each street in the town, are randomly ordered and represent the town's streets. The line corresponding to street k (k <= no_of_streets) consists of two positive integers, separated by one blank: Sk (1 <= Sk <= no_of_intersections) - the number of the intersection that is the start of the street, and Ek (1 <= Ek <= no_of_intersections) - the number of the intersection that is the end of the street. Intersections are represented by integers from 1 to no_of_intersections.
There are no blank lines between consecutive sets of data. Input data are correct.

Output

The result of the program is on standard output. For each input data set the program prints on a single line, starting from the beginning of the line, one integer: the minimum number of paratroopers required to visit all the intersections in the town.

Sample Input

2433 41 32 3331 31 22 3

Sample Output

21

思路:

此题的答案为:n-最大匹配数。

代码如下:

#include 
#include
#include
#include
using namespace std;const int maxn=125;int t;int n,m;struct edge{ int to; int next;}edge[maxn*maxn];int head[maxn];int vis[maxn];int match[maxn];void add (int id,int u,int v){ edge[id].to=v; edge[id].next=head[u]; head[u]=id;}bool Find(int x){ for (int i=head[x];i!=-1;i=edge[i].next) { int u=edge[i].to; if(!vis[u]) { vis[u]=1; if(match[u]==-1||Find(match[u])) { match[u]=x; return true; } } } return false;}int algor(){ memset (match,-1,sizeof(match)); int ans=0; for (int i=1;i<=n;i++) { memset(vis,0,sizeof(vis)); if(Find(i)) ans++; } return ans;}int main(){ scanf("%d",&t); while(t--) { memset (head,-1,sizeof(head)); int cnt=0; scanf("%d%d",&n,&m); while(m--) { int x,y; scanf("%d%d",&x,&y); add(++cnt,x,y); } printf("%d\n",n-algor()); } return 0;}

 

转载地址:http://kzoen.baihongyu.com/

你可能感兴趣的文章
概率图模型学习笔记:HMM、MEMM、CRF
查看>>
新手小白从零开始开发微信小游戏
查看>>
CentOS下docker安装
查看>>
软考相关英语
查看>>
[老老实实学WCF] 第四篇 初探通信--ChannelFactory
查看>>
ASP.NET 中的 Async/Await 简介
查看>>
解决Chrome中调试JS提示“Uncaught TypeError: Cannot use 'in' operator to search for”错误信息问题
查看>>
Mac下安装OpenCV2 for Python 3.7
查看>>
阿里巴巴java规范 第一版
查看>>
Oracle无法新增数据,提示ORA-01653,无法通过8192(在表空间USERS中)扩展
查看>>
Windows下RabbitMQ安装
查看>>
RabbitMQ如何发送与接收数据
查看>>
WebApi使用redis模拟抢购场景
查看>>
【SpringMVC】使用Myeclipse创建SpringMVC项目【超详细教程】
查看>>
SpringMVC使用Jedis发布后提示java.lang.ClassNotFoundException: redis.clients.jedis.Jedis问题
查看>>
MVC Controller 链接到 API Controller 以及反向链接
查看>>
CORS解决跨域问题
查看>>
使用JWT Token实现WebApi访问验证及用户登录限制
查看>>
SpringMVC加JWT token验证
查看>>
CentOS 7 Docker中运行.net core程序
查看>>