hdu 1087 A Plug for UNIX 最大流
题意:http://www.phpfans.net/article/htmls/201012/MzI1MDQw.html
1、在一个会议室里有n种插座,每种插座一个;
2、每个插座只能插一种以及一个电器(或者适配器);
3、有m个电器,每个电器有一个插头需要插在相应一种插座上;
4、不是所有电器都能在会议室找到相应插座;
5、有k种适配器,每种适配器可以有无限多数量;
6、每种适配器(a, b)可以把b类插座变为a类插座;
7、问最后有多少个电器无法使用。
分析:图片来源:http://www.cnblogs.com/longdouhzt/archive/2011/09/03/2165860.html

网上给出的很直观的建图方式。看着很舒心,但是要注意。每种适配器(a, b)可以把b类插座变为a类插座。
在图中,B->X, X->A,X->D是可以的,但是没有反向边。
这个不知道的话,一直错。我以为是自己哪里错掉了。还换了算法做。
SAP: 0MS
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<algorithm>
using namespace std; const int N=;
const int M=*N*N, INF=0x3f3f3f3f;
struct node
{
int to,next,w;
}edge[M];
int head[N],numh[N],h[N],cure[N],pre[N];
int ans,tot;
map<string,int> st;
void SAP(int s, int e,int n)
{
int flow,u,tmp,neck,i;
ans=;
for(i=;i<=n;i++)
cure[i]=head[i];
numh[]=n;
u=s;
while(h[s]<n)
{
if(u==e)
{
flow =INF;
for(i=s;i!=e;i=edge[cure[i]].to)
{
if(flow>edge[cure[i]].w)
{
neck=i;
flow =edge[cure[i]].w;
}
}
for(i=s;i!=e;i=edge[cure[i]].to)
{
tmp=cure[i];
edge[tmp].w-=flow;
edge[tmp^].w+=flow;
}
ans+=flow;
u=neck;
}
for(i=cure[u];i!=-;i=edge[i].next)
if(edge[i].w && h[u]==h[edge[i].to]+) break;
if(i!=-) {cure[u]=i;pre[edge[i].to]=u;u=edge[i].to;}
else
{
if(==--numh[h[u]]) break;
cure[u]=head[u];
for(tmp=n,i=head[u];i!=-;i=edge[i].next)
if(edge[i].w) tmp=min(tmp, h[edge[i].to]);
h[u]=tmp+;
++numh[h[u]];
if(u!=s) u=pre[u];
}
}
}
void init()
{
tot=;
memset(head,-,sizeof(head));
memset(pre,-,sizeof(pre));
memset(h,,sizeof(h));
memset(numh,,sizeof(numh));
st.clear(); }
void addedge(int i,int j,int w)
{
edge[tot].to=j;edge[tot].w=w;edge[tot].next=head[i];head[i]=tot++;
edge[tot].to=i;edge[tot].w=;edge[tot].next=head[j];head[j]=tot++;
}
char str[],ch[];
int main()
{
//freopen("test.txt","r",stdin);
int s,e,i,j,k,a,b,c,t;
init();
s=;e=;t=;
scanf("%d",&a);
for(i=;i<=a;i++){
scanf("%s",str);
if(st[str]==) st[str]=++t;
addedge(st[str],e,);
}
scanf("%d",&b);
for(i=;i<=b;i++){
scanf("%s%s",ch,str);
if(st[ch]==) st[ch]=++t;
if(st[str]==) st[str]=++t;
addedge(st[ch],st[str],);
addedge(s,st[ch],);
}
scanf("%d",&c);
for(i=;i<=c;i++){
scanf("%s%s",ch,str);
if(st[ch]==) st[ch]=++t;
if(st[str]==) st[str]=++t;
addedge(st[ch],st[str],INF);
//addedge(st[str],st[ch],INF);
}
SAP(s,e,t);
printf("%d\n",b-ans);
return ;
}
EK: 32MS
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
map<string,int> st;
const int N=, INF=0x3f3f3f3f;
int Map[N][N],pre[N],n,ans;
bool vis[N];
queue<int> que;
bool EK_bfs(int s,int e)
{
int i,k;
while(!que.empty()) que.pop();
memset(vis,,sizeof(vis));
memset(pre,-,sizeof(pre));
que.push(s);
vis[s]=;
while(!que.empty())
{
k=que.front();
if(e==k) return ;
que.pop();
for(i=;i<=n;i++)
{
if(Map[k][i]&&!vis[i])
{
vis[i]=;
pre[i]=k;
que.push(i);
}
}
}
return ;
}
void EK(int s,int e)
{
int u,mn;
ans=;
while(EK_bfs(s,e))
{
mn=INF;
u=e;
while(pre[u]!=-)
{
mn=min(mn,Map[pre[u]][u]);
u=pre[u];
}
ans+=mn;
u=e;
while(pre[u]!=-)
{
Map[pre[u]][u]-=mn;
Map[u][pre[u]]+=mn;
u=pre[u];
}
}
}
void init()
{
memset(Map,,sizeof(Map));
}
char str[],ch[];
int main()
{
//freopen("test.txt","r",stdin);
int s,e,i,j,k,a,b,c,t;
init();
s=;e=;t=;
scanf("%d",&a);
for(i=;i<=a;i++){
scanf("%s",str);
if(st[str]==) st[str]=++t;
Map[st[str]][e]=;
}
scanf("%d",&b);
for(i=;i<=b;i++){
scanf("%s%s",ch,str);
if(st[ch]==) st[ch]=++t;
if(st[str]==) st[str]=++t;
Map[st[ch]][st[str]]=;
Map[s][st[ch]]=;
}
scanf("%d",&c);
for(i=;i<=c;i++){
scanf("%s%s",ch,str);
if(st[ch]==) st[ch]=++t;
if(st[str]==) st[str]=++t;
Map[st[ch]][st[str]]=INF;
//Map[st[str]][st[ch]]=INF;
}
n=t;
EK(s,e);
printf("%d\n",b-ans);
return ;
}
hdu 1087 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 ...
- 【poj1087/uva753】A Plug for UNIX(最大流)
A Plug for UNIX Description You are in charge of setting up the press room for the inaugural meeti ...
- POJ1087:A Plug for UNIX(最大流)
A Plug for UNIX 题目链接:https://vjudge.net/problem/POJ-1087 Description: You are in charge of setting u ...
- POJ1087 A Plug for UNIX —— 最大流
题目链接:https://vjudge.net/problem/POJ-1087 A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K T ...
- poj 1087 A Plug for UNIX(字符串编号建图)
A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14862 Accepted: 5026 ...
- POJ 1087 A Plug for UNIX (网络流,最大流)
题面 You are in charge of setting up the press room for the inaugural meeting of the United Nations In ...
- poj 1087 A Plug for UNIX 【最大流】
题目连接:http://poj.org/problem? id=1087 题意: n种插座 ,m个电器,f组(x,y)表示插座x能够替换插座y,问你最多能给几个电器充电. 解法:起点向插座建边,容量1 ...
- poj 1087.A Plug for UNIX (最大流)
网络流,关键在建图 建图思路在代码里 /* 最大流SAP 邻接表 思路:基本源于FF方法,给每个顶点设定层次标号,和允许弧. 优化: 1.当前弧优化(重要). 1.每找到以条增广路回退到断点(常数优化 ...
- TZOJ 1911 A Plug for UNIX(最大流)
描述 You are in charge of setting up the press room for the inaugural meeting of the United Nations In ...
随机推荐
- Unity与Android通信的中间件
2.1.1 Fragment和Activity都需要实现的接口——IBaseView/** * Description:Basic interface of all {@link Activity} ...
- GFS分布式文件系统理论个人总结
GlusterFS 两种模式 可以通过TCP/IP和RDMA高速网络互连,客户端可通过原生Gluster协议访问数据 没有GlusterFS客户端的可以通过NFS/CIFS标准协议通过存储网关访问数据 ...
- appium+python,app自动化测试框架
目前正在写一个app的自动化UI测试框架,目录结构如, 脚本还在调试,实现的方法是从excel表格读取测试用例,执行完成后会将结果保存到Excel中. 等待.......
- 在Ubuntu环境下搭建esp32开发环境
第一步:下载必要的库文件 sudo apt-get install git make gcc libncurses5-dev flex bison gperf python-serial ...
- Python画三维图-----插值平滑数据
一.二维的插值方法: 原始数据(x,y) 先对横坐标x进行扩充数据量,采用linspace.[如下面例子,由7个值扩充到300个] 采用scipy.interpolate中的spline来对纵坐标数据 ...
- 11.5 【Linq 】连接
11.5.1 使用 join 子句的内连接 如果你打算把一个巨大的序列连接到一个极小的序列上,应尽可能把小序列作为右边序列 class Program { static void Main(strin ...
- Linux放弃到入门
流星,因为短暂而美丽,划过黑寂的夜空,释放出那一闪而逝的光芒,虽然微弱,但却没有人能无视它的存在.人生如同流星,充满了精彩与传奇,如同一支美丽的传说,究竟能否想流星那样短暂,别人决定不了,上天也决定不 ...
- 继续聊WPF——自定义CheckBox控件外观
上一篇文章中谈到了BulletDecorator控件,就是为自定义CheckBox控件的模板做准备,因为CheckBox需要比较严格的布局,正好,BulletDecorator控件就合适了,该控件的布 ...
- 大数据学习[16]--使用scroll实现Elasticsearch数据遍历和深度分页[转]
题目:使用scroll实现Elasticsearch数据遍历和深度分页 作者:星爷 出处: http://lxWei.github.io/posts/%E4%BD%BF%E7%94%A8scroll% ...
- CODEVS——T 1297 硬币
http://www.codevs.cn/problem/1297/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Descrip ...