POJ编程6----A Plug for UNIX

POJ编程6----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

算法思路

流图相关概念:https://blog.csdn.net/zitian246/article/details/76218020
https://www.cnblogs.com/GorgeousBankarian/p/12220835.html
Dinic算法:https://blog.csdn.net/yjr3426619/article/details/82808303
https://www.cnblogs.com/evlpsrfc/p/11280248.html
参考:https://www.cnblogs.com/agenthtb/p/7468836.html
最大流问题
求出一个可行流量使得从单源点到单汇点的流网络中流量最大。
增广路:一条能够增加源点到汇点的流量的路径
残余网络=容量网络-流量网络
关键边:一条增广路上流量=容量的边,即恰好能被流量填满的边。
最大流=最小割,最小割问题:把边权看作割去某条边代价,找到一种割边的方案使得源点s和汇点t不连通所付出的代价最小。
Dinic算法
层级网络:原网络指定一个源点s一个汇点t,每一个顶点u赋予一个level[u]=u与s的最短距离,层级网络则是包含所有满足level[v]=level[u]+1的边的网络。
算法思想:循环使用BFS来查找增广路;若在残余网络中没有s到t的增广路,流量flow已经最大。沿着多条层级逐一递增的路径传送流量。当层级网络无法构建时,即残留网络已经无法到达汇点时,说明当前流量已经最大。

本题建图,源点连接电器,权值为1,电器连接自己的插头,权值为1,适配器的插头连接插头,权值为INF,插座连接汇点,权值为1。建好之后,用dinic算法求原点到汇点的最大流。用m减去最大流值,就是无法充电的设备的最少值。

代码示例

程序注释比较清晰啦

#include <iostream>
#include <map>
#include <string>
#include <queue>
using namespace std;

const int INF = 0x3f3f3f3f;
const int MAXN = 1000;
const int MAXM = 1000;
map<string, int> name;

struct Edge 
{
    int to, next, cap;//对应一条边的终点,下一条边,边的容量
    Edge(int a = 0, int b = 0, int c = 0) :to(a), next(b), cap(c) {}
}edge[MAXM];

int tot;//边的序号
int head[MAXN];
int dep[MAXN];
int cur[MAXN];
//cur为当前弧优化,dep存储分层图中每个点的层数,head建邻接表

inline void init() 
{
    tot = 0;
    memset(head, -1, sizeof(head));
}

inline void add_edge(int u, int v, int w) //对应起点、终点、权重
{
    edge[tot] = Edge(v, head[u], w);
    head[u] = tot;
    tot++;
    edge[tot] = Edge(u, head[v], 0);
    head[v] = tot;
    tot++;
}

bool bfs(int s, int t) //bfs分层找增广路,建立层次图
{
    memset(dep, -1, sizeof(dep));
    dep[s] = 0;
    queue<int> Q;
    Q.push(s);
    while (!Q.empty())
    {
        int u = Q.front();
        Q.pop();
        for (int i = head[u]; i != -1; i = edge[i].next)
        {
            int v = edge[i].to;
            if (dep[v] == -1 && edge[i].cap > 0) //没有被标记且有容量
            {
                dep[v] = dep[u] + 1;
                Q.push(v);
                if (v == t)
                    break;
            }
        }
    }
    return dep[t] != -1;
}

int dfs(int u, int t, int r) //u为当前节点,t汇点,r到达此点的流量
{
    if (u == t || r == 0) return r;
    int flow(0);//点u流出的流量
    int f;
    //i为cur[u]的引用-->cur[u]随着i改变
    //保证了cur[u]为第一个有用的边
    for (int& i = cur[u]; i != -1; i = edge[i].next)
    {
        int v = edge[i].to;
        if (dep[v] == dep[u] + 1 && (f = dfs(v, t, min(r, edge[i].cap))) > 0) //f为点u经边e[i]流向v的流量
        {
            edge[i].cap -= f;
            edge[i ^ 1].cap += f;//反向边,用了下异或1的小技巧
            flow += f;
            r -= f;//剩余的流入的流量
            if (r == 0)
                break;
        }
    }
    return flow;
}
int dinic(int s, int t) 
{
    int maxflow(0);
    while (bfs(s, t)) //BFS建立层次网络,若能够建立,则存在增广路能够增加流量
    {
        memcpy(cur, head, sizeof(head));
        maxflow += dfs(s, t, INF);
    }
    return maxflow;//返回最大流
}

int main()
{
    int n, m, k;
    init();
    cin >> n;
    int sp = 0, tp = 500;
    int t = 1;
    for (int i = 0; i < n; i++)//进行建图
    {
        string str;
        cin >> str;
        if (name[str] == 0)
        {
            name[str] = t;
            add_edge(t, tp, 1);//插座和汇连接
            t++;
        }        
    }
    cin >> m;
    for (int i = 0; i < m; i++)
    {
        string str;
        cin >> str;
        cin >> str;
        add_edge(sp, t, 1);//源和用电设备连接
        t++;
        if (name[str] == 0)
        {
            name[str] = t;
            add_edge(t - 1, t, 1);//用电设备和插头连接
            t++;
        }
        else 
            add_edge(t - 1, name[str], 1);
    }
    cin >> k;
    for (int i = 0; i < k; i++)
    {
        string str1, str2;
        cin >> str1 >> str2;
        if (name[str1] == 0)
        {
            name[str1] = t;
            t++;
        }
        if (name[str2] == 0)
        {
            name[str2] = t;
            t++;
        }
        add_edge(name[str1], name[str2], INF);//适配器的插头相连
    }
    t++;
    cout << m - dinic(sp, tp) << endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值