POJ 1422 Air Raid(二分图匹配+最小路径覆盖+匈牙利算法)
来源:互联网 发布:cad软件下载 编辑:程序博客网 时间:2024/05/19 17:52
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.
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.
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.
2433 41 32 3331 31 22 3
21
题解:
题意:
有n个节点,给m个数据,x,y表示点x,y有一条有向边,然后可以从任意位置空降伞,伞兵只会走之前没有伞兵到过的地方,问最少多少个伞兵可以覆盖全点
思路:
这题如果不是二分匹配的专题我恐怕就用搜索做了。。。想了一会不知道如何用二分匹配。。然后就搜博客,以下是我的理解:问题可以转化为求最多解有多少个每两个点之间只有一个入度一个出度(也就是伞兵一路走下来不走回头路或者别人走过的路)然后用总点数减去能建成多少个这样的路的数目就是答案,感觉二分匹配就是建模很难,代码以一下就出来了,这里用匈牙利算法邻接表写法0ms,邻接矩阵写法是15ms
代码:
#include<algorithm>#include<iostream>#include<cstring>#include<stdio.h>#include<math.h>#include<string>#include<stdio.h>#include<queue>#include<stack>#include<map>#include<vector>#include<deque>using namespace std;#define lson k*2#define rson k*2+1#define M (t[k].l+t[k].r)/2#define INF 1008611111int used[155];int vis[155];int num[155];int p[155][155],n,m;int find(int u)//匈牙利算法邻接表写法{ int i,v; for(i=0;i<num[u];i++) { v=p[u][i]; if(!vis[v]) { vis[v]=1; if(!used[v]||find(used[v])) { used[v]=u; return 1; } } } return 0;}int main(){ int i,j,k,test,x,y; scanf("%d",&test); while(test--) { scanf("%d%d",&n,&m); memset(num,0,sizeof(num)); for(i=0;i<m;i++) { scanf("%d%d",&x,&y); p[x][num[x]]=y; num[x]++; } int ans=0; memset(used,0,sizeof(used)); for(i=1;i<=n;i++) { memset(vis,0,sizeof(vis)); if(find(i)) ans++; } printf("%d\n",n-ans); } return 0;}
阅读全文
0 0
- POJ 1422 Air Raid(二分图匹配+最小路径覆盖+匈牙利算法)
- POJ 1422 Air Raid(匈牙利算法—最小路径覆盖)
- POJ 1422 Air Raid 最小路径覆盖(二分图匹配)
- poj 1422 Air Raid(最小路径覆盖 + 二分图最大匹配)
- POJ 1422Air Raid(二分图最大匹配之最小路径覆盖)
- POJ 1422 Air Raid(二分图匹配最小路径覆盖)
- HDU 1151 Air Raid(匈牙利算法 二分图的最小路径覆盖 )
- POJ 1422 Air Raid (二分图最小点集覆盖 匈牙利算法)
- 最小路径覆盖(二分图最大匹配):Air Raid
- POJ-1422-Air Raid-求最小路径覆盖(匈牙利算法)
- POJ-1422-Air Raid (二分图 最小路径覆盖)
- 【二分图|最小路径覆盖】POJ-1422 Air Raid
- hdu 1151 &&poj 1422 Air Raid (二分图匹配 最小路径覆盖)
- poj 1422 Air Raid 最小路径覆盖 二分图匹配 解题报告
- HDU 1151 Air Raid 最小路径覆盖,二分图匹配匈牙利算法(邻接表存关系)处理有向图
- hdu 1151 Air Raid(二分图匹配--最小路径覆盖)
- poj 1422 Air Raid (二分图+有向图最小不相交路径覆盖)
- POJ 1422-Air Raid(二分图_最小路径覆盖)
- 在百度搜了几个eclipse配置Tomcat服务的教程
- Redis 连接命令
- JAVA设计模式初探之装饰者模式
- MapReduce多路径输入与多文件输出
- 欢迎使用CSDN-markdown编辑器
- POJ 1422 Air Raid(二分图匹配+最小路径覆盖+匈牙利算法)
- 8.15(一个难忘的日子)
- LeetCode题-coins in a line
- redis 事务 理解和使用
- android开发键盘状态监听
- shell之sed
- XYNUOJ 确定比赛名次(拓补)
- Corn Fields POJ
- Libevent-2.1.8源码分析——日志消息