poj2584

#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");

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值