当做模板来用吧..
#include<stdio.h>
#include<string.h>
int n1,n2,m,ans;
int result[1005];
int state[1005];
int maps[1005][1005];
void init()
{
int t1,t2,job;
int i;
memset(maps,0,sizeof(maps));
memset(result,0,sizeof(result));
ans = 0;
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&job,&t1,&t2);
if(t1&&t2)
maps[t1][t2] = 1;
}
return ;
}
int find(int a)
{
int i;
for(i=1;i<=m;i++)
{
if(maps[a][i] && !state[i])
{
state[i] = 1;
if(result[i]==0 || find(result[i]))
{
result[i]=a;
return 1;
}
}
}
return 0;
}
int main()
{
int i;
while(1)
{
scanf("%d",&n1);if(n1==0)break;
scanf("%d%d",&n2,&m);
init();
for(i=1;i<=n1;i++)
{
memset(state,0,sizeof(state));
if(find(i))ans++;
}
printf("%d\n",ans);
}
return 0;
}
其实1151和1150差不多..顺便把1151代码也贴出来.
#include<stdio.h>
#include<string.h>
int n2,m,ans;
int xresult[201];
int yresult[201];
int state[201];
int maps[126][126];
void init()
{
int t1,t2;
int i;
memset(maps,0,sizeof(maps));
ans = 0;
for(i=1;i<=m;i++)
{
scanf("%d%d",&t1,&t2);
maps[t1][t2] = 1;
}
return ;
}
int find(int a)
{
int i;
for(i=1;i<=n2;i++)
{
if(maps[a][i] && !state[i])
{
state[i] = 1;
if(yresult[i]==0 || find(yresult[i]))
{
xresult[a]=i;
yresult[i]=a;
return 1;
}
}
}
return 0;
}
int main()
{
int i;
int n;
while(scanf("%d",&n)!=EOF)
{
while(n--)
{
scanf("%d%d",&n2,&m);
init();
memset(xresult,0,sizeof(xresult));
memset(yresult,0,sizeof(yresult));
for(i=1;i<=n2;i++)
{
if(!xresult[i])
{
memset(state,0,sizeof(state));
if(find(i))ans++;
}
}
printf("%d\n",n2-ans);
}
}
return 0;
}