POJ 1087 -- 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

AC

  • 建图:

    1. 将源点和电器需要插座建边,权值为需要的个数
    2. 将插座转换器建边,权值为INF(商店可以提供无数个)
    3. 将已有的插座和汇点相连,权值为该种类插座的数目
  • 注意:

    1. 如果用邻接矩阵写,矩阵最小开到400,因为插座的种类最坏的情况是400种,已存在的插座100种,需要的插座100种,转换器100 + 100 = 200 种,所以最多400种
  • 代码后面有极限数据

#include <iostream>
#include <stdio.h>
#include <map>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#define N 300005
#include <cstring>
#define ll long long
#define P pair<int, int>
#define mk make_pair
using namespace std;

int pre[405], a[405][405];
bool vis[405];
int inf = 0x3f3f3f3f;
int n, m, k;
// EK最大流 
bool bfs(int s, int e) {
    memset(pre, -1, sizeof(pre));
    memset(vis, false, sizeof(vis));
    vis[s] = true;
    pre[s] = s;
    queue<int> que;
    que.push(s);
    while (!que.empty()) {
        int t = que.front();
        que.pop();
        for (int i = 0; i <= e; ++i) {
            if (vis[i] || a[t][i] == 0) continue;
            vis[i] = true;
            pre[i] = t;
            if (i == e) return true;
            que.push(i);
        }
    }
    return false;
}
ll solve(int s, int e) {
    ll sum = 0;
    while (bfs(s, e)) {
        int MIN = inf;
        for (int i = e; i != s; i = pre[i]) {
            MIN = min(MIN, a[pre[i]][i]);
        }
        for (int i = e; i != s; i = pre[i]) {
            a[pre[i]][i] -= MIN;
            a[i][pre[i]] += MIN;
        }
        sum += MIN;
    }
    return sum;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);

    while (cin >> n) {
        memset(a, 0, sizeof(a));
        // 统计现有的插座种类和数目 
        map<string, int> mp1;
        // 统计电器插头种类和数目 
        map<string, int> mp2;
        // 对所有插座和插头进行标记 
        map<string, int> mp;
        int now = 1;
        // 读入插座 
        for (int i = 0; i < n; ++i) {
            string s;
            cin >> s;
            mp1[s]++;
            if (mp[s] == 0) mp[s] = now++;
        }
        // 读入插头 
        cin >> m;
        for (int i = 0; i < m; ++i) {
            string s;
            cin >> s >> s;
            mp2[s]++; 
            if (mp[s] == 0) mp[s] = now++;
        }
        // 将源点和电器需要的插座相连,权值为需要的个数 
        map<string, int>::iterator it;
        for (it = mp2.begin(); it != mp2.end(); ++it) {
            a[0][mp[it->first]] = it->second;
        }
        // 读入转换器 
        cin >> k;
        for (int i = 0; i < k; ++i) {
            string s1, s2;
            cin >> s1 >> s2;
            if (mp[s1] == 0)    mp[s1] = now++;
            if (mp[s2] == 0)    mp[s2] = now++;
            // 因为商店可以提供无数个,权值设为 inf 
            a[mp[s1]][mp[s2]] = inf;
        }
        // 将现有的插座和汇点相连 
        for (it = mp1.begin(); it != mp1.end(); ++it) {
            a[mp[it->first]][401] = it->second;
        }
        // 插座和插头最多400种,已有的100种,需要的100种,转换器100 + 100 
        ll ans = solve(0, 401);
        printf("%d\n", m - ans);
    }
    return 0;
}
  • 数据
100
a0
a1
a2
a3
a4
a5
a6
a7
a8
a9
a10
a11
a12
a13
a14
a15
a16
a17
a18
a19
a20
a21
a22
a23
a24
a25
a26
a27
a28
a29
a30
a31
a32
a33
a34
a35
a36
a37
a38
a39
a40
a41
a42
a43
a44
a45
a46
a47
a48
a49
a50
a51
a52
a53
a54
a55
a56
a57
a58
a59
a60
a61
a62
a63
a64
a65
a66
a67
a68
a69
a70
a71
a72
a73
a74
a75
a76
a77
a78
a79
a80
a81
a82
a83
a84
a85
a86
a87
a88
a89
a90
a91
a92
a93
a94
a95
a96
a97
a98
a99
100
b0 c0
b1 c1
b2 c2
b3 c3
b4 c4
b5 c5
b6 c6
b7 c7
b8 c8
b9 c9
b10 c10
b11 c11
b12 c12
b13 c13
b14 c14
b15 c15
b16 c16
b17 c17
b18 c18
b19 c19
b20 c20
b21 c21
b22 c22
b23 c23
b24 c24
b25 c25
b26 c26
b27 c27
b28 c28
b29 c29
b30 c30
b31 c31
b32 c32
b33 c33
b34 c34
b35 c35
b36 c36
b37 c37
b38 c38
b39 c39
b40 c40
b41 c41
b42 c42
b43 c43
b44 c44
b45 c45
b46 c46
b47 c47
b48 c48
b49 c49
b50 c50
b51 c51
b52 c52
b53 c53
b54 c54
b55 c55
b56 c56
b57 c57
b58 c58
b59 c59
b60 c60
b61 c61
b62 c62
b63 c63
b64 c64
b65 c65
b66 c66
b67 c67
b68 c68
b69 c69
b70 c70
b71 c71
b72 c72
b73 c73
b74 c74
b75 c75
b76 c76
b77 c77
b78 c78
b79 c79
b80 c80
b81 c81
b82 c82
b83 c83
b84 c84
b85 c85
b86 c86
b87 c87
b88 c88
b89 c89
b90 c90
b91 c91
b92 c92
b93 c93
b94 c94
b95 c95
b96 c96
b97 c97
b98 c98
b99 c99
100
d0 e0
d1 e1
d2 e2
d3 e3
d4 e4
d5 e5
d6 e6
d7 e7
d8 e8
d9 e9
d10 e10
d11 e11
d12 e12
d13 e13
d14 e14
d15 e15
d16 e16
d17 e17
d18 e18
d19 e19
d20 e20
d21 e21
d22 e22
d23 e23
d24 e24
d25 e25
d26 e26
d27 e27
d28 e28
d29 e29
d30 e30
d31 e31
d32 e32
d33 e33
d34 e34
d35 e35
d36 e36
d37 e37
d38 e38
d39 e39
d40 e40
d41 e41
d42 e42
d43 e43
d44 e44
d45 e45
d46 e46
d47 e47
d48 e48
d49 e49
d50 e50
d51 e51
d52 e52
d53 e53
d54 e54
d55 e55
d56 e56
d57 e57
d58 e58
d59 e59
d60 e60
d61 e61
d62 e62
d63 e63
d64 e64
d65 e65
d66 e66
d67 e67
d68 e68
d69 e69
d70 e70
d71 e71
d72 e72
d73 e73
d74 e74
d75 e75
d76 e76
d77 e77
d78 e78
d79 e79
d80 e80
d81 e81
d82 e82
d83 e83
d84 e84
d85 e85
d86 e86
d87 e87
d88 e88
d89 e89
d90 e90
d91 e91
d92 e92
d93 e93
d94 e94
d95 e95
d96 e96
d97 e97
d98 e98
d99 e99

猜你喜欢

转载自blog.csdn.net/henuyh/article/details/81808094