解题报告 之 POJ1087 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

题目大意:酒店有n种插座,你有m种设备分别需要某种插座,你可以买到 k 种转换器并且你可以买无数多,并且转换器可以串联起来多重转换。(比如A->B, B->C = A->C)。问你最多有几种设备充不了电?

分析:首先明确问题其实是问最多能充多少种设备,再用m来减。第一个坑点是任意地方输入的字符串可能是出现过也可能是没出现过的。这一点很坑,意味着可能重复,也可能出现之前没出现过的字符串。具体办法是,用map看看之前是否出现过,感觉用set也行,不过数据小就用map吧。然后第一次统计拥有数量timesh,第二次统计需求数量times,其中每次读入一个字符串先判断之前是否出现过。然后给对应的timesh和times++。

统计好了之后建图,src连接每种需求插口,与第i种插口边的容量为times[i]。再将每个拥有的插座与des相连,容量为timesh[i]。然后就遇到一个问题,也是我终于搞清楚的地方,就是要不要拆点?其实这道题不用拆点,为什么呢?拆不拆点的关键在于节点之间是否可以进行状态转移。

比如这道题,插头之间是可以装换状态的,比如插头A流到插头B则意味着A插头就转化为了B插头,B插头就的的确确多了一个,所以并不需要拆点。而上一篇企鹅问题中,浮冰1->浮冰2,只是企鹅转移到了浮冰2,而浮冰2向其他浮冰转移的能力却并没有增加,即浮冰1没有状态转换到浮冰2,此时需要拆点,以限制节点浮冰2的实际数量,防止他向后面的节点多次的实际无效转移(大量的流量集中于浮冰2,使转移的量多倍于负载量,因为这个节点向后面节点流了多条路径,但他其实只能流一部分,这是一种虚假繁荣)。不过理论上能不拆点的用拆点也能过,只是负载要设置为INF就行了,其实是鸡肋。

好了上代码:

<span style="font-size:18px;">#include<iostream>
#include<queue>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<map>
#include<string>
using namespace std;const int MAXN = 810;
const int MAXM = 200000;
const int INF = 0x3f3f3f3f;struct Edge
{int from, to, cap, next;
};Edge edge[MAXM];
int level[MAXN];
int head[MAXN];
int src, des, cnt;void addedge(int from, int to, int cap)
{edge[cnt].from = from;edge[cnt].to = to;edge[cnt].cap = cap;edge[cnt].next = head[from];head[from] = cnt++;edge[cnt].from = to;edge[cnt].to = from;edge[cnt].cap = 0;edge[cnt].next = head[to];head[to] = cnt++;
}int bfs()
{queue<int> q;while (!q.empty())q.pop();memset(level, -1, sizeof level);level[src] = 0;q.push(src);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 (edge[i].cap > 0 && level[v] == -1){level[v] = level[u] + 1;q.push(v);}}}return level[des] != -1;
}int dfs(int u, int f)
{if (u == des) return f;int tem;for (int i = head[u]; i != -1; i = edge[i].next){int v = edge[i].to;if (edge[i].cap > 0 && level[v] == level[u] + 1){tem = dfs(v, min(f, edge[i].cap));if (tem > 0){edge[i].cap -= tem;edge[i ^ 1].cap += tem;return tem;}}}level[u] = -1;return 0;
}int Dinic()
{int ans = 0, tem;while (bfs()){while (tem = dfs(src, INF)){ans += tem;}}return ans;
}int main()
{int n, m, k;src = 0;des = 805;map<string, int> M;map<string, int> timesh, times;string str1, str2;memset(head, -1, sizeof head);cnt = 0;cin >> n;for (int i = 1; i <= n; i++){cin >> str1;if (M[str1] == 0)M[str1] = i;timesh[str1]++;}cin >> m;for (int i = 1; i <= m; i++){cin >> str1 >> str1;if (M[str1] == 0){M[str1] = i + 200;}times[str1]++;}for (map<string, int>::iterator c = M.begin(); c != M.end(); c++){int tem = M[c->first];if (timesh[c->first])addedge(tem, des, timesh[c->first]);if (times[c->first])addedge(src, tem, times[c->first]);}cin >> k;for (int i = 1; i <= k; i++){int tem;cin >> str1 >> str2;if (M[str1] == 0){tem = M[str1] = i + 400;}if (M[str2] == 0){tem = M[str2] = i + 600;}addedge(M[str1], M[str2], INF);}cout << m - Dinic() << endl;//system("pause");
}</span>

