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 ...
随机推荐
- 关于python多线程编程中join()和setDaemon()的一点儿探究
关于python多线程编程中join()和setDaemon()的用法,这两天我看网上的资料看得头晕脑涨也没看懂,干脆就做一个实验来看看吧. 首先是编写实验的基础代码,创建一个名为MyThread的 ...
- Fedora 17下安装Oracle 10g详细图文教程
一.硬件要求——内存 & swap & 硬盘 最小内存与swap: 1 GB of RAM & swap 建议内存与swap: 2 GB of RAM & swap [ ...
- having count(*) > 1
select phone from aa group by phone having count(*) > 1 后面的having子句是什么意思啊? 以phone分组,分组后每组里面phone出 ...
- 灵光乍现,lua数据绑定
MVVM的核心就是数据驱动,数据驱动的核心就是数据绑定. 我一直在思考,如何使用lua做一个数据绑定的功能,仔细思考一下,数据绑定需要做到的功能很简单,就是当一个数据改变时,能主动回调一个或多个函数就 ...
- EXTJS4.2 控件之Grid 行点击事件
listeners: { 'itemclick': function (view, record, item, index, e) { //Ext.MessageBox.alert("标题& ...
- Hibernate从入门到精通(二)Hibernate实例演示
上篇Hibernate从入门到精通(一)JDBC简介,我们主要对JDBC进行了简单介绍和使用说明,这次我们做一个Hibernate简单实例,通过这个实例对比Hibernate和JDBC,了解Hiber ...
- ios实现截屏(转)
-(UIImage*) makeImage { UIGraphicsBeginImageContext(self.view.bounds.size); [self.view.layer rende ...
- WdatePicker 设置开始时间和结束时间
开始时间: <input type="text" placeholder=" -请选择- " readonly="readonly" ...
- JavaScript中常谈的对象
为浏览器编写代码时,总少不了window对象 window对象表示JavaScript程序的全局环境 同时 也表示应用的主窗口 到处都是对象 window对象 常用的属性和方法介绍 location ...
- 云风的BLOG❳可靠 UDP 传输
http://mp.weixin.qq.com/s?__biz=MzA3NjYxOTA0MQ==&mid=405432715&idx=1&sn=2e40ceafd4b298e1 ...