UVa11138 - Nuts and Bolts(二分匹配)

本文介绍了一个编程竞赛中遇到的问题——如何在限定时间内匹配最多的螺母和螺栓组合,并提供了一段示例代码及输入输出样例。通过递归算法解决最大匹配问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

One afternoon your cell phone rings; it's your cousin Jimmy.
"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

2
3 4
0 0 1 0
1 1 0 1
0 0 1 0
5 5
1 0 0 1 1
1 1 0 0 0
1 0 0 0 0
0 1 1 0 0
0 0 0 0 1 

Sample Output

Case 1: a maximum of 2 nuts and bolts can be fitted together
Case 2: a maximum of 5 nuts and bolts can be fitted together
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.util.Arrays;

public class Main implements Runnable{
	private static final boolean DEBUG = false;
	private BufferedReader cin;
	private PrintWriter cout;
	private StreamTokenizer tokenizer;
	private int bolts, nuts;
	private boolean[] vis;
	private int[] My;
	private int[][] g;
	
	private void init() 
	{
		try {
			if (DEBUG) {
				cin = new BufferedReader(new InputStreamReader(
						new FileInputStream("d:\\OJ\\uva_in.txt")));
			} else {
				cin = new BufferedReader(new InputStreamReader(System.in));
			}

			tokenizer = new StreamTokenizer(cin);
			cout = new PrintWriter(new OutputStreamWriter(System.out));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private String next() 
	{
		try {
			 tokenizer.nextToken(); 
			 if (tokenizer.ttype == StreamTokenizer.TT_EOF) return null; 
			 else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) { 
			 	return String.valueOf((int)tokenizer.nval); 
			} else return tokenizer.sval;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	private boolean input() 
	{
		bolts = Integer.parseInt(next());
		nuts = Integer.parseInt(next());
		
		g = new int[bolts][nuts];
		for (int i = 0; i < bolts; i++) {
			for (int j = 0; j < nuts; j++) {
				g[i][j] = Integer.parseInt(next());
			}
		}
		return true;
	}
	
	private boolean find(int x)
	{
		for (int y = 0; y < nuts; y++) {
			if (!vis[y] && g[x][y] != 0) {
				vis[y] = true;
				if (My[y] == -1 || find(My[y])) {
					My[y] = x;
					return true;
				}
			}
		}
		
		return false;
	}
	
	private void solve(int cas) 
	{
		My = new int[nuts];
		Arrays.fill(My, -1);
		vis = new boolean[nuts];
		
		int ans = 0;
		for (int i = 0; i < bolts; i++) {
			Arrays.fill(vis, false);
			if (find(i)) ans++;
		}
		
		cout.println("Case " + cas + ": a maximum of " + ans + " nuts and bolts can be fitted together");
		cout.flush();
	}

	public void run()
	{
		init();
		
		int t = Integer.parseInt(next());
		for (int i = 1; i <= t; i++){
			input();
			solve(i);
		}
		
	}
	
	public static void main(String[] args) 
	{
		new Thread(new Main()).start();
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值