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 #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 } } } /* 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 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 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
Problem Setter: Joachim Wulff, Special Thanks: Igor Naverniouk
PS. If you are a Pascal programmer and you don't understand C, then you're in trouble: in 2006 it was the last time that Pascal was allowed at the ICPC World Finals and other programming contests will probably follow soon. Better learn C or one of its derivatives!
bipartite graph
#include <stdio.h>
#include<cstring>
#define REP(i, b, n) for (int i = b; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define MAXN 500
/* global copy of the input data */
int nuts,bolts;
int fits[MAXN][MAXN],xM[MAXN],yM[MAXN], cases,caseno;
bool trav[MAXN];
void read_input_data(){
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]);
}
bool search(int a){
rep(i,nuts){
if(fits[a][i]&&!trav[i]){
trav[i]=1;
if(yM[i]<0||search(yM[i])){
xM[a]=i,yM[i]=a;
return 1;
}
}
}
return 0;
}
void ans(){
int res=0;
memset(xM,-1,sizeof(xM));
memset(yM,-1,sizeof(yM));
rep(i,bolts){
memset(trav,0,sizeof(trav));
if(xM[i]<0&&search(i))res++;
}
printf("Case %d: ",caseno);
printf("a maximum of %d nuts and bolts can be fitted together\n",res);
}
int main(){
scanf("%d",&cases);
for(caseno=1;caseno<=cases;caseno++){
read_input_data();
ans();
}
return 0;
}
沒有留言:
張貼留言