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 ...
随机推荐
- java操作Excel的poi 设置单元格的对其方式
设置单元格的对其方式 package com.java.poi; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.po ...
- eoLinker上线两周年+ AMS V4.0 发布:全新UI界面,带来领先的API开发管理解决方案!
2018年7月,eoLinker 发布了<eoLinker AMS 2018年年中用户调研问卷>,前后经历一周的时间,共收集到超过1000份有效调查问卷.超过300个有效改进意见. eoL ...
- Google Shell Style Guide
转自:http://google.github.io/styleguide/shell.xml Shell Style Guide Revision 1.26 Paul Armstrong Too m ...
- 51nod1117 聪明的木匠【贪心+优先队列】
一位老木匠需要将一根长的木棒切成N段.每段的长度分别为L1,L2,......,LN(1 <= L1,L2,-,LN <= 1000,且均为整数)个长度单位.我们认为切割时仅在整数点处切且 ...
- 爬虫数据使用MongDB保存时自动过滤重复数据
本文转载自以下网站: 爬虫断了?一招搞定 MongoDB 重复数据 https://www.makcyun.top/web_scraping_withpython13.html 需要学习的地方: Mo ...
- 43.mapping的理解
主要知识点: mapping的理解 (1)往es里面直接插入数据,es会自动建立索引,同时建立type以及对应的mapping (2)mapping中就自动定义了每个field的数据类型. ( ...
- netty byteBuf (二)
netty重新定义了byteBuf 而没使用jdk byteBuffer netty byteBuf与jdk byteBuffer的区别 (1)jdk buffer长度固定 byteBuf超过最大 ...
- java陷阱之自动拆箱
项目中突然报空指针异常 Integer code=null; code=code==null?500:code; 排查发现三元运算符空指针异常,表面上看不出来有什么问题,编译器编译的时候会保证:2边数 ...
- java IO(BIO)、NIO、AIO
IO 服务端ServerSocket 客户端Socket 缺点每次客户端建立连接都会另外启一个线程处理.读取和发送数据都是阻塞式的. 如果1000个客户端建立连接将会产生1000个线程 Server端 ...
- MySQL日志格式 binlog_format
MySQL 5.5 中对于二进制日志 (binlog) 有 3 种不同的格式可选:Mixed,Statement,Row,默认格式是 Statement.总结一下这三种格式日志的优缺点. MySQL ...