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

uva753 A Plug for UNIX 网络流最大流的更多相关文章

  1. UVa753/POJ1087_A Plug for UNIX(网络流最大流)(小白书图论专题)

    解题报告 题意: n个插头m个设备k种转换器.求有多少设备无法插入. 思路: 定义源点和汇点,源点和设备相连,容量为1. 汇点和插头相连,容量也为1. 插头和设备相连,容量也为1. 可转换插头相连,容 ...

  2. poj 1087 C - A Plug for UNIX 网络流最大流

    C - A Plug for UNIXTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contes ...

  3. UVA 753 - A Plug for UNIX(网络流)

      A Plug for UNIX  You are in charge of setting up the press room for the inaugural meeting of the U ...

  4. POJ1087 A Plug for UNIX 【最大流】

    A Plug for UNIX Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13855   Accepted: 4635 ...

  5. POJ 1087 A Plug for UNIX (网络流,最大流)

    题面 You are in charge of setting up the press room for the inaugural meeting of the United Nations In ...

  6. 【uva753/poj1087/hdu1526-A Plug for UNIX】最大流

    题意:给定n个插座,m个插头,k个转换器(x,y),转换器可以让插头x转成插头y.问最少有多少个插头被剩下. 题解: 最大流或者二分图匹配.然而我不知道怎么打二分图匹配..打了最大流.这题字符串比较坑 ...

  7. poj 1087.A Plug for UNIX (最大流)

    网络流,关键在建图 建图思路在代码里 /* 最大流SAP 邻接表 思路:基本源于FF方法,给每个顶点设定层次标号,和允许弧. 优化: 1.当前弧优化(重要). 1.每找到以条增广路回退到断点(常数优化 ...

  8. uva753 A Plug for UNIX

    最大流. 流可以对应一种分配方式. 显然最大流就可以表示最多匹配数 #include<cstdio> #include<algorithm> #include<cstri ...

  9. poj 1087 A Plug for UNIX 【最大流】

    题目连接:http://poj.org/problem? id=1087 题意: n种插座 ,m个电器,f组(x,y)表示插座x能够替换插座y,问你最多能给几个电器充电. 解法:起点向插座建边,容量1 ...

随机推荐

  1. MJExtension使用指导(转)

    MJExtension使用指导(转)  MJExtension能做什么? MJExtension是一套字典和模型之间互相转换的超轻量级框架 MJExtension能完成的功能 字典(JSON) --& ...

  2. Easyui treegrid 无法显示树形结构解决办法

    easyui treegrid 中检查了数据结构没有问题的,但就是不展示树形结构, 检查发现原来是 var columnsAll = [ { title: '任务ID', field: 'TaskID ...

  3. Android Facebook和Twitter登录和分享完整版

    最近公司的软件需要改国际版,需要Facebook和Twitter的登录和分享. 本人先用Umeng的第三方社会化分享实现了该功能,但是后来一想问题来了,经过查证.Umeng只在中国和美国有服务器,那也 ...

  4. MySql中的concat()相关函数

    concat 函数的基本应用一: SQL CONCAT函数用于将两个字符串连接起来,形成一个单一的字符串.试试下面的例子: SQL> SELECT CONCAT('FIRST ', 'SECON ...

  5. 使用iozone测试磁盘性能(测试文件读写)

    IOzone是一个文件系统测试基准工具.可以测试不同的操作系统中文件系统的读写性能.可以通过 write, re-write, read, re-read, random read, random w ...

  6. Zookeeper的集群安装和配置

    zk服务器集群规模不小于3个节点,要求各服务器之间系统时间要保持一致. 在master节点的/home/hadoop目录下,解压缩zk....tar.gz,具体安装的路径自选 解压后重命名该文件夹为z ...

  7. Ruby中map, collect,each,select,reject,reduce的区别

    # map 针对每个element进行变换并返回整个修改后的数组 def map_method arr1 = ["name2", "class2"] arr1. ...

  8. JavaScriptl 类数组转换为数组

    slice和Array.form方法,具体见示例代码: <!DOCTYPE html> <html lang="zh"> <head> < ...

  9. How to get the url of a page in OpenERP?

    How to get the url of a page in OpenERP? User is using OpenERP. I have a button on one web page. The ...

  10. python 排序函数

    1.sorted()函数:内建函数,适用于所有类型,返回排序后的对象,原对象不改变,sorted(a,key=,reversed=True) >>> sorted((3,1,4,2) ...