最大流解决 。

设置源点 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的更多相关文章

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

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

  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 电器插座(最大基数匹配,网络流)

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

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

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

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

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

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

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

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

    题意:给定 n 种插座,m种设备,和k个转换器,问你最少有几台设备不能匹配. 析:一个很裸的网络流,直接上模板就行,建立一个源点s和汇点t,源点和每个设备连一条边,每个插座和汇点连一条边,然后再连转换 ...

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

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

随机推荐

  1. JAVA运行机制

    这一篇我们来简单理解一下JAVA的运行机制 大概可以分为三大部分 1.编写程序 2.编译程序 3.运行程序 1.编写程序 编写程序就是我们前面说的源代码 这些源代码都有特殊的语法 例如main函数 他 ...

  2. zeppelin的数据集的优化

    前面我们介绍了zeppelin的修改,前面由于自己的原因,对zeppelin的修改过于多,现在由于优化了,我们两个类, 一个是zeppelin-server的NotebookServer的类的broa ...

  3. PHP.TP框架下商品项目的优化2-图片优化

    图片存储.上传.显示优化 1.图片路径写进配置文件,当路径有变动时[因业务扩大,服务器存储图片空间不足等],只需修改配置文件,而不用修改代码 2.封装显示.上传.删除函数,实现代码重用 [可类比其他类 ...

  4. 3 Vue.js

    1 2 3 <script> var vm = new Vue({ el:"#app", //context data:{ //context["articl ...

  5. laravel5.5服务提供器

    目录 1. 编写服务提供器 1.1 注册方法 register 1.1.1 简单绑定 1.1.2 绑定单例 1.1.3 绑定实例 1.1.4 绑定初始数据 1.2 引导方法 boot 2. 注册服务提 ...

  6. C#中窗体、MDI的使用心得

    1、Show()和ShowDialog()的差别,前者在执行之后,无需等待返回,多用于MDI窗体的显示(MDI窗体之间是并列的,可以随时切换,需要注意自己调整的页面的需求,是要并列,还是要等待其中一个 ...

  7. 【Java】Java JDK 安装及环境配置

    安装包下载: Java Platform (JDK) 11 https://www.oracle.com/technetwork/java/javase/downloads/index.html 安装 ...

  8. Mysql忘记root密码怎么办?(已解决)

    为了写这篇文档,假装一下忘记密码!!!! 首先我数据库是正常的,可以使用. root@localhost:~# mysql -uroot -p mysql> mysql> show dat ...

  9. python 学习分享-线程

    多线程类似于同时执行多个不同程序,多线程运行有如下优点: 使用线程可以把占据长时间的程序中的任务放到后台去处理. 用户界面可以更加吸引人,这样比如用户点击了一个按钮去触发某些事件的处理,可以弹出一个进 ...

  10. NGUI执行基本事件的原理

    通常我们为对象附加一个脚本组件,脚本组件只要加此鼠标处理事件方法,这个对象就有了点击事件了: void OnClick() { Debug.Log("onclick"); } 可为 ...