比赛 |
20120613 |
评测结果 |
WWWWWWWWWWWWWWW |
题目名称 |
奶牛战争 |
最终得分 |
0 |
用户昵称 |
kaaala |
运行时间 |
0.000 s |
代码语言 |
C++ |
内存使用 |
0.00 MiB |
提交时间 |
2012-06-13 18:18:21 |
显示代码纯文本
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<deque>
using namespace std;
const int oo=~0u>>2;
struct edge
{
int t,w;
edge *next,*back;
edge(int _t,int _w,edge *_next):t(_t),w(_w),next(_next){}
}*Map[5010],*Lmap[5010];
int V,E,S,T,N,maxflow,dist[5010],sum;
char ch[5010];
deque<int>ans[1010];
void addedge(int s,int t,int w)
{
Map[s]=new edge(t,w,Map[s]);
Map[t]=new edge(s,0,Map[t]);
Map[s]->back=Map[t];
Map[t]->back=Map[s];
}
bool dinic_able()
{
deque<int>deq;
memset(dist,-1,sizeof(dist));
deq.push_back(S);
dist[S]=0;
while(!deq.empty())
{
int u=deq.front();
deq.pop_front();
for(edge *p=Map[u];p;p=p->next)
{
int w=p->w,t=p->t;
if(w&&dist[t]==-1)
{
deq.push_back(t);
dist[t]=dist[u]+1;
if(t==T)
return true;
}
}
}
return false;
}
void dinic_run()
{
int v[5010],now=1;
edge *pre[5010];
for(int i=S;i<=T;i++)
Lmap[i]=Map[i];
v[now]=S;
while(now)
{
int u=v[now];
if(u==T)
{
int del=oo,tot=0,l[1010],j=0;
for(int i=now;i>1;i--)
{
j++;
del=min(del,pre[i]->w);
if(j%3==0&&i!=2)
{
int s,t;
if(pre[i]->t%V==0)
t=V;
else
t=pre[i]->t%V;
if(pre[i]->back->t%V==0)
s=V;
else
s=pre[i]->back->t%V;
l[tot++]=t;
l[tot++]=s;
}
}
for(int i=now;i>1;i--)
{
pre[i]->w-=del;
pre[i]->back->w+=del;
if(!pre[i]->w)
now=i-1;
}
if(del!=oo)
maxflow+=del;
sum++;
for(int i=tot-1;i>=0;i--)
ans[sum].push_back(l[i]);
}
else
{
edge *&p=Lmap[u];
for(;p;p=p->next)
{
int w=p->w,t=p->t;
if(w&&dist[t]==dist[u]+1)
break;
}
if(p)
{
now++;
pre[now]=p;
v[now]=p->t;
}
else
{
dist[u]=-1;
now--;
}
}
}
}
void dinic()
{
while(dinic_able())
dinic_run();
}
int main()
{
freopen("cowwar.in","r",stdin);
freopen("cowwar.out","w",stdout);
scanf("%d%d\n",&V,&E);
S=0;
T=V*3+1;
N=T;
for(int i=1;i<=V;i++)
{
scanf("%c",&ch[i]);
if(ch[i]=='J')
addedge(S,i,1);
else
if(ch[i]=='T')
addedge(V+i,T,1);
}
for(int x,y,i=1;i<=E;i++)
{
scanf("%d%d",&x,&y);
if(ch[x]=='J'&&ch[y]=='T')
addedge(2*V+x,y,1);
if(ch[x]=='T'&&ch[y]=='J')
addedge(2*V+y,x,1);
if(ch[x]=='J'&&ch[y]=='E')
addedge(V+x,y,oo);
if(ch[x]=='E'&&ch[y]=='J')
addedge(V+y,x,oo);
if(ch[x]=='E'&&ch[y]=='T')
addedge(2*V+x,y,1);
if(ch[x]=='T'&&ch[y]=='E')
addedge(2*V+y,x,1);
}
for(int i=1;i<=V;i++)
if(ch[i]=='J')
{
addedge(i,V+i,oo);
addedge(V+i,2*V+i,1);
}
else
if(ch[i]=='T')
{
addedge(i,V+i,1);
}
else
if(ch[i]=='E')
{
addedge(i,V+i,oo);
addedge(V+i,2*V+i,1);
}
dinic();
printf("%d\n",maxflow);
for(int i=1;i<=sum;i++)
while(!ans[i].empty())
{
if(ans[i].size()!=2)
{
printf("MOVE %d ",ans[i].front());
ans[i].pop_front();
printf("%d\n",ans[i].front());
ans[i].pop_front();
}
else
{
printf("ATTACK %d ",ans[i].front());
ans[i].pop_front();
printf("%d\n",ans[i].front());
ans[i].pop_front();
}
}
return 0;
}