题意:给定 n 种插座,m种设备,和k个转换器,问你最少有几台设备不能匹配。

析:一个很裸的网络流,直接上模板就行,建立一个源点s和汇点t,源点和每个设备连一条边,每个插座和汇点连一条边,然后再连转换器,

最后跑一次最大流即可。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-5;
const int maxn = 500 + 10;
const int mod = 1e6;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
struct Edge{
int from, to, cap, flow;
}; struct Dinic{
int n, m, s, t;
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn]; void init(){
edges.clear();
for(int i = 0; i < maxn; ++i) G[i].clear();
}
bool bfs(){
memset(vis, 0, sizeof vis);
queue<int> q;
q.push(s);
d[s] = 0; vis[s] = true;
while(!q.empty()){
int x = q.front(); q.pop();
for(int i = 0; i < G[x].size(); ++i){
Edge &e = edges[G[x][i]];
if(!vis[e.to] && e.cap > e.flow){
vis[e.to] = 1;
d[e.to] = d[x] + 1;
q.push(e.to);
}
}
}
return vis[t];
} int dfs(int x, int a){
if(x == t || a == 0) return a;
int flow = 0, f;
for(int& i = cur[x]; i < G[x].size(); ++i){
Edge& e = edges[G[x][i]];
if(d[x] + 1 == d[e.to] && (f = dfs(e.to, min(a, e.cap-e.flow))) > 0){
e.flow += f;
edges[G[x][i]^1].flow -= f;
flow += f;
a -= f;
if(!a) break;
}
}
return flow;
} int maxflow(int s, int t){
this->s = s; this->t = t;
int flow = 0;
while(bfs()){
memset(cur, 0, sizeof cur);
flow += dfs(s, INF);
}
return flow;
} void addEdge(int from, int to, int cap){
edges.push_back(Edge{from, to, cap, 0});
edges.push_back(Edge{to, from, 0, 0});
m = edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
};
map<string, int> mp; int getId(const string &s){
if(mp.count(s)) return mp[s];
return mp[s] = mp.size();
}
vector<int> socket;
vector<P> device;
vector<P> convert; void init(){
mp.clear();
socket.clear();
device.clear();
convert.clear();
} int solve(){
int s = mp.size() + 1;
int t = mp.size() + 2;
Dinic dinic;
dinic.init();
for(int i = 0; i < device.size(); ++i){
dinic.addEdge(s, device[i].first, 1);
dinic.addEdge(device[i].first, device[i].second, 1);
}
for(int i = 0; i < socket.size(); ++i)
dinic.addEdge(socket[i], t, 1);
for(int i = 0; i < convert.size(); ++i)
dinic.addEdge(convert[i].first, convert[i].second, INF);
return (int)device.size() -dinic.maxflow(s, t);
} int main(){
int T; cin >> T;
while(T--){
cin >> n;
init();
string s;
for(int i = 0; i < n; ++i){
cin >> s;
socket.push_back(getId(s));
}
cin >> n;
string ss;
for(int i = 0; i < n; ++i){
cin >> s >> ss;
device.push_back(P(getId(s), getId(ss)));
}
cin >> n;
for(int i = 0; i < n; ++i){
cin >> s >> ss;
convert.push_back(P(getId(s), getId(ss)));
}
cout << solve() << endl;
if(T) cout << endl;
}
return 0;
}

UVa 753 A Plug for UNIX (最大流)的更多相关文章

  1. ZOJ1157, POJ1087,UVA 753 A Plug for UNIX (最大流)

    链接 : http://acm.hust.edu.cn/vjudge/problem/viewProblem.action? id=26746 题目意思有点儿难描写叙述 用一个别人描写叙述好的. 我的 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. UVa 753 - A Plug for UNIX(最大流)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  6. UVA 753 A Plug for UNIX (最大流)

    关键在建图,转换器连一条容量无限的边表示可以转化无数次,设备的插头连源点,插座连汇点. dinic手敲已熟练,输出格式又被坑,总结一下,输出空行多case的,一个换行是必要的,最后一个不加空行,有Te ...

  7. UVA 753 A Plug for UNIX 电器插座(最大基数匹配,网络流)

    题意: 给n个插座,m个设备(肯定要插电了),k种转换头可无限次使用(注意是单向的),问有多少设备最终是不能够插上插座的? 分析: 看起来就是设备匹配插座,所以答案不超过m.这个题适合用网络流来解. ...

  8. UVA 753 A Plug for UNIX

    最大流解决 . 设置源点 0,连接所有设备(device) .设备-插头 -汇点 #include <map> #include <set> #include <list ...

  9. UVA - 753 A Plug for UNIX(网络流)

    题意 给定一些插头设备和插座,有一些方法可以把其中一些插头变成另一种插头.求无法匹配插座的插头设备个数. 题解 用\(map\)给每个字符串标号为\(a_i\)和\(b_i\). 读入每种改变插头的方 ...

随机推荐

  1. ubuntu 16.04 更新 gcc/g++ 4.9.2

    ubuntu 转载 2016年10月12日 :: 标签:ubuntu /g++ /gcc [html] view plain copy sudo dpkg -l g++ 最近在学C++primer , ...

  2. libraries_v140_x64_py35_1.0.1.tar.bz2 libraries_v120_x64_py27_1.1.0.tar 下载链接以及百度云下载链接

    下载链接 wget  -c  https://github.com/willyd/caffe-builder/releases/download/v1.0.1/libraries_v140_x64_p ...

  3. OpenCV4Android编译

    http://blog.sina.com.cn/s/blog_602f87700102vdnw.html (2015-04-02 11:10:01) 转载▼     最近的一个项目中,需要自己编译Op ...

  4. C#读取自定义的config

    今天说下C#读写自定义config文件的各种方法.由于这类文章已经很多,但是大多数人举例子都是默认的在app.confg或者web.config进行读写,而不是一般的XML文件,我主要写的是一般的Xm ...

  5. MyBatis学习(二):与Spring整合(非注解方式配置MyBatis)

    搭建SpringMVC的-->传送门<-- 一.环境搭建: 目录结构: 引用的JAR包: 如果是Maven搭建的话,pom.xml的配置如下: <?xml version=" ...

  6. 果壳、推库、虎秀、知乎、it世界

    果壳.推库.虎秀.知乎.it世界

  7. PHP基础函数、自定义函数以及数组

    2.10 星期五  我们已经真正开始学习PHP 了,今天的主要内容是php基础函数.自定义函数以及数组, 内容有点碎,但是对于初学者来说比较重要,下面是对今天所讲内容的整理:  1 php的基本语法和 ...

  8. 安装DotNetCore.1.0.1-VS2015Tools.Preview2.0.3引发的血案

    1.下载了一个开源项目,是用netcore开发的 2.VS2015打不开解决方案 3.于是安装DotNetCore.1.0.1-VS2015Tools.Preview2.0.3 4.安装成功,项目顺利 ...

  9. java之HashMap的遍历Iterator

    package com.ql_2;/* * 功能:HashMap 的使用 */import java.util.*; public class Test_2 { public static void ...

  10. Machine Learning in Action(3) 朴素贝叶斯算法

    贝叶斯决策一直很有争议,今年是贝叶斯250周年,历经沉浮,今天它的应用又开始逐渐活跃,有兴趣的可以看看斯坦福Brad Efron大师对其的反思,两篇文章:“Bayes'Theorem in the 2 ...