poj 2584
#include<iostream>
#include<fstream>
#include<queue>
using namespace std;
struct e{
int data;
int c,f;
e *next;
e *opt;
};
e edge[40];
int n,m,total;
int x[40],y[40];
int mark[300];
void add(int s,int t,int w){
e *p=new e;
p->data=t;
p->c=w;
p->f=0;
p->next=edge[s].next;
edge[s].next=p;
e *q=new e;
q->data=s;
q->c=q->f=0;
q->next=edge[t].next;
edge[t].next=q;
p->opt=q;
q->opt=p;
}
int d[40];
int build(){
int i,j,k;
memset(d,0,sizeof(d));
queue<int> q;
q.push(0);
d[0]=1;
while(!q.empty())
{
i=q.front();
q.pop();
e *p=edge[i].next;
while(p)
{
if(d[p->data]==0&&p->c>p->f)
{
d[p->data]=d[i]+1;
if(p->data==total) return 1;
q.push(p->data);
}
p=p->next;
}
}
return 0;
}
int dfs(int s,int t){
int i;
if(s==total) return t;
e *p=edge[s].next;
while(p)
{
if(d[p->data]==d[s]+1&&p->c>p->f)
{
i=dfs(p->data,min(t,p->c-p->f));
if(i!=-1)
{
p->f+=i;
p->opt->f=-1*p->f;
return i;
}
}
p=p->next;
}
return -1;
}
int b[40];
void make(){
int i,j,k;
for(i=0;i<=total;i++)
edge[i].next=0;
for(i=1;i<=n;i++)
add(0,i,1);
for(i=1;i<=n;i++)
for(j=x[i];j<=y[i];j++)
{
add(i,j+n,1);
}
for(i=n+1;i<=n+5;i++)
add(i,total,b[i-n]);
}
void solve(){
int i,j,k;
int flow=0;
while(build())
{
flow+=dfs(0,1000000);
}
if(flow==n)
cout<<"T-shirts rock!"<<endl;
else
cout<<"I'd rather not wear a shirt anyway..."<<endl;
}
void read(){
// ifstream cin("in.txt");
int i,j,k;
char c[20];
mark['S']=1;
mark['M']=2;
mark['L']=3;
mark['X']=4;
mark['T']=5;
while(cin>>c)
{
if(strcmp(c,"ENDOFINPUT")==0)
return;
cin>>n;
total=n+6;
for(i=1;i<=n;i++)
{
cin>>c;
x[i]=mark[c[0]];
y[i]=mark[c[1]];
}
for(i=1;i<=5;i++)
cin>>b[i];
cin>>c;
make();
solve();
}
}
int main(){
read();
return 0;
}
#include<fstream>
#include<queue>
using namespace std;
struct e{
int data;
int c,f;
e *next;
e *opt;
};
e edge[40];
int n,m,total;
int x[40],y[40];
int mark[300];
void add(int s,int t,int w){
e *p=new e;
p->data=t;
p->c=w;
p->f=0;
p->next=edge[s].next;
edge[s].next=p;
e *q=new e;
q->data=s;
q->c=q->f=0;
q->next=edge[t].next;
edge[t].next=q;
p->opt=q;
q->opt=p;
}
int d[40];
int build(){
int i,j,k;
memset(d,0,sizeof(d));
queue<int> q;
q.push(0);
d[0]=1;
while(!q.empty())
{
i=q.front();
q.pop();
e *p=edge[i].next;
while(p)
{
if(d[p->data]==0&&p->c>p->f)
{
d[p->data]=d[i]+1;
if(p->data==total) return 1;
q.push(p->data);
}
p=p->next;
}
}
return 0;
}
int dfs(int s,int t){
int i;
if(s==total) return t;
e *p=edge[s].next;
while(p)
{
if(d[p->data]==d[s]+1&&p->c>p->f)
{
i=dfs(p->data,min(t,p->c-p->f));
if(i!=-1)
{
p->f+=i;
p->opt->f=-1*p->f;
return i;
}
}
p=p->next;
}
return -1;
}
int b[40];
void make(){
int i,j,k;
for(i=0;i<=total;i++)
edge[i].next=0;
for(i=1;i<=n;i++)
add(0,i,1);
for(i=1;i<=n;i++)
for(j=x[i];j<=y[i];j++)
{
add(i,j+n,1);
}
for(i=n+1;i<=n+5;i++)
add(i,total,b[i-n]);
}
void solve(){
int i,j,k;
int flow=0;
while(build())
{
flow+=dfs(0,1000000);
}
if(flow==n)
cout<<"T-shirts rock!"<<endl;
else
cout<<"I'd rather not wear a shirt anyway..."<<endl;
}
void read(){
// ifstream cin("in.txt");
int i,j,k;
char c[20];
mark['S']=1;
mark['M']=2;
mark['L']=3;
mark['X']=4;
mark['T']=5;
while(cin>>c)
{
if(strcmp(c,"ENDOFINPUT")==0)
return;
cin>>n;
total=n+6;
for(i=1;i<=n;i++)
{
cin>>c;
x[i]=mark[c[0]];
y[i]=mark[c[1]];
}
for(i=1;i<=5;i++)
cin>>b[i];
cin>>c;
make();
solve();
}
}
int main(){
read();
return 0;
}
浙公网安备 33010602011771号