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 ...
随机推荐
- Throwing cards away I
Throwing cards away I Given is an ordered deck of n cards numbered 1 to n with card 1 at the top a ...
- 【BZOJ 2440】[中山市选2011]完全平方数
Description 小 X 自幼就很喜欢数.但奇怪的是,他十分讨厌完全平方数.他觉得这些数看起来很令人难受.由此,他也讨厌所有是完全平方数的正整数倍的数.然而这丝毫不影响他对其他数的热爱. 这天是 ...
- 【POJ2104】kth num
You are working for Macrohard company in data structures department. After failing your previous tas ...
- jsf2.0视频
jsf2.0 入门视频 教程 需要的看下.初次录视频.还有很多需要完善. JSF交流QQ群84376982 JSF入门视频下载地址 http://pan.baidu.com/s/1jG3y4T4 ...
- Telerik RadGridView 右键菜单如何设置?
问题: 我想去掉红线框住的部分,希望有会的网友帮助我,谢谢! 解决方法: 默认: 修改: [利用 ContextMenuOpening 事件,对应你的项目,你要自己修改那判断的字符串(你的中文)] p ...
- JavaScript与DOM的关系
JavaScript与浏览器的工作 1.浏览器获取并加载你的页面,从上至下解析它的内容. 遇到JavaScript时,浏览器会解析代码,检查它的正确性,然后执行代码. 浏览器还会建立一个HTML页面的 ...
- SQL SERVER数据导入
我的博客已好久没有文字方面的记载了,好歹昨天已经结束软件设计师的考试了,今天怎么说也需要锻炼自己的写作能力.不然真怕自己又像上一年一样,一停就一年多了. 想好好学习数据库(SQL SERVER)方面的 ...
- 【string】KMP, 扩展KMP,trie,SA,ACAM,SAM,最小表示法
[KMP] 学习KMP,我们先要知道KMP是干什么的. KMP?KMPLAYER?看**? 正如AC自动机,KMP为什么要叫KMP是因为它是由三个人共同研究得到的- .- 啊跑题了. KMP就是给出一 ...
- [转载]Unity3D 访问Access数据库
在开始这个小教程之前呢,其实在网上你已经可以找到相关的资料了,但是我还是要把我自己做练习的一点东西分享出来.写这个教程的主要原因呢,是一个朋友在u3d的官网论坛里,找到了这个demo,但是在他使用的过 ...
- [machine learning] Loss Function view
[machine learning] Loss Function view 有关Loss Function(LF),只想说,终于写了 一.Loss Function 什么是Loss Function? ...