POJ 1087 A Plug for UNIX(最大流)

                                                                                       A Plug for UNIX

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18389   Accepted: 6378

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.

Input

The input will consist of one case. The first line contains a single positive integer n (1 <= n <= 100) indicating the number of receptacles in the room. The next n lines list the receptacle types found in the room. Each receptacle type consists of a string of at most 24 alphanumeric characters. The next line contains a single positive integer m (1 <= m <= 100) indicating the number of devices you would like to plug in. Each of the next m lines lists the name of a device followed by the type of plug it uses (which is identical to the type of receptacle it requires). A device name is a string of at most 24 alphanumeric 
characters. No two devices will have exactly the same name. The plug type is separated from the device name by a space. The next line contains a single positive integer k (1 <= k <= 100) indicating the number of different varieties of adapters that are available. Each of the next k lines describes a variety of adapter, giving the type of receptacle provided by the adapter, followed by a space, followed by the type of plug.

Output

A line containing a single non-negative integer indicating the smallest number of devices that cannot be plugged in.

Sample Input

4 
A 
B 
C 
D 
5 
laptop B 
phone C 
pager B 
clock B 
comb X 
3 
B X 
X A 
X D 

Sample Output

1

Source

East Central North America 1999

题目大意:有n个插座,m个电器,给出每个电器要插的那个插座,再给出k个信息,表示a插座可以换到b插座。问最后会有多少个电器没有插座

将每一个插座先连到源点,边权为1,再将每个电器连上相应的插座,权值为1,电器与汇点相连,权值为1,对于那些能交换的插座,将他们相连,边权为inf

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
using namespace std;
const int maxn=10010;
const int maxm=10010;
const int inf=0x3f3f3f3f;
struct Node
{
    int to;
    int next;
    int capa;
}edge[maxm];
int cnt;
int node_index;
int source,sink;
int head[maxn];
int dep[maxn];
char str[maxn][30];
void init()
{
    memset(head,-1,sizeof(head));
    memset(dep,-1,sizeof(dep));
    cnt=node_index=0;
    return;
}
void add(int u,int v,int capa)
{
    edge[cnt].to=v;
    edge[cnt].capa=capa;
    edge[cnt].next=head[u];
    head[u]=cnt++;
    edge[cnt].to=u;
    edge[cnt].capa=0;
    edge[cnt].next=head[v];
    head[v]=cnt++;
    return;
}
int change(char *tmp)
{
    if(node_index==0)
    {
        strcpy(str[1],tmp);
        node_index++;
        return node_index;
    }
    else
    {
        for(int i=1;i<=node_index;i++)
        {
            if(strcmp(str[i],tmp)==0)
            {
                return i;
            }
        }
        node_index++;
        strcpy(str[node_index],tmp);
        return node_index;
    }
}
bool bfs()
{
    queue<int> que;
    memset(dep,-1,sizeof(dep));
    que.push(source);
    dep[source]=0;
    while(!que.empty())
    {
        int node=que.front();
        que.pop();
        for(int i=head[node];~i;i=edge[i].next)
        {
            int v=edge[i].to;
            if(edge[i].capa&&dep[v]==-1)
            {
                dep[v]=dep[node]+1;
                if(v==sink) return true;
                que.push(v);
            }
        }
    }
    return dep[sink]!=-1;
}
int dfs(int node,int minn)
{
    if(node==sink||minn==0)
    {
        return minn;
    }
    int r=0;
    for(int i=head[node];~i;i=edge[i].next)
    {
        int v=edge[i].to;
        if(edge[i].capa&&dep[v]==dep[node]+1)
        {
            int tmp=dfs(v,min(edge[i].capa,minn));
            if(tmp>0)
            {
                edge[i].capa-=tmp;
                edge[i^1].capa+=tmp;
                r+=tmp;
                minn-=tmp;
                if(!minn) break;
            }
        }
    }
    if(!r) dep[node]=-1;
    return r;
}
int dinic()
{
    int ans=0;
    while(bfs())
    {
        ans+=dfs(source,inf);
    }
    return ans;
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n,m,k;
    while(~scanf("%d",&n))
    {
        init();
        source=0;
        for(int i=1;i<=n;i++)
        {
            char tmp[30];
            scanf("%s",tmp);
            add(source,change(tmp),1);
        }
        scanf("%d",&m);
        sink=n+2*m+1;
        for(int i=1;i<=m;i++)
        {
            char a[30];
            char b[30];
            scanf("%s%s",a,b);
            int c=change(a);
            int d=change(b);
            add(d,c,1);
            add(c,sink,1);
        }
        scanf("%d",&k);
        for(int i=0;i<k;i++)
        {
            char a[30];
            char b[30];
            scanf("%s%s",a,b);
            int c=change(a);
            int d=change(b);
            add(d,c,inf);
        }
        cout<<m-dinic()<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37943488/article/details/81232608