#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<limits.h>
#include<string>
#define MAX 110
using namespace std;
struct node
{
int y;
int next;
int cap;
int flow;
int b;
}a[MAX*MAX];
int h[MAX],tot=0,leve[MAX],bcap[MAX];
void addedge(int i,int j, int c)
{
a[tot].flow=0;
a[tot].y=j;
a[tot].cap=c;
a[tot].next=h[i];
h[i]=tot++;
a[tot].flow=0;
a[tot].y=i;
a[tot].cap=0;
a[tot].next=h[j];
h[j]=tot++;
}
int getno(char c)
{
switch(c)
{
case 'S':return 1;
case 'M':return 2;
case 'L':return 3;
case 'X':return 4;
case 'T':return 5;
}
}
void addedge(int i,int j,int b, int c)
{
a[tot].flow=0;
a[tot].b=b;
a[tot].y=j;
a[tot].cap=c;
a[tot].next=h[i];
h[i]=tot++;
a[tot].flow=0;
a[tot].b=-b;
a[tot].y=i;
a[tot].cap=0;
a[tot].next=h[j];
h[j]=tot++;
}
bool bfs(int s,int t)
{
int u,v,c,i;
queue<int> q;
memset(leve,-1,sizeof(leve));
leve[s]=0;
q.push(s);
while(!q.empty())
{
u=q.front();
q.pop();
i=h[u];
while(i!=-1)
{
v=a[i].y;
if(a[i].cap>0&&leve[v]==-1)
{
q.push(v);
leve[v]=leve[u]+1;
}
i=a[i].next;
}
}
return leve[t]!=-1;
}
int dinic(int s,int t)
{
int stack[MAX],last[MAX];
int u,v,top=1,minf,i,temp,c,edge,sum=0;
while(bfs(s,t))
{
top=1;
stack[top]=s;
for(i=s;i<=t;i++)
{
last[i]=h[i];
}
while(top)
{
u=stack[top];
if(u==t)
{
minf=INT_MAX;
for(i=1;i<top;i++)
{
edge=last[stack[i]];
if(minf>a[edge].cap)
{
minf=a[edge].cap;
temp=i;
}
}
for(i=1;i<top;i++)
{
edge=last[stack[i]];
a[edge].cap-=minf;
a[edge].flow+=minf;
a[edge^1].cap+=minf;
a[edge^1].flow-=minf;
}
sum+=minf;
top=temp;
continue;
}
edge=last[u];
while(edge!=-1)
{
v=a[edge].y;
c=a[edge].cap;
if(c>0&&leve[u]+1==leve[v])
{
stack[++top]=v;
break;
}
edge=a[edge].next;
}
last[u]=edge;
if(edge==-1)
{
top--;
if(top!=0)
last[stack[top]]=a[last[stack[top]]].next;
}
}
}
return sum;
}
int main()
{
string str;
int i,j,k,u,v,m;
while(cin>>str)
{
if(str=="ENDOFINPUT")
break;
memset(h,-1,sizeof(h));
tot=0;
scanf("%d",&m);
for(i=1;i<=m;i++)
{
cin>>str;
addedge(0,i+5,1);
for(j=getno(str[0]);j<=getno(str[1]);j++)
{
addedge(i+5,j,1);
}
}
for(i=1;i<=5;i++)
{
scanf("%d",&k);
addedge(i,m+6,k);
}
cin>>str;
int sum=dinic(0,m+6);
if(m==sum)
printf("T-shirts rock!\n");
else
printf("I'd rather not wear a shirt anyway...\n");
}
}
poj2584
最新推荐文章于 2016-10-03 23:03:42 发布