poj1087(最大流)

传送门:A Plug for UNIX

题意:有插座用电器和适配器,用电器有插头,适配器本身有一个插孔和插头,它的作用是可以把别的插头插入到适合该适配器插孔的适配器,然后就可以用适配器的插头接到适合的插座,相当于转换插头的作用。每个插座只能插入一个插头。3种东西都最多有100个,但是任一种适配器可以有无限个。问最后最少能剩下几个用电器不能用上电。

分析:每一个用电器每一种插头看成一点,然后用电器与源点连边,插座与汇点连边,适合的插座与用电器连边,边权都为1,最后k种适配器也就是插座直接连边,边权无限大,用建好的图跑一遍dinic即可。

#pragma comment(linker,"/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <limits.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 100000000
#define inf 0x3f3f3f3f
#define eps 1e-6
#define N 510
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PII pair<int,int>
using namespace std;
inline int read()
{
char ch=getchar();
int x=,f=;
while(ch>''||ch<''){if(ch=='-')f=-;ch=getchar();}
while(ch<=''&&ch>=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,vs,vt,tot;
int pre[N],q[N],cur[N],h[N];
struct edge
{
int v,w,next;
edge(){}
edge(int v,int w,int next):v(v),w(w),next(next){}
}e[N*N];
void addedge(int u,int v,int w)
{
e[tot]=edge(v,w,pre[u]);
pre[u]=tot++;
e[tot]=edge(u,,pre[v]);
pre[v]=tot++;
}
void init()
{
memset(pre,-,sizeof(pre));
tot=;
}
/*******************dinic************************/
int bfs()
{
int head=,tail=;
memset(h,-,sizeof(h));
q[]=vs;h[vs]=;
while(head!=tail)
{
int u=q[head++];
for(int i=pre[u];~i;i=e[i].next)
{
int v=e[i].v,w=e[i].w;
if(w&&h[v]==-)
{
h[v]=h[u]+;
q[tail++]=v;
}
}
}
return h[vt]!=-;
}
int dfs(int u,int flow)
{
if(u==vt)return flow;
int used=;
for(int i=cur[u];~i;i=e[i].next)
{
int v=e[i].v,w=e[i].w;
if(h[v]==h[u]+)
{
w=dfs(v,min(flow-used,w));
e[i].w-=w;e[i^].w+=w;
if(e[i].w)cur[u]=i;
used+=w;
if(used==flow)return flow;
}
}
if(!used)h[u]=-;
return used;
}
int dinic()
{
int res=;
while(bfs())
{
for(int i=vs;i<=vt;i++)cur[i]=pre[i];
res+=dfs(vs,inf);
}
return res;
}
/********************dinic***********************/
map<string,int>mp;
char str[][];
char s1[],s2[];
void build()
{
mp.clear();
vs=,vt=;
int num=;
for(int i=;i<=n;i++)
{
scanf("%s",s1);
if(!mp[s1])mp[s1]=++num;
addedge(vs,mp[s1],);
}
scanf("%d",&m);
for(int i=;i<=m;i++)
{
scanf("%s%s",s1,s2);
if(!mp[s1])mp[s1]=++num;
if(!mp[s2])mp[s2]=++num;
addedge(mp[s2],mp[s1],);
addedge(mp[s1],vt,);
}
scanf("%d",&k);
for(int i=;i<=k;i++)
{
scanf("%s%s",s1,s2);
if(!mp[s1])mp[s1]=++num;
if(!mp[s2])mp[s2]=++num;
addedge(mp[s2],mp[s1],inf);
}
}
int main()
{
while(scanf("%d",&n)>)
{
init();
build();
printf("%d\n",m-dinic());
}
}
上一篇:文件的存储读写,XML文件的存储与读写


下一篇:WOW.js和animate.css让页面滚动时显示动画