POJ 1087 A Plug for UNIX

来源:互联网 发布:mysql修改字符集utf8 编辑:程序博客网 时间:2024/05/17 22:28

Description
You are in charge of setting up the press room for the inaugural meeting of the United Nations Internet eXecutive (UNIX), which has an international mandate to make the free flow of information and ideas on the Internet as cumbersome and bureaucratic as possible.
Since the room was designed to accommodate reporters and journalists from around the world, it is equipped with electrical receptacles to suit the different shapes of plugs and voltages used by appliances in all of the countries that existed when the room was built. Unfortunately, the room was built many years ago when reporters used very few electric and electronic devices and is equipped with only one receptacle of each type. These days, like everyone else, reporters require many such devices to do their jobs: laptops, cell phones, tape recorders, pagers, coffee pots, microwave ovens, blow dryers, curling
irons, tooth brushes, etc. Naturally, many of these devices can operate on batteries, but since the meeting is likely to be long and tedious, you want to be able to plug in as many as you can.
Before the meeting begins, you gather up all the devices that the reporters would like to use, and attempt to set them up. You notice that some of the devices use plugs for which there is no receptacle. You wonder if these devices are from countries that didn’t exist when the room was built. For some receptacles, there are several devices that use the corresponding plug. For other receptacles, there are no devices that use the corresponding plug.
In order to try to solve the problem you visit a nearby parts supply store. The store sells adapters that allow one type of plug to be used in a different type of outlet. Moreover, adapters are allowed to be plugged into other adapters. The store does not have adapters for all possible combinations of plugs and receptacles, but there is essentially an unlimited supply of the ones they do have.


【题目分析】
网络流模板题。


【代码】

#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <set>#include <map>#include <string>#include <algorithm>#include <vector>#include <iostream>#include <queue>using namespace std;#define maxn 200005#define inf 1000000000int read(){    int x=0,f=1; char ch=getchar();    while (ch<'0'||ch>'9') {if (ch=='-') f=-1; ch=getchar();}    while (ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar();}    return x*f;}int n,m,k,S=0,T=maxn-1;char nam[50],nam2[50];int fr[maxn],to[maxn],h[maxn],ne[maxn],f[maxn],mp[maxn],en=0;queue <int> q;map <string,int> list;string s,ss;int cnt=0;void add(int a,int b,int r){//  printf("%d to %d is %d\n",a,b,r);    fr[en]=a;to[en]=b;ne[en]=h[a];f[en]=r;h[a]=en++;    fr[en]=b;to[en]=a;ne[en]=h[b];f[en]=0;h[b]=en++;}bool tell(){    memset(mp,-1,sizeof mp);    q.push(S); mp[S]=0;    while (!q.empty())    {        int x=q.front(); q.pop();        for (int i=h[x];i>=0;i=ne[i])        {            if (mp[to[i]]==-1&&f[i])            {                mp[to[i]]=mp[x]+1;                q.push(to[i]);             }        }    }    if (mp[T]==-1) return false;    return true;}int zeng (int k,int now){    if (k==T) return now;    int ret=0;    for (int i=h[k];i>=0&&ret<now;i=ne[i])    {        if (mp[to[i]]==mp[k]+1&&f[i]!=0)        {            int tmp=zeng(to[i],min(now-ret,f[i]));            f[i]-=tmp; f[i^1]+=tmp; ret+=tmp;        }    }    if (!ret) mp[k]=-1;    return ret;}int main(){    memset(h,-1,sizeof h);    n=read();    for (int i=1;i<=n;++i)    {//      scanf("%s",nam);//      add(nam[0]-'A'+1+26,T,1);        cin>>s;        if (!list.count(s)) list[s]=++cnt;        add(list[s],T,1);    }    m=read();    for (int i=1;i<=m;++i)    {//      scanf("%*s%s",nam);//      add(S,nam[0]-'A'+1,1);        cin>>ss>>s;//      cout<<ss<<endl<<s;        if (!list.count(s)) list[s]=++cnt;        add(S,list[s],1);    }    k=read();    for (int i=1;i<=k;++i)    {//      scanf("%s%s",nam,nam2);//      add(nam[0]-'A'+1,nam2[0]-'A'+1,inf);        cin>>s>>ss;        if (!list.count(s)) list[s]=++cnt;        if (!list.count(ss)) list[ss]=++cnt;        add(list[s],list[ss],inf);    }//  for (int i=1;i<=26;++i)//  {//      add(i,i+26,inf);//  }    int ans=0,tmp;    while (tell())     {        while (tmp=zeng(S,inf)) ans+=tmp;//      printf("%d\n",tmp);    }    printf("%d\n",m-ans);}
0 0