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. 可转换插头相连,容 ...
随机推荐
- ASP.NET网站性能提升的几个方法
1. HTTP 压缩 HTTP 压缩通常用于压缩从服务端返回的页面内容.它压缩HTTP请求和响应,这个会是巨大的性能提升.我的项目是基于Window Server 2003开发的,可以参考这篇文章. ...
- Java基础--虚拟机JVM
JVM内存结构 Heap Space: 堆内存(Heap Space)是由Young Generation和Old Generation组成,而Young Generation又被分成三部分,Eden ...
- Python:序列的copy() 方法和 copy 模块
转于:Python中copy和deepcopy中的区别 博主:assan 一.序列中的 copy() 方法 # 此方法为浅度复制:复制的数会随着被复制数的嵌套序列的元素的改变而改变: # 功能:将一个 ...
- Even uploading a JPG file can lead to Cross-Site Content Hijacking (client-side attack)!
Introduction: This post is going to introduce a new technique that has not been covered previously i ...
- Don’t panic, it’s just a kernel panic (ZT)
http://blog.kreyolys.com/2011/03/17/no-panic-its-just-a-kernel-panic/ One of the main young sysadmin ...
- 11-09SQLserver 基础-数据库之汇总练习45题
设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表(四)所示,数据如表1-2的表 ...
- TS封装格式
ts流最早应用于数字电视领域,其格式非常复杂包含的配置信息表多达十几个,视频格式主要是mpeg2.苹果公司发明的http live stream流媒体是基于ts文件的,不过他大大简化了传统的ts流,只 ...
- 【259】ucpole.dat update
2017年2月21日 57871 +0.020896 0.007232 +0.414732 0.009212 +0.418044 0.007533 p 57872 +0.022055 0.007284 ...
- oracle --(二)分区(extent)
基本关系:数据库---表空间---数据段---分区---数据块 一.分区(extent)分区extent是比数据块大一级的存储结构,是几个逻辑上相邻的data block的组合.我们知道,物理存储通常 ...
- C++面向对象类的实例题目六
问题描述: 编写一个程序计算两个给定长方形的面积,其中在设计类成员函数addarea()(用于计算两个长方形的总面积)时使用对象作为参数. 程序代码: #include<iostream> ...