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 ...
随机推荐
- easypoi 一行代码搞定excel导入导出
开发中经常会遇到excel的处理,导入导出解析等等,java中比较流行的用poi,但是每次都要写大段工具类来搞定这事儿,此处推荐一个别人造好的轮子[easypoi],下面介绍下“轮子”的使用. pom ...
- Mybatis中updateByPrimaryKeySelective和updateByPrimaryKey区别
int updateByPrimaryKeySelective(TbItem record); int updateByPrimaryKey(TbItem record); 上面的是逆转工程生成的Ma ...
- 通过IIS共享文件夹来实现静态资源"本地分布式"部署
以下以文件型数据库(如sqlite)为例 楼主话:以下内容,若有不专业处,大胆喷,虚心求教. 起因:要进行一个项目的分布式部署,而这个项目所涉及的其中一个数据库为sqlite(经测试,同为文件型数据库 ...
- div嵌套img高度不相同
div中嵌套img,如果div里嵌套一个img元素且div的高度是由img的高度来撑开,那么div的高度总会比img的高度多3px. 可以明显看到div实际高度高出img高度3px.为了解决此问题,我 ...
- P3365 改造二叉树
P3365 改造二叉树 链接 分析: 求出中序遍历后,然后使其变成上升子序列.过程:每个点减去坐标,然后nlogn求出最长不下降子序列,n-ans即答案. 做题时一直认为二叉树就是完全二叉树,然后一直 ...
- Linux命令学习总结(一)
命令 -选项 参数 如果选项是一个单词时,选项前面要加2个- modprobe -r pcspkr 在终端中输入的时候有声音,可以用这个命令屏蔽声音 ,需要root权限 useradd userd ...
- PJSIP-PJMEDIA【使用pjmedia 播放wav格式的音乐】
应宝哥建议以及更好的交流学习,这篇开始使用中文,英语就先放一放吧! 要使用PJSIP中的PJMEDIA首先我们需要搭建好它所需要的环境. [环境搭建与调试] 1 在 工具 加入pjmedia所需要的包 ...
- Freemarker 语法详解
Freemarker 在线中文官方参考手册 Freemarker是一款模板引擎,是一种基于模版生成静态文件的通用工具,它是使用纯java编写的,一般用来生成HTML页面. Freemarker 生成静 ...
- (转) Unreal的HLSL交叉编译-UEAPI
HLSL Cross Compiler This library compiles High Level Shading Language (HLSL) shader source code into ...
- python作业:模拟登陆(第一周)
模拟登陆作业需求: 1. 用户输入帐号密码进行登陆 2. 用户信息保存在文件内 3. 用户密码输入错误三次后锁定用户 额外实现功能: 1.提示输入错误次数 2.输入已锁定用户会提示 3.用户不存在会提 ...