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 ...
随机推荐
- webpack核心提炼
基本是学习的时候在网上整理的资料,并非自己原创,这篇文章的的主要目的是记录webpack.config.js的配置方式.可能也有不少错误,欢迎指正!! 一.应用场景 前端模块化开发.功能拓展.css预 ...
- wafII笔记
wafII笔记: 组件的使用方法: 组件属性: 属性的设置和获取通过option方法来完成 waf("#id").wafProm ...
- 为 Elasticsearch 添加中文分词,对比分词器效果
转自:http://keenwon.com/1404.html 为 Elasticsearch 添加中文分词,对比分词器效果 Posted in 后端 By KeenWon On 2014年12月12 ...
- vue_music:排行榜rank中top-list.vue中样式的实现:class
排行榜的歌曲列表,根据排名渲染不同的样式,同时需要考虑分辨率的2x 3x图 不同的样式--:class 考虑分辨率的2x 3x图--需要在stylu中的mixin中bgImage根据屏幕分辨率选择图片 ...
- 【ACM】hdu_1042_N!_201308071639
N!Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...
- [bzoj4027][HEOI2015]兔子与樱花_贪心_树形dp
兔子与樱花 bzoj-4027 HEOI-2015 题目大意:每个点有c[i]朵樱花,有一个称重m, son[i]+c[i]<=m.如果删除一个节点,这个节点的樱花或移动到它的祖先中深度最大的, ...
- ReentrantLock公平锁与非公平锁lock()方法去竞争锁的不同点
- Android Internet - WebView 的使用
WebView是Android 提供的操作网页的一个组件. 用于浏览网页及其它Internet资源. 这里总结了一些WebView 的经常使用接口.和2个小演示样例程序用于自己开发时直接使用.就不用再 ...
- [剑指offer]Q13:O(1)时间删除链表的结点
通常我们所说的删除链表的某个结点,是彻底删除该结点的空间.而要这么做就必须知道其前驱结点.这里的想法是,链表中存储的val是同类型的,仅仅要将该结点的val内容删除就能够了. 那么就能够用该结点的后继 ...
- 使用java -jar运行jar包
使用java -jar运行jar包 摘要 其实不难,网上搜了不少,但是讲不全,或者讲得不少,有的细节却漏掉了, 在此,我想将详细点 java -jar 1.其实命令比较简单:进入相应的目录后,执行ja ...