• A Plug for UNIX (最大流 邻接矩阵dinic)POJ


    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

    经过最近一段时间对网络流算法的学习

    发现网络流的题目 主要考察的就是建模的能力 只要对不同的问题 用不同的建图方法 那么剩下的事情就是套板子了

    本题还是比较好想的题目 

    只要对不同的插座建点

    然后源点-》插座-》转换器-》汇点

    对于电器的多少只需要在不同的插座上添加不同的边就可以了

    代码

    #include <iostream>
    #include <queue>
    #include<map>
    #include<cstring>
    #include<cstdio>
    #include<string>
    using namespace std;
    
    const int INF = 0x7fffffff;
    int level[405];
    int cnt=1;
    map<string,int> mp;
    
    struct Dinic
    {
        int c;
        int f;
    } edge[405][405];
    
    bool dinic_bfs()      //bfs方法构造层次网络
    {
        //cout<<"level"<<endl;
        queue<int> q;
        memset(level, 0, sizeof(level));
        q.push(0);
        level[0] = 1;
        int u, v;
        while (!q.empty())
        {
            u = q.front();
            q.pop();
            for (v = 1; v <= cnt; v++)
            {
                if (!level[v] && edge[u][v].c>edge[u][v].f)
                {
                    level[v] = level[u] + 1;
                    q.push(v);
                }
            }
        }
        return level[1] != 0;                //question: so it must let the sink node is the Mth?/the way of yj is give the sink node's id
    }
    
    int dinic_dfs(int u, int cp)             //use dfs to augment the flow
    {
        int tmp = cp;
        int v, t;
        if (u == 1)
            return cp;
        for (v = 1; v <= cnt&&tmp; v++)
        {
            if (level[u] + 1 == level[v])
            {
                if (edge[u][v].c>edge[u][v].f)
                {
                    t = dinic_dfs(v, min(tmp, edge[u][v].c - edge[u][v].f));
                    edge[u][v].f += t;
                    edge[v][u].f -= t;
                    tmp -= t;
                }
            }
        }
        return cp - tmp;
    }
    
    int dinic()
    {
        int sum=0, tf=0;
        while (dinic_bfs())
        {
            while (tf = dinic_dfs(0, INF))
                sum += tf;
        }
        return sum;
    }
    
    int main()
    {
    	memset(edge,0,sizeof(edge));
        int n,m,k;//1 源点 2 汇点
        string tmp,tmp1;
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            cin>>tmp;
            //cout<<tmp<<endl;
            if(mp[tmp]==0) mp[tmp]=++cnt;
            edge[0][mp[tmp]].c+=1;
        }
        cin>>m;
        for(int i=1;i<=m;i++)
        {
            cin>>tmp1>>tmp;
            if(mp[tmp]==0) mp[tmp]=++cnt;
            edge[mp[tmp]][1].c+=1;
        }
        cin>>k;
        for(int i=1;i<=k;i++)
        {
            cin>>tmp>>tmp1;
            if(mp[tmp1]==0) mp[tmp1]=++cnt;
            if(mp[tmp]==0) mp[tmp]=++cnt;
            //edge[mp[tmp]][mp[tmp1]].c=1e9;
            edge[mp[tmp1]][mp[tmp]].c=1e9;
        }
    //    cout<<" d"<<endl;
    //	for(int i=0;i<=cnt;i++)
    //	{
    //		for(int j=0;j<=cnt;j++)
    //		{
    //			printf("%9d ",edge[i][j].c);
    //		}
    //		cout<<endl;
    //	}
        int ans;
        ans=dinic();
        cout<<m-ans<<endl;
        return 0;
    }
    
  • 相关阅读:
    用nginx解决前端跨域问题
    vue中router-link的详细用法
    leetcode 148 排序链表
    leetcode 146 LRU缓存机制
    leetcode 101 对称二叉树
    leetcode 84 柱状图中最大的矩形
    leetcode76 最小覆盖子串
    C++ map, unordered_map
    python随机函数
    丑数
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852272.html
Copyright © 2020-2023  润新知