uva753 A Plug for UNIX 网络流最大流

Posted 天道酬勤007

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了uva753 A Plug for UNIX 网络流最大流相关的知识,希望对你有一定的参考价值。

C - A Plug for UNIX
    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


/**
题目:uva753 A Plug for UNIX
链接:https://vjudge.net/contest/170488#problem/C
题意:lrj入门经典P374 n个插座和m个插头,k种转换器(每种都是无限个,可以把插头转换成其他类型的插头),求最少剩多少个插头没插上插座。
思路:

记录插座类型和插头类型。

转换器可以floyd获得两个插头的关联关系。

一个插座只能有一个插头。

插头和插座建立连接,通过floyd获得的联系。

然后建立一个源点和汇点。网络流求最大流,插头数-最大流=结果。

*/
#include<iostream>
#include<cstring>
#include<vector>
#include<map>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long LL;
const int N = 410;///由于100种插座,100中插头,200种转换器。所以开这么大。否则re。
vector<int>chazuo;
vector<int>chatou;
int f[N][N];
string s, ss;
map<string,int>mp;///用数字来表示插座,插头类型。
struct Edge{
    int from, to, cap, flow;
    Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
struct EdmondsKarp
{
    int n, m;
    vector<Edge> edges;
    vector<int> G[N];
    int p[N];
    int a[N];

    void init(int n)
    {
        for(int i = 0; i <= n; i++) G[i].clear();
        edges.clear();
    }
    void AddEdge(int from,int to,int cap){
        edges.push_back((Edge){from,to,cap,0});
        edges.push_back((Edge){to,from,0,0});
        m = edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }

    int Maxflow(int s,int t)
    {
        int flow = 0;
        for(;;){
            memset(a, 0, sizeof a);
            queue<int>Q;
            Q.push(s);
            a[s] = INF;
            while(!Q.empty()){
                int x = Q.front(); Q.pop();
                for(int i = 0; i < G[x].size(); i++){
                    Edge& e = edges[G[x][i]];
                    if(!a[e.to]&&e.cap>e.flow){
                        p[e.to] = G[x][i];
                        a[e.to] = min(a[x],e.cap-e.flow);
                        Q.push(e.to);
                    }
                }
                if(a[t]) break;
            }
            if(!a[t]) break;
            for(int u = t; u != s; u = edges[p[u]].from){
                edges[p[u]].flow += a[t];
                edges[p[u]^1].flow -= a[t];
            }
            flow += a[t];
        }
        return flow;
    }
};
void Floyd(int n)
{
    for(int k = 0; k < n; k++){
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                f[i][j] |= f[i][k]&&f[k][j];
            }
        }
    }
}
int main()
{
    int  n, m, k;
    while(scanf("%d",&n)==1)
    {
        mp.clear();
        chazuo.clear();
        chatou.clear();
        int tot = 1;
        for(int i = 0; i < n; i++){
            cin>>s;
            if(mp[s]==0){
                mp[s] = tot;
                chazuo.push_back(mp[s]);
                tot++;
            }else
            {
                chazuo.push_back(mp[s]);
            }
        }
        scanf("%d",&m);
        for(int i = 0; i < m; i++){
            cin>>ss>>s;
            if(mp[s]==0){
                mp[s] = tot;
                chatou.push_back(mp[s]);
                tot++;
            }else
            {
                chatou.push_back(mp[s]);
            }
        }
        scanf("%d",&k);
        memset(f, 0, sizeof f);
        for(int i = 0; i < k; i++){
            cin>>s>>ss;
            if(mp[s]==0){
                mp[s] = tot++;
            }
            if(mp[ss]==0){
                mp[ss] = tot++;
            }
            f[mp[s]][mp[ss]] = 1;
        }
        for(int i = 1; i < tot; i++) f[i][i] = 1;
        Floyd(tot);
        EdmondsKarp ek;
        int S = 0, T = chatou.size()+chazuo.size()+1;
        ek.init(T);
        ///s -> chatou
        for(int i = 0; i < chatou.size(); i++){///不同名称的插头。
            ek.AddEdge(S,i+1,1);
        }
        ///chazuo -> t
        for(int i = 0; i < chazuo.size(); i++){///不同类型的插座。
            ek.AddEdge(chatou.size()+i+1,T,1);
        }
        ///chatou -> chazuo
        for(int i = 0; i < chatou.size(); i++){///不同名称的插头和不同类型的插座建立多对多联系。
            for(int j = 0; j < chazuo.size(); j++){///注意把名称和类型区分开。
                if(f[chatou[i]][chazuo[j]]==0) continue;
                int from, to, cap;
                from = i+1;
                to = chatou.size()+j+1;
                cap = 1;
                ek.AddEdge(from,to,cap);
            }
        }
        printf("%d\n",chatou.size()-ek.Maxflow(S,T));
    }
    return 0;
}

 



































以上是关于uva753 A Plug for UNIX 网络流最大流的主要内容,如果未能解决你的问题,请参考以下文章

uva753 A Plug for UNIX

uva753 A Plug for UNIX

UVA753:A Plug for UNIX

UVa 753 - A Plug for UNIX(最大流)

题解 UVA753 UNIX插头 A Plug for UNIX

uva 753A Plug for UNIX(图论--最大流 Dinic)