A Plug for UNIX UVA - 753(网络流)
题意:n个插座,m个设备及其插头类型,k种转换器,没有转换器的情况下插头只能插到类型名称相同的插座中,问最少剩几个不匹配的设备
lrj紫书里面讲得挺好的。
先跑一遍floyd,看看插头类型a能否转换为b
然后构造网络
源点为0, 汇点为n + m + 1,源点连插头 容量为1,插座连汇点,容量为1
插头和插座能互相转换的容量为INF,跑一遍最大流 m - 最大流就是答案
顺便粘一下dinic的板子
#include <bits/stdc++.h>
using namespace std; inline int read() {
int x = , f = ; char ch = getchar();
while (ch < '' || ch > '') { if (ch == '-') f = -; ch = getchar();}
while (ch >= '' && ch <= '') { x = x * + ch - ; ch = getchar();}
return x * f;
} const int N = ;
const int INF = 0x3f3f3f3f;
int n, m, k, tol, cnt;
map<string, int> mp;
vector<int> a;
vector<int> b;
bool f[N][N];
struct Edge { int v, f, next; } edge[N * ];
int head[N], level[N], iter[N]; inline void init() {
mp.clear();
a.clear();
b.clear();
tol = cnt = ;
memset(f, , sizeof(f));
memset(head, -, sizeof(head));
} inline void addedge(int u, int v, int f) {
edge[cnt].v = v; edge[cnt].next = head[u]; edge[cnt].f = f; head[u] = cnt++;
} void floyd() {
for (int k = ; k <= tol; k++) {
for (int i = ; i <= tol; i++) {
for (int j = ; j <= tol; j++) {
f[i][j] = f[i][j] || (f[i][k] && f[k][j]);
}
}
}
} bool bfs(int s, int t) {
for (int i = ; i <= t; i++) iter[i] = head[i], level[i] = -;
level[s] = ;
queue<int> que;
que.push(s);
while (!que.empty()) {
int u = que.front(); que.pop();
for (int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].v, f = edge[i].f;
if (f > && level[v] == -) {
level[v] = level[u] + ;
que.push(v);
}
}
}
return level[t] != -;
} int dfs(int u, int t, int f) {
if (!f || u == t) return f;
int flow = , w;
for (int i = iter[u]; ~i; i = edge[i].next) {
iter[u] = i;
int v = edge[i].v;
if (level[v] == level[u] + && edge[i].f > ) {
w = dfs(v, t, min(f, edge[i].f));
if (w == ) continue;
f -= w;
edge[i].f -= w;
edge[i^].f += w;
flow += w;
if (f <= ) break;
}
}
return flow;
} int dinic(int s, int t) {
int ans = ;
while (bfs(s, t)) ans += dfs(s, t, INF);
return ans;
} int main() {
int T = read();
while (T--) {
init();
n = read();
for (int i = ; i <= n; i++) {
string s;
cin >> s;
mp[s] = i;
a.emplace_back(i);
tol++;
}
m = read();
for (int i = ; i <= m; i++) {
string str1, s;
cin >> str1 >> s;
if (!mp.count(s)) {
mp[s] = ++tol;
}
b.emplace_back(mp[s]);
}
k = read();
for (int i = ; i <= k; i++) {
string s1, s2;
cin >> s1 >> s2;
if (!mp.count(s1)) mp[s1] = ++tol;
if (!mp.count(s2)) mp[s2] = ++tol;
f[mp[s1]][mp[s2]] = ;
}
for (int i = ; i <= tol; i++) f[i][i] = ;
floyd();
for (int i = ; i <= m; i++) {
addedge(, i, );
addedge(i, , );
}
for (int i = ; i <= n; i++) {
addedge(m + i, n + m + , );
addedge(n + m + , m + i, );
}
for (int i = ; i < m; i++) {
for (int j = ; j < n; j++) {
if (!f[b[i]][a[j]]) continue;
addedge(i + , m + j + , INF);
addedge(m + j + , i + , );
}
}
printf("%d\n", m - dinic(, n + m + ));
if (T) puts("");
}
return ;
}
A Plug for UNIX UVA - 753(网络流)的更多相关文章
- 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(图论--网络流最大流 Dinic)
题意:有N个插头,M个设备和K种转换器.要求插的设备尽量多,问最少剩几个不匹配的设备. 解法:给读入的各种插头编个号,源点到设备.设备通过转换器到插头.插头到汇点各自建一条容量为1的边.跑一次最大流就 ...
- C - A Plug for UNIX POJ - 1087 网络流
You are in charge of setting up the press room for the inaugural meeting of the United Nations Inter ...
- 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(二分图匹配)
A Plug for UNIX You are in charge of setting up the press room for the inaugural meeting of the Unit ...
- uva753 A Plug for UNIX 网络流最大流
C - A Plug for UNIX You are in charge of setting up the press room for the inaugural meeting of t ...
- POJ1087 A Plug for UNIX(网络流)
A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K Total S ...
- 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 ...
- UVa753/POJ1087_A Plug for UNIX(网络流最大流)(小白书图论专题)
解题报告 题意: n个插头m个设备k种转换器.求有多少设备无法插入. 思路: 定义源点和汇点,源点和设备相连,容量为1. 汇点和插头相连,容量也为1. 插头和设备相连,容量也为1. 可转换插头相连,容 ...
随机推荐
- php之laravel学习
http://laravel-china.github.io/php-the-right-way/#composer_and_packagist laravel 添加 dingoapi路由插件 并运用 ...
- hihoCoder#1068(RMQ-ST算法)
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho在美国旅行了相当长的一段时间之后,终于准备要回国啦!而在回国之前,他们准备去超市采购一些当地特产——比如汉堡 ...
- java流类、、、理解不够,流太多不知怎么用好?
总结:输入流.输出流..子类多.需要加强: package com.da; import java.io.*; public class rtr { public static void main(S ...
- DevExpress TreeList GridView 样式设置
1.GridView 样式设置 this.gridViewUser.PaintStyleName = "Flat"; 2.TreeList 样式设置 this.treeListDe ...
- mysql 异常宕机 ..InnoDB: Database page corruption on disk or a failed,,InnoDB: file read of page 8.
mysql 测试环境异常宕机 系统:\nKylin 3.3 mysql版本:5.6.15--yum安装,麒麟提供的yum源数据库版本 error日志 181218 09:38:52 mysqld_sa ...
- Spring学习五
1: servlet生命周期: Servlet加载 -> 实例化-> 服务 -> 销毁 2:Servlet重要函数: init():在Servlet的生命周期中,仅 ...
- javascript——对象的概念——Object(未完)
http://www.blogjava.net/zkjbeyond/archive/2006/04/16/41336.html javascript中对象只包括属性和方法两种成员.ECMA-262 把 ...
- python jvm数据
在网上找的抱歉忘了原链接了额 #!/usr/bin/env python # # import os import commands import re import sys (status1, re ...
- sqlserver 使用维护计划备份
https://www.cnblogs.com/teafree/p/4240040.html
- latex bib format
LaTeX 的对参考文献的处理实在是非常的方便,我用过几次,有些体会,写出来供大家参考.当然,自己的功力还不够深,有些地方问题一解决就罢手了,没有细究. LaTeX 对参考文献的处理有这么一些优点: ...