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 题目意思有点儿难描写叙述 用一个别人描写叙述好的. 我的 ... 
随机推荐
- Android模拟器使用SD卡
			在Android的应用开发中经常要用到与SD卡有关的调试,本文就是介绍关于在Android模拟器中SD卡的使用 一. 准备工作 在介绍之前首先做好准备工作,即配好android的应用开发环境 ... 
- Android入门:Service入门介绍
			一.Service介绍 Service类似于Windows中的服务,没有界面,只是在后台运行:而服务不能自己运行,而是需要调用Context.startService(Intent intent);或 ... 
- react的setState使用中遇到的问题
			setState()更新的数据和自己预期的不一致 对 React 新手来说,使用 setState 是一件很复杂的事情.即使是熟练的 React 开发,也很有可能因为 React 的一些机制而产生一些 ... 
- Caused by: java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(I)V
			项目中各种缺包现象... Caused by: java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(I)V ... 
- 禁止ASP.NET MVC模型绑定时将空字符串绑定为null
			为model添加[DisplayFormat(ConvertEmptyStringToNull = false)] [Display(ResourceType = typeof(AppStrings) ... 
- 修复SQL中的孤立账户
			EXEC sys.sp_change_users_login 'AUTO_FIX','登录名',NULL,'登录密码' 
- Python之HTML的解析(网页抓取一)
			http://blog.csdn.net/my2010sam/article/details/14526223 --------------------- 对html的解析是网页抓取的基础,分析抓取的 ... 
- 本人常用的Phpstorm快捷键
			我设置的是eclipse的按键风格(按键习惯),不是phpstorm的风格 1.添加TODO(这个不是快捷键)://TODO 后面是说明,换行写实现代码 2.选择相同单词做一次性修改:Alt+J+鼠标 ... 
- maven-整合到eclips
			1.把maven的识别文件放到maven的安装路径下 2.在eclips中的properties中找到maven,勾选下载文档和下载源码的复选框以下载源码 3.创建maven项目 4.右键pom.xm ... 
- WIN7 64位对Excel操作异常
			在本地做Excel导出功能的测试时,报出“检索COM 类工厂中CLSID 为 {00024500-0000-0000-C000-000000000046}的组件时失败”的异常,知道要对Excel进行D ... 