再听篇听力就洗洗睡啦!~

解题报告 之 POJ1087 A Plug for UNIX相关推荐

  1. 【POJ - 1087】A Plug for UNIX(建图,网络流最大流)

    题干: You are in charge of setting up the press room for the inaugural meeting of the United Nations I ...

  2. [zz][ZOJ Monthly]October 2008解题报告

    Connect4 Connect Four(Author: SONG, Yu[EZdestroyer]) 题目的背景就是Linux下的同名游戏,两个人在7*7的槽里轮流扔棋子,每次棋子都扔进某一列,棋 ...

  3. uscao 线段树成段更新操作及Lazy思想(POJ3468解题报告)

    线段树成段更新操作及Lazy思想(POJ3468解题报告) 标签: treequerybuildn2cstruct 2011-11-03 20:37 5756人阅读 评论(0) 收藏 举报  分类: ...

  4. 解题报告(十八)数论题目泛做(Codeforces 难度:2000 ~ 3000 + )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量的题解和代码,题目难度不一 ...

  5. 【解题报告系列】超高质量题单 + 题解(ACM / OI)超高质量题解

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我新写的超高质量的题解和代码,题目难度不 ...

  6. 解题报告(三)多项式求值与插值(拉格朗日插值)(ACM / OI)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量的题解和代码,题目难度不一 ...

  7. 解题报告(十三)中国剩余定理(ACM / OI)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量的题解和代码,题目难度不一 ...

  8. 解题报告(四)生成函数(ACM/ OI)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量的题解和代码,题目难度不一 ...

  9. 解题报告(八) prufer 序列与 Cayley 公式(ACM / OI)超高质量题解

    繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量题解和代码,题目难度不一定按照题号排序,我会在每道题后面加上题目难度指数(1∼51 \sim 51∼5),以模板题难度 11 ...

最新文章

  1. java的关键字与保留字
  2. iOS保存model数据(自定义Model 可以存放到本地)
  3. 【Android 安装包优化】WebP 应用 ( Android 中使用 libwebp.so 库编码 WebP 图片 )
  4. 没有找到合适的方法来重写_玻璃片价格太高?你可能没有找到合适的供应商
  5. 做项目开发你必须得掌握的知识:设计模式
  6. H264规定了三种主要档次
  7. 【动态规划】[Uva11270]Tiling Dominoes
  8. linux用户取消密码,[Linux]linux下取消用户名和密码直接登录
  9. java 模板接口开发_微信公众平台 发送模板消息(Java接口开发)
  10. tableView cell 中如果有文本框点击自动滚动不被键盘挡住
  11. 下载android平台源码
  12. 像在K8S集群中一样运行本地程序
  13. Netch游戏加速器自建(糖豆人Free就是研究的动力)
  14. 使用Phoenix连接HBASE,squirrel使用,代码连接使用Phoenix
  15. c语言字符串求n的阶乘,C语言求n的阶乘(n!)
  16. 【前端17_JS】ES 6:Let 、Const、对象冻结、解构赋值、暂时性死区 TDZ、惰性求值、模板字符串
  17. [转]设置IE背景色保护你的眼睛视力_鹤壁吧_贴吧
  18. Android视图绑定,设置控件点击事件不生效
  19. 【NLP】Pre-train,Prompt,and Predict
  20. 自己用JavaScript写出吉他和弦图生成器

热门文章

  1. 视频转换格式的软件哪个好,视频转mp4格式转换器,
  2. Coursera | Andrew Ng (01-week-1-1.2)—什么是神经网络?
  3. 西电软工操作系统实验:编译Ubuntu18.04新内核并添加系统调用(含代码以及详细分析)
  4. 5.21 使用波纹效果命令给卡通人物设计波浪发型 [Illustrator CC教程]
  5. 为什么自动驾驶汽车也必须联网
  6. 80秒的语音芯片有哪些?看这里~
  7. 通过Kmeans聚类算法分析行业价格给商品定价
  8. 解决macOS Catalina有道词典打开就崩溃的问题
  9. 2021_HUASACM_寒假周测_1 题解
  10. Dart开发(一)Windows平台环境搭建