POJ A Plug for UNIX (最大流 建图)

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

现在有n个插头,m个用电器,每个用电器有一个自己的插口,还有k个插头转化器(将插头从一种转换为另一种),问你最少有多少个充电器充不上电主要思路就是建图,我们把源点与每个用电器建一条容量为1的边,每个用电器跟自己的插头建一条容量为1的边,在对于每个转换器的头跟尾建一条容量为inf的边,跑最大流即可为什么要对转换器建一条inf的边呢?因为不能让这条边容量的大小卡住了源点的流,所以容量尽可能大细节!!!!!输完n个插头之后还可能出现新的插头,别忘了继续加入mapmaxn开大一点
#include <string>
#include <cstdio>
#include <cstring>
#include <map>
#include <cmath>
#include <queue>
#include <iostream>
using namespace std;
#define inf 0x3f3f3f3f
const int maxn = 810;
int c[maxn][maxn];
int dep[maxn];
int cur[maxn];
int n,m,k;
map<string,int> name;
int tot;
int bfs (int s,int t)
{
    memset(dep,-1,sizeof dep);
    queue<int> q;
    while (!q.empty()) q.pop();
    dep[s] = 0;
    q.push(s);
    while (!q.empty()){
        int u=q.front();
        q.pop();
        for (int v=0;v<=801;++v){
            if (c[u][v]>0&&dep[v]==-1){
                dep[v]=dep[u]+1;
                q.push(v);
            }
        }
    }
    return dep[t]!=-1;
}
int dfs (int u,int mi,int t)
{
    if (u==t)
        return mi;
    int tmp;
    for (int &v=cur[u];v<=801;++v){
        if (c[u][v]>0&&dep[v]==dep[u]+1&&(tmp=dfs(v,min(mi,c[u][v]),t))){
            c[u][v]-=tmp;
            c[v][u]+=tmp;
            return tmp;
        }
    }
    return 0;
}
int dinic ()
{
    int ans = 0;
    int tmp;
    while (bfs(0,801)){
        while (1){
            for (int i=0;i<maxn;++i) cur[i]=0;
            tmp = dfs(0,inf,801);
            if (tmp==0)
                break;
            ans+=tmp;
        }
    }
    return ans;
}
int main()
{
    //freopen("de.txt","r",stdin);
    while(~scanf("%d",&n)){
        memset(c,0,sizeof c);
        tot=1;
        for (int i=1;i<=n;++i){
            string str;
            cin>>str;
            name[str]=i;
            c[name[str]][801]=1;
            tot++;
        }
        scanf("%d",&m);
        for (int i=1;i<=m;++i){
            string stra,strb;
            cin>>stra>>strb;
            name[stra]=tot++;
            if (!name[strb]) name[strb]=tot++;
            c[0][name[stra]]=1;
            c[name[stra]][name[strb]]=1;
        }
        scanf("%d",&k);
        for (int i=0;i<k;++i){
            string a,b;
            cin>>a>>b;
            if (!name[a]) name[a]=tot++;
            if (!name[b]) name[b]=tot++;
            c[name[a]][name[b]]=inf;
        }
        printf("%d\n",m-dinic());
    }
    return 0;
}

 
时间: 2024-10-05 23:11:01

POJ A Plug for UNIX (最大流 建图)的相关文章

POJ 1149 PIGS(最大流+建图)

题目链接:http://poj.org/problem?id=1149 题意:M个猪圈,N个顾客,每个顾客有一些的猪圈的钥匙,只能购买能打开的猪圈里的猪,而且要买一定数量的猪,每个猪圈有已知数量的猪, 但是猪圈可以重新打开,将猪的个数,重新分配,但是只能将猪往当前打开状态的猪圈里赶,以达到卖出的猪的数量最多. 思路:还是4部分,源点->猪圈->猪圈->汇点 Accepted 976K 63MS C++ 能用EK水的,当然用EK水 #include <iostream> #in

POJ 3281 Dining(最大流建图 &amp;&amp; ISAP &amp;&amp; 拆点)

题目链接:http://poj.org/problem?id=3281 努力练建图ing!!! 题意:有 N 头牛,有 F 种食物和 D 种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料. 第2行-第N+1行.是牛i 喜欢A种食物,B种饮料,及食物种类列表和饮料种类列表. 问最多能使几头牛同时享用到自己喜欢的食物和饮料.->最大流. 本题难点是建图: 思路:一般都是左边一个集合表示源点与供应相连,右边一个集合表示需求与汇点相连. 但是本题,牛作为需求仍然是一个群体,但是供

poj 3281 最大流+建图

很巧妙的思想 转自:http://www.cnblogs.com/kuangbin/archive/2012/08/21/2649850.html 本题能够想到用最大流做,那真的是太绝了.建模的方法很妙! 题意就是有N头牛,F个食物,D个饮料. N头牛每头牛有一定的喜好,只喜欢几个食物和饮料. 每个食物和饮料只能给一头牛.一头牛只能得到一个食物和饮料. 而且一头牛必须同时获得一个食物和一个饮料才能满足.问至多有多少头牛可以获得满足. 最初相当的是二分匹配.但是明显不行,因为要分配两个东西,两个东

poj3680 Intervals 区间k覆盖问题 最小费用最大流 建图巧妙

/** 题目:poj3680 Intervals 区间k覆盖问题 最小费用最大流 建图巧妙 链接:http://poj.org/problem?id=3680 题意:给定n个区间,每个区间(ai,bi),以及权值wi.选出一些区间,满足权值和最大且任何一个点不会被超过k个区间覆盖. 思路: 建图:对于每个区间(ai,bi). ai->bi,cap = 1,cost = -wi; (离散化后的ai,bi) 所有区间的端点放到数组,进行从小到大排序,去重,离散化,在数组内相邻的u端点,v端点.u->

hdu4106 区间k覆盖问题(连续m个数,最多选k个数) 最小费用最大流 建图巧妙

/** 题目:hdu4106 区间k覆盖问题(连续m个数,最多选k个数) 最小费用最大流 建图巧妙 链接:http://acm.hdu.edu.cn/showproblem.php?pid=4106 题意:给你n个数,每连续m个数,最多选k个数,问可以选的数的权值和最大多少. 思路:可以转化为区间k覆盖问题.区间k覆盖问题是每个点最多被k个区间覆盖.本题是每个区间最多选k个点. 刚好相反.我的做法有点不同其他博客那种做法.当然本质一样. 我这里的i就是原来n个数的下标,现在作为图中该数的节点编号

POJ 1087 A Plug for UNIX (最大流)

A Plug for UNIX Time Limit: 1000MS   Memory Limit: 65536K       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

POJ 1087--A Plug for UNIX【最大流dinic】

A Plug for UNIX Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14711   Accepted: 4959 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 int

POJ1087:A Plug for UNIX(最大流)

A Plug for UNIX 题目链接:https://vjudge.net/problem/POJ-1087 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 o

ZOJ1157, POJ1087,UVA 753 A Plug for UNIX (最大流)

链接 : http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26746 题目意思有点儿难描述 用一个别人描述好的. 我的建图方法:一个源点一个汇点,和所有种类的插座.输入的n个插座直接与源点相连,容量为1,m个物品输入里 记录每个插座对应的物品个数,物品数然后大于0的插座直接连到汇点,意味着最终的物品只能由这些插座流出.中间的插座转换容量都是INF  a b表示  无论多少b都可以选择转化到a. /*-------------