码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-08-06 15:10:40      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:


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 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,相应的插座和电器连边,容量为1,前一个插座转化为后一个插座,后一个插座向前一个插座建边,容量为无穷大,求得的最大流即为最多配对的电器。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
#define maxn 10000
#define maxm 1000000
#define INF 0x3f3f3f3f
using namespace std;

int head[maxn], cur[maxn], cnt;
int dist[maxn], vis[maxn];
int n;// 插座个数
int m;// 电器个数
int k;// 转换器个数
int sect; //超级汇点
struct node{
    int u, v, cap, flow, next;
};

node edge[maxm];
map<string, int>mp;

void init(){
    cnt = 0;
    memset(head, -1, sizeof(head));
    mp.clear();
}

void add(int u, int v, int w){
    edge[cnt] = {u, v, w, 0, head[u]};
    head[u] = cnt++;
    edge[cnt] = {v, u, 0, 0, head[v]};
    head[v] = cnt++;
}

void getmap(){
    char s[30];
    char str[30];
    int ans = 1;
    while(n--){
        scanf("%s", s);
        mp[s] = ans++;
        add(0, mp[s], 1); // 0为超级源点
    }
    scanf("%d", &m);
    for(int i = 0 ; i < m; ++i){
        scanf("%s%s", str, s);
        mp[str] = ans++;
        if(!mp[s]){
            mp[s] = ans++;
        }
        add(mp[s], mp[str], 1);
        add(mp[str], sect, 1);
    }
    scanf("%d", &k);
    while(k--){
        scanf("%s%s", s, str);
        if(!mp[s])
            mp[s] = ans++;
        if(!mp[str])
            mp[str] = ans++;
        add(mp[str], mp[s], INF);//后一个插座向前一个插座建边
    }
}

bool BFS(int st, int ed){
    queue<int>q;
    memset(vis, 0, sizeof(vis));
    memset(dist, -1, sizeof(dist));
    vis[st] = 1;
    dist[st] = 0;
    q.push(st);
    while(!q.empty()){
        int u  = q.front();
        q.pop();
        for(int i = head[u]; i != -1; i = edge[i].next){
            node E = edge[i];
            if(!vis[E.v] && E.cap > E.flow){
                vis[E.v] = 1;
                dist[E.v] = dist[u] + 1;
                if(E.v == ed) return true;
                q.push(E.v);
            }
        }
    }
    return false;
}

int DFS(int x, int ed, int a){
    if(x == ed || a == 0)
        return a;
    int flow = 0, f;
    for(int &i = cur[x]; i != -1; i = edge[i].next){
        node &E = edge[i];
        if(dist[E.v] == dist[x] + 1 && (f = DFS(E.v, ed, min(a, E.cap - E.flow))) > 0){
            E.flow += f;
            edge[i ^ 1].flow -= f;
            a -= f;
            flow += f;
            if(a == 0) break;
        }
    }
    return flow;
}

int maxflow(int st, int ed){
    int flowsum = 0;
    while(BFS(st, ed)){
        memcpy(cur, head, sizeof(head));
        flowsum += DFS(st, ed, INF);
    }
    return flowsum;
}
int main(){
    while(scanf("%d", &n)!=EOF){
        sect = n + m + k + 1;
        init();
        getmap();
        int sum;
        sum = maxflow(0 , sect);
        printf("%d\n", m - sum);
    }
    return 0;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

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

标签:

原文地址:http://blog.csdn.net/hpuhjh/article/details/47315333

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!