uva 11138 - Nuts and Bolts(二分匹配)
来源:互联网 发布:python 全局变量 编辑:程序博客网 时间:2024/04/29 11:42
Problem J
Nuts 'n' Bolts
"Hi Cuz," he says, "I need your help and I need it fast. I'm in the middle of a programming contest and however hard I try, I can't get one problem to finish within the two second time limit."
"Hmm... well..., isn't that a bit illegal?", you try to say to him. But he rattles on.
"I just snook out of the contest room and managed to send you my code and the sample I/O by email", he continues without pausing. "I will check my mail again in an hour, so please make it work for me."
"What about the problem description?", you ask.
"Can't do", he says, "Zoroman the Head Judge is already on my tail, so I got to go. Bye, ... and, eh, thanks."
Are you going to help him?
Jimmy's Code
#include <stdio.h>#define MAX_BOLTS 500
#define MAX_NUTS 500
/* global copy of the input data */
int nuts,bolts;
int fits[MAX_BOLTS][MAX_NUTS];
void read_input_data(void){
int n,b;
scanf("%d%d",&bolts,&nuts);
for(b=0;b<bolts;b++){
for(n=0;n<nuts;n++){
scanf("%d",&fits[b][n]);
}
}
}
/* data used to match nuts with bolts */
int nut_used[MAX_NUTS];
int bestmatched;
void init_match(void){
int n;
bestmatched=0;
for(n=0;n<nuts;n++) nut_used[n]=0;
}
void match_bolt(int boltno, int matched){
int n;
if(boltno==bolts){
if(matched>bestmatched) bestmatched=matched;
return;
}
/* don't match this bolt */
match_bolt(boltno+1,matched);
/* match with all unused nuts that fit this bolt */
for(n=0;n<nuts;n++) if(!nut_used[n] && fits[boltno][n]){
nut_used[n]=1;
match_bolt(boltno+1,matched+1);
nut_used[n]=0;
}
}
int main(){
int cases,caseno;
scanf("%d",&cases);
for(caseno=1;caseno<=cases;caseno++){
read_input_data();
init_match();
match_bolt(0,0);
printf("Case %d: ",caseno);
printf("a maximum of %d nuts and bolts ",bestmatched);
printf("can be fitted together\n");
}
return 0;
}
This is the code that Jimmy sent you by email.
A plain-text version can be found here.
Sample Input
23 40 0 1 01 1 0 10 0 1 05 51 0 0 1 11 1 0 0 01 0 0 0 00 1 1 0 00 0 0 0 1
Sample Output
Case 1: a maximum of 2 nuts and bolts can be fitted togetherCase 2: a maximum of 5 nuts and bolts can be fitted together
本题题意:
行代表botle,列代表nut,每个botle只能放一个nut,1代表可放,0代表不可放,问最大匹配数?
#include <iostream>#include <cstdio>#include <vector>using namespace std;const int maxn = 510;vector<int> mp[maxn];int father[maxn] , vis[maxn] , v , B , N , ans;void initial(){ for(int i = 0; i < maxn; i++){ father[i] = -1; vis[i] = 0; mp[i].clear(); } v = 1; ans = 0;}void readcase(){ scanf("%d%d" , &B , &N); for(int i = 0; i < B; i++){ for(int j = 0; j < N; j++){ int flag; scanf("%d" , &flag); if(flag) mp[i].push_back(j); } }}bool Hungry(int f){ for(int k = 0; k < mp[f].size(); k++){ int son = mp[f][k];if(vis[son] != v){vis[son] = v;if(father[son] == -1 || Hungry(father[son])){father[son] = f;return true;}} } return false;}void computing(){ for(int i = 0; i < B; i++){ if(Hungry(i)) ans++; v++; } printf("a maximum of %d nuts and bolts can be fitted together\n" , ans);}int main(){ int t; scanf("%d" , &t); for(int i = 1; i <= t; i++){ initial(); readcase(); printf("Case %d: " , i); computing(); } return 0;}
0 0
- uva 11138 - Nuts and Bolts(二分匹配)
- UVa11138 - Nuts and Bolts(二分匹配)
- [LintCode] Nuts and Bolts
- Matching Nuts and Bolts 螺母螺帽的匹配
- R Programming: Part 1 - Nuts and Bolts
- Hardware Virtualization: the Nuts and Bolts / EPT / VPID
- 螺丝和螺帽Nuts and bolts 《算法》2.3.15
- 事务处理的nuts和bolts
- 《算法》第二章——Nuts and bolts(螺丝和螺帽)
- 题目:nuts 和 bolts 的问题
- lintcode(399)Nuts 和 Bolts 的问题
- Uva-10944-Nuts for nuts..
- 399.Nuts & Bolts Problem-Nuts 和 Bolts 的问题(中等题)
- UVa 二分图匹配 Examples
- UVa 二分图匹配 Biginners
- uva 11045(二分图匹配)
- uva 12083 二分图匹配
- uva 10122 Mysterious Mountain and ZOJ 1231 Mysterious Mountain (二分+二部图最大匹配)
- DatePicker的使用(一)
- Cassandra源码学习:数据文件分布
- windows下使用git管理github项目
- CxImage图像处理库VS2010编译和调用(静态库和动态库调用)
- 网络编程
- uva 11138 - Nuts and Bolts(二分匹配)
- Android 图片Bitmap保存到内存卡
- CodeForces Round #191 (327C) - Magic Five 乘法逆元与费马小定理
- 最大子段和几种算法
- 8款唯美设计的HTML5/CSS3应用
- JAVA学习脚印7 : Object类的四个关键方法
- touch命令
- CF 86D Powerful array 分块算法入门,n*sqrt(n)
- 字符旋转