POJ 1087
来源:互联网 发布:lol比赛数据查询 编辑:程序博客网 时间:2024/05/19 16:29
A Plug for UNIX
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 17824 Accepted: 6150
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.
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.
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
Source
East Central North America 1999
[Submit] [Go Back] [Status] [Discuss]
题意:给出设备和插座,每个插座只有一个位置和插头,每个设备都只有一个插头,且只能插对应的插座,最后有k种适配器可以选择,可以将插头转换为另外一种插头;
思路:最大流,这里我用Dinic
Ac code:
#include <cstdio>#include <algorithm>#include <cstring>#include <vector>#include <queue>#include <map>#include <iostream>using namespace std;const int INF =0x3f3f3f3f;const int N=1000+50;map<string ,int> map1;struct Edge { int from,to,cap,flow;};vector<Edge> edge;vector<int> G[N];int pre[N];//记录路径int vis[N];//起点到i的可改进量bool bfs(int s,int t) { memset(vis,0,sizeof(vis)); queue<int>q; q.push(s); vis[s]=INF; while(!q.empty()) {//从源点出发寻找 int x=q.front(); q.pop(); int len=G[x].size(); for(int i=0; i<len; i++) { Edge& e=edge[G[x][i]]; if(!vis[e.to]&&e.cap>e.flow) { vis[e.to]=min(vis[x],e.cap-e.flow); pre[e.to]=G[x][i]; q.push(e.to); } } } return vis[t];}int max_flow(int s,int t) { int flow=0; while(bfs(s,t)) { for(int u=t; u!=s; u=edge[pre[u]].from) { edge[pre[u]].flow+=vis[t]; edge[pre[u]^1].flow-=vis[t]; } flow+=vis[t]; } return flow;}void add_edge(int from,int to,int cap) { edge.push_back((Edge) {from,to,cap,0}); edge.push_back((Edge) {to,from,0,0}); int m=edge.size(); G[from].push_back(m-2); G[to].push_back(m-1);}void init(int n) { for(int i=0; i<=n; i++) { G[i].clear(); } edge.clear(); memset(pre,0,sizeof pre); // memset(vis,0,sizeof vis);}int main() { //ios::sync_with_stdio(false); int n; while(~scanf("%d",&n)){ int st=0; int ter=N-1;//vis只开到N-1 int plugNum=1; init(n*2); char s[30]; while(n--){ scanf("%s",s); map1[s]=plugNum++; add_edge(map1[s],ter,1); } int m; scanf("%d",&m); int ans=m; char k[30],v[30]; while(m--){ scanf("%s%s",k,v); if(!map1[v]) {//新插头 map1[v]=plugNum++; } map1[k]=plugNum++; add_edge(map1[k],map1[v],1); //设备指向插头 add_edge(st,map1[k],1); } int q; cin>>q; char u[30]; while(q--){ scanf("%s%s",u,v); if(!map1[u]) { map1[u]=plugNum++; //新插头 } if(!map1[v]) { map1[v]=plugNum++; } add_edge(map1[u],map1[v],INF); //插头之间无穷 } ans-=max_flow(st,ter); printf("%d\n",ans); } return 0;}
阅读全文
0 0
- POJ 1087
- poj 1087
- poj-1087
- POJ 1087
- poj 1087
- poj 1087
- POJ 1087
- poj 1087 解题报告
- poj 1087(最大流)
- poj 1087 最大流
- poj 1087 网络流
- POJ 1087 UNIX dinic
- poj 1087 (最大流)
- POJ 1087最大流
- POJ 1087 网络流
- POJ
- poj
- POJ
- Javase—抽象类与接口习题
- 命令模式
- Charles抓包工具使用介绍
- 汇编实验报告
- 【2017年11月27日】CSDN博客更新周报
- POJ 1087
- CodeForces
- cookie和session总结
- 欢迎使用CSDN-markdown编辑器
- 关于storm版本的问题
- JSTL
- [Python]如何理解Python中的装饰器
- 计算机视觉牛人博客和代码汇总(全)
- Parameter Server 详解