UVA 753 A Plug for UNIX 电器插座(最大基数匹配,网络流)
题意:
给n个插座,m个设备(肯定要插电了),k种转换头可无限次使用(注意是单向的),问有多少设备最终是不能够插上插座的?
分析:
看起来就是设备匹配插座,所以答案不超过m。这个题适合用网络流来解。
假设每种头对应着一个编号(可以用map实现转换string到int),主要在k种转换头的建边,他们之间的转换关系就是编号与编号之间的边,因为可以无限次使用,所以容量无穷。再添加源点和汇点就建完了,汇点连接每个插座,源点连接每个设备,每边容量为1。使用增广路算法就得出解了。注意要空一行。
很不愿意用结构体的,但是既然用起来这么方便,就用着吧,有些题是需要用结构体的。
//#pragma comment(linker,"/STACK:102400000,102400000")
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <stack>
#include <algorithm>
#include <map>
#include <bits/stdc++.h>
#define LL long long
#define pii pair<int,int>
#define INF 0x7f7f7f7f
using namespace std;
const int N=+;
unordered_map<string, int> has;
vector<int> dev;
vector<int> plug;
vector<pii> chg;
vector<int> vect[];
int path[];
int flow[];
int edge_cnt; struct node
{
int from;
int to;
int cap;
int flo;
}edge[N]; void add_node(int fr,int t,int ca,int fl)
{
edge[edge_cnt].from=fr;
edge[edge_cnt].to=t;
edge[edge_cnt].cap=ca;
edge[edge_cnt].flo=fl;
vect[fr].push_back(edge_cnt++);
} void build_graph(int n, int m, int k, int hui)
{
//每种型号的插口是个点
for(int i=; i<chg.size(); i++) //转换器自身
{
int in=chg[i].first;
int out=chg[i].second; add_node(in, out, INF, ); //应该是四条边
add_node(out, in, , );
} for(int i=; i<dev.size(); i++) //添加源点0号
{
int in=dev[i];
add_node(, in, , );
add_node(in, , , );
} for(int i=; i<plug.size(); i++) //添加汇点
{
int out=plug[i];
add_node(out, hui, , );
add_node(hui, out, , );
}
} int bfs(int s,int e)
{
deque<int> que;
que.push_back(s);
flow[s]=INF;
while(!que.empty())
{
int x=que.front();
que.pop_front();
for(int i=; i<vect[x].size(); i++)
{
node e=edge[vect[x][i]];
if(!flow[e.to] && e.cap>e.flo)
{
path[e.to]=vect[x][i];
flow[e.to]=min(flow[e.from],e.cap-e.flo);
que.push_back(e.to);
}
}
if(flow[e]) return flow[e];
}
return flow[e];
} int cal(int s, int e)
{
int big_flow=;
while(true)
{
memset(flow,,sizeof(flow));
memset(path,,sizeof(path)); int tmp=bfs(s,e);
if(!tmp) return big_flow;
big_flow+=tmp;//统计流 int ed=e;
while(ed!=s)
{
int t=path[ed];
edge[t].flo+=tmp;
edge[t^].flo-=tmp;
ed=edge[t].from;
}
}
} int main()
{
freopen("input.txt", "r", stdin);
int n, m, t, k;
char s3[], s4[];
string s1,s2;
cin>>t;
while(t--)
{
has.clear();
dev.clear();
plug.clear();
chg.clear(); memset(edge,,sizeof(edge));
edge_cnt=;
for(int i=; i<; i++) vect[i].clear();
int num=; scanf("%d",&n); //插座
for(int i=; i<n; i++)
{
scanf("%s",s3);
s1=s3;
if(!has[s1]) has[s1]=++num;
plug.push_back(has[s1]);
} scanf("%d",&m); //设备
for(int i=; i<m; i++)
{
scanf("%s%s",s3,s4);
s1=s3;
s2=s4;
if(!has[s2]) has[s2]=++num;
dev.push_back(has[s2]);
} scanf("%d",&k); //转换头
for(int i=; i<k; i++)
{
scanf("%s%s",s3,s4);
s1=s3;
s2=s4;
if(!has[s1]) has[s1]=++num;
if(!has[s2]) has[s2]=++num;
chg.push_back(make_pair(has[s1], has[s2]));
}
build_graph(n, m, k, num+);
printf("%d\n",m-cal(, num+));
if(t) printf("\n");
}
return ;
} AC代码
AC代码
//#pragma comment(linker,"/STACK:102400000,102400000")
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <stack>
#include <algorithm>
#include <map>
#include <bits/stdc++.h>
#define LL long long
#define pii pair<int,int>
#define INF 0x7f7f7f7f
using namespace std;
const int N=+;
unordered_map<string, int> has;
vector<int> dev;
vector<int> plug;
vector<pii> chg;
vector<int> vect[];
int path[];
int flow[];
int edge_cnt; struct node
{
int from;
int to;
int cap;
int flo;
}edge[N]; void add_node(int fr,int t,int ca,int fl)
{
edge[edge_cnt].from=fr;
edge[edge_cnt].to=t;
edge[edge_cnt].cap=ca;
edge[edge_cnt].flo=fl;
vect[fr].push_back(edge_cnt++);
} void build_graph(int n, int m, int k, int hui)
{
//每种型号的插口是个点
for(int i=; i<chg.size(); i++) //转换器自身
{
int in=chg[i].first;
int out=chg[i].second; add_node(in, out, INF, ); //应该是四条边
add_node(out, in, , );
} for(int i=; i<dev.size(); i++) //添加源点0号
{
int in=dev[i];
add_node(, in, , );
add_node(in, , , );
} for(int i=; i<plug.size(); i++) //添加汇点
{
int out=plug[i];
add_node(out, hui, , );
add_node(hui, out, , );
}
} int spfa(int s,int e)
{
deque<int> que;
que.push_back(s);
flow[s]=INF;
while(!que.empty())
{
int x=que.front();
que.pop_front();
for(int i=; i<vect[x].size(); i++)
{
node e=edge[vect[x][i]];
if(!flow[e.to] && e.cap>e.flo)
{
path[e.to]=vect[x][i];
flow[e.to]=min(flow[e.from],e.cap-e.flo);
que.push_back(e.to);
}
}
if(flow[e]) return flow[e];
}
return flow[e];
} int cal(int s, int e)
{
int big_flow=;
while(true)
{
memset(flow,,sizeof(flow));
memset(path,,sizeof(path)); int tmp=spfa(s,e);
if(!tmp) return big_flow;
big_flow+=tmp;//统计流 int ed=e;
while(ed!=s)
{
int t=path[ed];
edge[t].flo+=tmp;
edge[t^].flo-=tmp;
ed=edge[t].from;
}
}
} int main()
{
freopen("input.txt", "r", stdin);
int n, m, t, k;
char s3[], s4[];
string s1,s2;
cin>>t;
while(t--)
{
has.clear();
dev.clear();
plug.clear();
chg.clear(); memset(edge,,sizeof(edge));
edge_cnt=;
for(int i=; i<; i++) vect[i].clear();
int num=; scanf("%d",&n); //插座
for(int i=; i<n; i++)
{
scanf("%s",s3);
s1=s3;
if(!has[s1]) has[s1]=++num;
plug.push_back(has[s1]);
} scanf("%d",&m); //设备
for(int i=; i<m; i++)
{
scanf("%s%s",s3,s4);
s1=s3;
s2=s4;
if(!has[s2]) has[s2]=++num;
dev.push_back(has[s2]);
} scanf("%d",&k); //转换头
for(int i=; i<k; i++)
{
scanf("%s%s",s3,s4);
s1=s3;
s2=s4;
if(!has[s1]) has[s1]=++num;
if(!has[s2]) has[s2]=++num;
chg.push_back(make_pair(has[s1], has[s2]));
}
build_graph(n, m, k, num+);
printf("%d\n",m-cal(, num+));
if(t) printf("\n");
}
return ;
}
AC代码
UVA 753 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 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(网络流)
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(最大流)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 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 题目意思有点儿难描写叙述 用一个别人描写叙述好的. 我的 ...
- UVA - 753 A Plug for UNIX(网络流)
题意 给定一些插头设备和插座,有一些方法可以把其中一些插头变成另一种插头.求无法匹配插座的插头设备个数. 题解 用\(map\)给每个字符串标号为\(a_i\)和\(b_i\). 读入每种改变插头的方 ...
- UVa 753 A Plug for UNIX (最大流)
题意:给定 n 种插座,m种设备,和k个转换器,问你最少有几台设备不能匹配. 析:一个很裸的网络流,直接上模板就行,建立一个源点s和汇点t,源点和每个设备连一条边,每个插座和汇点连一条边,然后再连转换 ...
- UVA 753 A Plug for UNIX (最大流)
关键在建图,转换器连一条容量无限的边表示可以转化无数次,设备的插头连源点,插座连汇点. dinic手敲已熟练,输出格式又被坑,总结一下,输出空行多case的,一个换行是必要的,最后一个不加空行,有Te ...
随机推荐
- java 中的异常处理
一. 异常的概念和Java异常体系结构 异常是程序运行过程中出现的错误.本文主要讲授的是Java语言的异常处理.Java语言的异常处理框架, 是Java语言健壮性的一个重要体现. Java把 ...
- [msf]那些年儿跑过的字典
SEC标签里都会说一些网络完全相关的,光说理论也不好,光将工具太肤浅,不做脚本小子,有一句话说的好,我们都知道最酷的是什么?酷的不是“h4ck3r”这两个字,而是技术. OK,-let's go!! ...
- asp .NET弹出窗口 汇总(精华,麒麟创想)
asp .NET弹出窗口 汇总(精华,麒麟创想) 注://关闭,父窗口弹出对话框,子窗口直接关闭 this.Response.Write("<script language=javas ...
- dive into python 读笔(2)
chapter 4 自省, summary: # 用可选和命名参数定义和调用函数 # 用 str 强制转换任意值为字符串形式 # 用 getattr 动态得到函数和其它属性的引用 # 扩展列表解析语法 ...
- ios设备 分辨率(转)
1 iOS设备的分辨率 iOS设备,目前最主要的有3种(Apple TV等不在此讨论),按分辨率分为两类 iPhone/iPod Touch 普屏分辨率 320像素 x 480像素 Retina ...
- 1040: [ZJOI2008]骑士 - BZOJ
Description Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬.最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争.战火 ...
- 1015: [JSOI2008]星球大战starwar - BZOJ
Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过 ...
- winform访问url传参有返回值
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Net;usi ...
- Hibernate关系级别注解
最近在学习Hibernate的相关知识,这一站学习的是Hibernate的注解相关的操作和知识.在这里标注以下为以后查阅和需要帮助的朋友提供便利. 一. 开发环境的搭建: 1. 需要的jar包配置: ...
- 关于ax+by=c的解x,y的min(|x|+|y|)值问题
首先我们移动一下项,并强行让a>b. 然后我们可以画出这样一个图像 我们发现,在线段l与x轴交点处的下方,x,y的绝度值是递增的,所以我们不考虑那个最小点在下端. 之后我们发现在点的上端,因为斜 ...