UVA 753 A Plug for UNIX
最大流解决 。
设置源点 0,连接所有设备(device) 。设备-插头 -汇点
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == ? b : gcd(b, a % b);}
#define MAXN 330
const int INF = 0x3f3f3f3f ;
int N,M,K,src,tag;
int p[MAXN];
int flow[MAXN][MAXN],cap[MAXN][MAXN];
int a[MAXN];
map<string ,int > cz,deg;
string str;
void read()
{
cin >> N;
int add = ;
cz.clear(); deg.clear();
memset(cap,,sizeof(cap));
for (int i = ; i <= N; i++)
{
cin >> str;
cz[str] = i;
}
cin >> M;
for (int i = ; i <= M; i++)
{
cin >> str;
deg[str] = i;
cap[][i] += ;
cin >> str;
int tmp = cz[str];
if (tmp == )//需要新的插座
{
add ++;
cz[str] = N + add;
tmp = N + add;
}
cap[i][tmp + M] += ;
}
for (int i = M + ; i <= M + N; i++)
cap[i][N + M + add + ] += ;
src = ; tag = N + M + add + ;
cin >> K;
while (K --)
{
cin >> str;
int tmp = cz[str];
cin >> str;
int res = cz[str];
cap[M + tmp][M + res] += INF;
}
}
int Edmonds_karp()
{
memset(flow,,sizeof(flow));
queue<int>q;
int ans = ;
while (!q.empty()) q.pop();
while (true)
{
memset(a,,sizeof(a));
a[src] = INF;
q.push(src);
while (!q.empty())
{
int u = q.front(); q.pop();
for (int v = ; v <= tag; v++)
if (!a[v] && cap[u][v] > flow[u][v])
{
p[v] = u;
q.push(v);
a[v] = min (a[u],cap[u][v] - flow[u][v]);
}
}
if (a[tag] == ) break;
for (int u = tag; u != src; u = p[u])
{
flow[p[u]][u] += a[tag];
flow[u][p[u]] -= a[tag];
}
ans += a[tag];
}
return ans;
}
int main()
{
//freopen("sample.txt","r",stdin);
int T;
scanf("%d",&T);
while (T--)
{
read();
printf("%d\n",M - Edmonds_karp());
if (T) putchar('\n');
}
return ;
}
UVA 753 A Plug for UNIX的更多相关文章
- POJ 1087 A Plug for UNIX / HDU 1526 A Plug for UNIX / ZOJ 1157 A Plug for UNIX / UVA 753 A Plug for UNIX / UVAlive 5418 A Plug for UNIX / SCU 1671 A Plug for UNIX (网络流)
POJ 1087 A Plug for UNIX / HDU 1526 A Plug for UNIX / ZOJ 1157 A Plug for UNIX / UVA 753 A Plug for ...
- 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 Unit ...
- 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 ...
- UVA 753 A Plug for UNIX 电器插座(最大基数匹配,网络流)
题意: 给n个插座,m个设备(肯定要插电了),k种转换头可无限次使用(注意是单向的),问有多少设备最终是不能够插上插座的? 分析: 看起来就是设备匹配插座,所以答案不超过m.这个题适合用网络流来解. ...
- UVa 753 - A Plug for UNIX(最大流)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- ZOJ1157, POJ1087,UVA 753 A Plug for UNIX (最大流)
链接 : http://acm.hust.edu.cn/vjudge/problem/viewProblem.action? id=26746 题目意思有点儿难描写叙述 用一个别人描写叙述好的. 我的 ...
- UVA - 753 A Plug for UNIX(网络流)
题意 给定一些插头设备和插座,有一些方法可以把其中一些插头变成另一种插头.求无法匹配插座的插头设备个数. 题解 用\(map\)给每个字符串标号为\(a_i\)和\(b_i\). 读入每种改变插头的方 ...
- UVa 753 A Plug for UNIX (最大流)
题意:给定 n 种插座,m种设备,和k个转换器,问你最少有几台设备不能匹配. 析:一个很裸的网络流,直接上模板就行,建立一个源点s和汇点t,源点和每个设备连一条边,每个插座和汇点连一条边,然后再连转换 ...
- UVA 753 A Plug for UNIX (最大流)
关键在建图,转换器连一条容量无限的边表示可以转化无数次,设备的插头连源点,插座连汇点. dinic手敲已熟练,输出格式又被坑,总结一下,输出空行多case的,一个换行是必要的,最后一个不加空行,有Te ...
随机推荐
- rpm、yum命令
一.rpm命令 挂载光盘文件到/media目录: 进去/media目录下的Packages目录: 查看系统已安装的所有rpm包: 查看系统是否安装dhcp软件包: 安装dhcp软件包: 查看dhcp软 ...
- 调试bug 技巧
两天,一个小bug 我调试了两天,最终调试成功了.还是在别人的帮助下. 问题是刷新相关的.当用户登录了,其他的页面都要刷新.也就是加上一些参数. 但是有一个fragment一直加不上,其他挨着的两个都 ...
- echart搭配时间轴进行展示 (本例展示的是多时间 多地区 多指标条件 )
1:照常先来几张图 看效果 2:首先 看官方文档 我把echart官方的例子给扒下来并整理了得出如下效果 上 案例图和代码 效果图 : 代码: <style type="text/c ...
- 【Java集合源码剖析】Java集合框架
Java集合工具包位于Java.util包下,包含了很多常用的数据结构,如数组.链表.栈.队列.集合.哈希表等.学习Java集合框架下大致可以分为如下五个部分:List列表.Set集合.Map映射.迭 ...
- 用(bootstrap)Handsontable做表格,手动实现数据排序
商品graph帐票时,用(bootstrap)Handsontable做表格,手动实现数据排序待解决的问题: 若使用控件本身的排序,必须指定colHead,colHead不能被copy,若想表头被co ...
- TCP close seq问题
测试mt_hls一条流时,发现会话的时长总是对应不上. 仔细观察发现: 注意 1.包1735 (客户端) 发送FIN 请求,seq = 2435582428 2.包1736,1737,1738 (服务 ...
- Redmine部署到Windows Azure
有幸,今天可以尝试将Redmine部署到Windows Azure中,记下点滴,方便大家查阅 步骤一:Windows Azure中安装Ubuntu VM 遇到的问题,创建VM时会提示云服务.云存储订阅 ...
- 剑指Offer - 九度1508 - 把字符串转换成整数
剑指Offer - 九度1508 - 把字符串转换成整数2014-02-06 23:46 题目描述: 将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数. 输入: 输入可能包含多个测试样例 ...
- 《Cracking the Coding Interview》——第5章:位操作——题目4
2014-03-19 06:15 题目:解释(n & (n - 1)) == 0是什么意思? 解法:n&n-1是去掉最低位‘1’的方法.根据运算符优先级,貌似用不着加那个括号,但位运算 ...
- Pascal 基础教程
Pascal现在还有人想学习吗?先给出一本不错的Pascal教程,Object Pascal的教程我日后给出. Pascal基础教程 第一课 初识PASCAL语言 …… ...