当前位置: 代码迷 >> 综合 >> poj 2584 T-Shirt Gumbo 二分图多重匹配
  详细解决方案

poj 2584 T-Shirt Gumbo 二分图多重匹配

热度:41   发布时间:2024-01-19 06:09:44.0

题意:

有x个选手,每个选手有能接受的T恤号码,每种号码的T恤都有一定的数量,问是否有一种分配方案能让所有的选手都满意。

分析;

二分图的多重匹配问题,用匈牙利变形直接水。

代码:

//poj 2584
//sep9
#include <iostream>
using namespace std;
const int maxX=32;
const int maxY=8;
int cap[maxY],g[maxX][maxY],match[maxY][maxX];
int map[200],vis[maxY];
int x;int dfs(int x)  
{  int i,j;  for(i=1;i<=5;++i){  if(vis[i]==0&&g[x][i]==1){  vis[i]=1;  if(match[i][0]<cap[i]){  match[i][++match[i][0]]=x;  return 1;  }  else{  for(j=1;j<=match[i][0];++j)  if(dfs(match[i][j])==1){  match[i][j]=x;  return 1;     }             }     }             }  return 0;  
}  int mulmatch()  
{  int i,j;  for(i=1;i<=5;++i)  match[i][0]=0;  for(i=1;i<=x;++i){  memset(vis,0,sizeof(vis));  if(dfs(i)==0)  return 0;  }  return 1;     
}  
int main()
{char s[32],tmp[8];map['S']=1;map['M']=2;map['L']=3;map['X']=4;map['T']=5;while(scanf("%s",s)==1){if(s[0]=='E')break;int i,j;memset(g,0,sizeof(g));scanf("%d",&x);for(i=1;i<=x;++i){scanf("%s",tmp);int a=map[tmp[0]],b=map[tmp[1]];for(j=a;j<=b;++j)g[i][j]=1;}for(i=1;i<=5;++i)scanf("%d",&cap[i]);	scanf("%*s");if(mulmatch()==1)printf("T-shirts rock!\n");elseprintf("I'd rather not wear a shirt anyway...\n");}return 0;	
}