UVA - 753 A Plug for UNIX(网络流)
题意
给定一些插头设备和插座,有一些方法可以把其中一些插头变成另一种插头。求无法匹配插座的插头设备个数。
题解
用\(map\)给每个字符串标号为\(a_i\)和\(b_i\)。
读入每种改变插头的方法,连边,权值为\(inf\)。
然后连边\(S \longrightarrow a_i\),权值为\(1\);\(b_i \longrightarrow T\),权值为\(1\)。
跑最大流即可。
代码
#include <bits/stdc++.h>
#define FOPI freopen("in.txt", "r", stdin)
#define FOPO freopen("out.txt", "w", stdout)
#define FOR(i,x,y) for (int i = x; i <= y; i++)
#define ROF(i,x,y) for (int i = x; i >= y; i--)
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 100;
const int maxm = 2e5 + 100;
struct Edge
{
int to, next, cap, flow;
}edge[maxm];
int tot;
int head[maxn];
void init()
{
tot = 2;
memset(head, -1, sizeof(head));
}
void build(int u, int v, int w, int rw = 0)
{
edge[tot].to = v; edge[tot].cap = w; edge[tot].flow = 0;
edge[tot].next = head[u]; head[u] = tot++;
edge[tot].to = u; edge[tot].cap = 0; edge[tot].flow = 0;
edge[tot].next = head[v]; head[v] = tot++;
}
int Q[maxn];
int dep[maxn], cur[maxn], sta[maxn];
bool bfs(int s, int t, int n)
{
int front = 0, tail = 0;
memset(dep, -1, sizeof(dep[0]) * (n+1));
dep[s] = 0;
Q[tail++] = s;
while(front < tail)
{
int u = Q[front++];
for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if (edge[i].cap > edge[i].flow && dep[v] == -1)
{
dep[v] = dep[u] + 1;
if (v == t) return true;
Q[tail++] = v;
}
}
}
return false;
}
LL dinic(int s, int t, int n)
{
LL maxflow = 0;
while(bfs(s, t, n))
{
for (int i = 0; i < n; i++) cur[i] = head[i];
int u = s, tail = 0;
while(cur[s] != -1)
{
if (u == t)
{
int tp = inf;
for (int i = tail-1; i >= 0; i--)
tp = min(tp, edge[sta[i]].cap - edge[sta[i]].flow);
//if (tp >= inf) return -1;
maxflow += tp;
for (int i = tail-1; i >= 0; i--)
{
edge[sta[i]].flow += tp;
edge[sta[i]^1].flow -= tp;
if (edge[sta[i]].cap - edge[sta[i]].flow == 0) tail = i;
}
u = edge[sta[tail]^1].to;
}
else if (cur[u] != -1 && edge[cur[u]].cap > edge[cur[u]].flow
&& dep[u]+1 == dep[edge[cur[u]].to])
{
sta[tail++] = cur[u];
u = edge[cur[u]].to;
}
else
{
while(u != s && cur[u] == -1) u = edge[sta[--tail]^1].to;
cur[u] = edge[cur[u]].next;
}
}
}
return maxflow;
}
int t;
int a[205], b[205];
string s, r;
map<string, int> M;
int n, m, S, T, cnt, q;
int getid(string &s)
{
if (M.count(s)) return M[s];
return M[s] = ++cnt;
}
int main()
{
// FOPI;
// FOPO;
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> t;
FOR(ca, 1, t)
{
init();
M.clear(); cnt = 0;
cin >> n;
FOR(i, 1, n) { cin >> s; a[i] = getid(s); }
cin >> m;
FOR(i, 1, m) { cin >> r >> s; b[i] = getid(s); }
cin >> q;
FOR(i, 1, q)
{
cin >> s >> r;
int x = getid(s), y = getid(r);
build(y, x, inf);
}
S = 0, T = cnt+1;
FOR(i, 1, n) build(S, a[i], 1);
FOR(j, 1, m) build(b[j], T, 1);
LL ans = dinic(S, T, T);
printf("%lld\n", m-ans);
if (ca != t) puts("");
}
}
UVA - 753 A Plug for UNIX(网络流)的更多相关文章
- 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 ...
- 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 电器插座(最大基数匹配,网络流)
题意: 给n个插座,m个设备(肯定要插电了),k种转换头可无限次使用(注意是单向的),问有多少设备最终是不能够插上插座的? 分析: 看起来就是设备匹配插座,所以答案不超过m.这个题适合用网络流来解. ...
- UVa 753 - A Plug for UNIX(最大流)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVa 753 A Plug for UNIX (最大流)
题意:给定 n 种插座,m种设备,和k个转换器,问你最少有几台设备不能匹配. 析:一个很裸的网络流,直接上模板就行,建立一个源点s和汇点t,源点和每个设备连一条边,每个插座和汇点连一条边,然后再连转换 ...
- UVA 753 A Plug for UNIX (最大流)
关键在建图,转换器连一条容量无限的边表示可以转化无数次,设备的插头连源点,插座连汇点. dinic手敲已熟练,输出格式又被坑,总结一下,输出空行多case的,一个换行是必要的,最后一个不加空行,有Te ...
- UVA 753 A Plug for UNIX
最大流解决 . 设置源点 0,连接所有设备(device) .设备-插头 -汇点 #include <map> #include <set> #include <list ...
- ZOJ1157, POJ1087,UVA 753 A Plug for UNIX (最大流)
链接 : http://acm.hust.edu.cn/vjudge/problem/viewProblem.action? id=26746 题目意思有点儿难描写叙述 用一个别人描写叙述好的. 我的 ...
随机推荐
- Dedecms标签arclistsg调用单表模型出错的解决方法
使用arclistsg标签调用分类信息等单表模型出错提示Column 'id' in where clause is ambiguous, 修改文件:include\taglib\arclistsg ...
- 使用WinSCP上传文件到linux系统
1.安装WinSCP 2.新建脚本test.txt option confirm off open username:password@host put C:\test\a.zip /home/tes ...
- MyBatis总结与复习
Spring 主流框架 依赖注入容器/AOP实现 声明式事务 简化JAVAEE应用 粘合剂,将大家组装到一起 SpringMVC 1. 结构最清晰的MVC Model2实现 2. 高度可配置,支持 ...
- 关于Mdi窗口-父窗口上的控件把子窗口的挡住的问题
using System.Runtime.InteropServices; [DllImport("user32")] public static extern int SetPa ...
- Spring之Quartz定时任务和Cron表达式详解
1.定时业务逻辑类 public class ExpireJobTask { /** Logger */ private static final Logger logger = LoggerFact ...
- slfj4 + logback
slf4j:(Simple Logging Facade for Java,简单日志门面),它不是具体的日志解决方案,只服务于各种各样的日志系统.在使用SLF4J的时候,不需要再代码中或配置文件中指定 ...
- uLua学习之创建游戏对象(二)
前言 上节,刚刚说到创建一个“HelloWorld”程序,大家想必都对uLua有所了解了,现在我们一步步地深入学习.在有关uLua的介绍中(在这里),我们可以发现它使用的框架是Lua + LuaJIT ...
- sk-learning(1)
sk-learning学习笔记(1) 简介 scikit learning 是一个python的机器学习库,内置许多机器学习的算法诸如svm.随机森林.逻辑回归.贝叶斯网络等算法.覆盖了分类.聚类.回 ...
- VMware-Ubuntu16.04LTS-安装ssh
1,检查是否安装ssh 2,安装ssh
- coursera_ML_1
机器学习定义: A computer program is said to leran from experience E with respect to some task T and some ...