题意:

  给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 电器插座(最大基数匹配,网络流)的更多相关文章

  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(最大流)

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

  5. UVA 753 A Plug for UNIX

    最大流解决 . 设置源点 0,连接所有设备(device) .设备-插头 -汇点 #include <map> #include <set> #include <list ...

  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. [旧博客]QQ旋风加速漏洞

    漏洞是这样的,用开通QQ会员的账号登录QQ旋风,添加要下载的任务,启动加速后,注销,登录lixian.qq.com 删除刚才添加的离线任务,这时QQ旋风还是在加速那个任务.而你又可以登录QQ旋风添加其 ...

  2. Nginx配置文件变量大全

    $args # 这个变量等于请求行中的参数. $binary_remote_addr # 远程地址的二进制表示 $body_bytes_sent # 已发送的消息体字节数 $content_lengt ...

  3. 干货CentOS6.5_Nginx1.40_Php5.57_MySQL5.5.35编译安装全记录

    http://www.unixdo.com/Unix_Linux/CentOS65_Nginx140_Php557_MySQL5535.html 如果是编译升级php,之前做过nginx的虚拟主机配置 ...

  4. Unity3d读取.csv文件

    原文地址:http://blog.csdn.net/dingkun520wy/article/details/26594991 (一)文件路径 需要把csv文件放在StreamingAssets这个文 ...

  5. python中变量

    在Python中,变量的概念基本上和初中代数的方程变量是一致的. 例如,对于方程式 y=x*x ,x就是变量.当x=2时,计算结果是4,当x=5时,计算结果是25. 只是在计算机程序中,变量不仅可以是 ...

  6. Python系统调用——运行其他程序

    转载:http://blog.csdn.net/ssihc0/article/details/7738527 在Python中可以方便地使用os模块运行其他的脚本或者程序,这样就可以在脚本中直接使用其 ...

  7. PD name 和 comment 互换

    1 PowerDesigner中批量根据对象的name生成comment的脚本 执行方法:Open PDM -- Tools -- Execute Commands -- Run Script --- ...

  8. Aimp3的播放列表 按评分排序 落雨

    如图,添加评分选项,并保存,就可以在下图的选项里找到此选项,并按评分排序 效果图如下:还可以倒置,迅速使评分高的音乐排在播放列表的前面位置!! 转自百度知道: http://zhidao.baidu. ...

  9. HDU 1422 重温世界杯(DP)

    点我看题目 题意 : 中文题不详述. 思路 : 根据题目描述及样例可以看出来,如果你第一个城市选的是生活费减花费大于等于0的时候才可以,最好是多余的,这样接下来的就算是花超了(一定限度内的花超),也可 ...

  10. [itint5]下一个排列

    http://www.itint5.com/oj/#6 首先,试验的时候要拿5个来试,3,4个都太少了.好久没做所以方法也忘了,是先从后往前找到第一个不合顺序的,然后在后面找到比这个大的最小的来交换, ...