POJ 【3041】Asteroids (二分图+模板)+HDU【2063】过山车(二分图+模板)
来源:互联网 发布:怎么创建淘宝店铺 编辑:程序博客网 时间:2024/05/18 19:19
Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K asteroids (1 <= K <= 10,000), which are conveniently located at the lattice points of the grid.
Fortunately, Bessie has a powerful weapon that can vaporize all the asteroids in any given row or column of the grid with a single shot.This weapon is quite expensive, so she wishes to use it sparingly.Given the location of all the asteroids in the field, find the minimum number of shots Bessie needs to fire to eliminate all of the asteroids.
Fortunately, Bessie has a powerful weapon that can vaporize all the asteroids in any given row or column of the grid with a single shot.This weapon is quite expensive, so she wishes to use it sparingly.Given the location of all the asteroids in the field, find the minimum number of shots Bessie needs to fire to eliminate all of the asteroids.
* Line 1: Two integers N and K, separated by a single space.
* Lines 2..K+1: Each line contains two space-separated integers R and C (1 <= R, C <= N) denoting the row and column coordinates of an asteroid, respectively.
* Lines 2..K+1: Each line contains two space-separated integers R and C (1 <= R, C <= N) denoting the row and column coordinates of an asteroid, respectively.
* Line 1: The integer representing the minimum number of times Bessie must shoot.
3 41 11 32 23 2
2
INPUT DETAILS:
The following diagram represents the data, where "X" is an asteroid and "." is empty space:
X.X
.X.
.X.
OUTPUT DETAILS:
Bessie may fire across row 1 to destroy the asteroids at (1,1) and (1,3), and then she may fire down column 2 to destroy the asteroids at (2,2) and (3,2).
The following diagram represents the data, where "X" is an asteroid and "." is empty space:
X.X
.X.
.X.
OUTPUT DETAILS:
Bessie may fire across row 1 to destroy the asteroids at (1,1) and (1,3), and then she may fire down column 2 to destroy the asteroids at (2,2) and (3,2).
表面上是问最少子弹数,但实际上就是求在这个二分图里的最大匹配数,代码如下,也是匈牙利算法的模板题(下面还有一道基本上一模一样的题,就是题目意思变了,但实际上解决方法一样,所以代码几乎不怎么变,所以就一起写在这篇博客里了):
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;bool fr[505][505];bool vis[505];int f[505];int N,K;bool find(int x){for(int i=1;i<=N;i++){if(fr[x][i]&&!vis[i]){vis[i]=true;if(f[i]==-1){f[i]=x;return true;}else if(find(f[i])){f[i]=x;return true;}}}return false;}int main(){while(~scanf("%d %d",&N,&K)){memset(fr,false,sizeof(fr));memset(f,-1,sizeof(f));for(int i=0;i<K;i++){int x,y;scanf("%d %d",&x,&y);fr[x][y]=true;}int ans=0;for(int i=1;i<=N;i++){memset(vis,false,sizeof(vis));if(find(i))ans++;}printf("%d\n",ans);}return 0;}
过山车
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 23759 Accepted Submission(s): 10338
Problem Description
RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了。可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做partner和她同坐。但是,每个女孩都有各自的想法,举个例子把,Rabbit只愿意和XHD或PQK做partner,Grass只愿意和linle或LL做partner,PrincessSnow愿意和水域浪子或伪酷儿做partner。考虑到经费问题,boss刘决定只让找到partner的人去坐过山车,其他的人,嘿嘿,就站在下面看着吧。聪明的Acmer,你可以帮忙算算最多有多少对组合可以坐上过山车吗?
Input
输入数据的第一行是三个整数K , M , N,分别表示可能的组合数目,女生的人数,男生的人数。0<K<=1000
1<=N 和M<=500.接下来的K行,每行有两个数,分别表示女生Ai愿意和男生Bj做partner。最后一个0结束输入。
1<=N 和M<=500.接下来的K行,每行有两个数,分别表示女生Ai愿意和男生Bj做partner。最后一个0结束输入。
Output
对于每组数据,输出一个整数,表示可以坐上过山车的最多组合数。
Sample Input
6 3 31 11 21 32 12 33 10
Sample Output
3
Author
PrincessSnow
Source
RPG专场练习赛
Recommend
lcy | We have carefully selected several similar problems for you: 1068 1083 2444 1281 1150
代码(可以看出来,基本上和上面的查不了多少):
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;bool fr[505][505];bool vis[505];int f[505];int k,m,n;bool find(int x){for(int i=1;i<=n;i++) { if(fr[x][i]&&!vis[i]) { vis[i]=true; if(f[i]==-1) { f[i]=x; return true;}else if(find(f[i])){f[i]=x;return true;}}}return false;}int main(){while(~scanf("%d",&k) && k){scanf("%d %d", &m,&n);memset(fr,false,sizeof(fr));memset(f,-1,sizeof(f));for(int i=0;i<k;i++){int x,y;scanf("%d %d",&x,&y);fr[x][y]=true;}int ans=0;for(int i=1;i<=m;i++){memset(vis,false,sizeof(vis));if(find(i)) ans++;}printf("%d\n",ans);}return 0;}
阅读全文
0 0
- POJ 【3041】Asteroids (二分图+模板)+HDU【2063】过山车(二分图+模板)
- HDU 2063 过山车 【二分图最大匹配(匈牙利模板)】
- 【HDU 2063】过山车(二分图最大匹配模板题)
- HDU 2063 过山车(二分图+匈牙利算法模板题)
- hdu2063过山车(二分图模板题)
- 匈牙利算法模板 图的二分匹配 hdu 2063 过山车
- HDU 2063 过山车(基础二分匹配)模板题
- POJ 3041 Asteroids(二分匹配模板题)
- POJ 3041 Asteroids(二分匹配模板题)
- HDU 2063 过山车 二分匹配模板题
- hdu 过山车 二分匹配模板
- hdu 2063 过山车(二分图匹配)
- hdu 2063 过山车(二分图匹配)
- HDU-#2063 过山车(二分图匹配)
- HDU - 2063 过山车(二分图匹配)
- HDU 2063 过山车(二分图)
- 【HDU】-2063-过山车(二分图)
- 【hdu 2063】过山车(二分图)
- HDU 6138 Fleet of the Eternal Throne (AC自动机)
- 数值的整数次方
- 【备忘】2017最新传智播客黑马java 32期基础就业班视频教程
- Opencv2.4.7+VS2010+win10安装配置(OpenCv 学习笔记一)
- Java多线程atomic原子变量
- POJ 【3041】Asteroids (二分图+模板)+HDU【2063】过山车(二分图+模板)
- windows系统nginx配置root绝对路径的问题
- 来套近乎啊——思维导图
- 方法重写要注意的事项
- 学习笔记TF043:TF.Learn 机器学习Estimator、DataFrame、监督器Monitors
- linux expect详解(ssh自动登录)
- 使用Gradle探索Android APP的构建过程
- 通过AMS.attachApplicationLocked()引入Binder.linkToDeath机制
- 使用vs2015打开.pro文件报错:Project ERROR: Cannot run compiler 'cl' -- 完美解决