题意: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 最大流的更多相关文章

  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. 【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 ...

  3. POJ1087:A Plug for UNIX(最大流)

    A Plug for UNIX 题目链接:https://vjudge.net/problem/POJ-1087 Description: You are in charge of setting u ...

  4. POJ1087 A Plug for UNIX —— 最大流

    题目链接:https://vjudge.net/problem/POJ-1087 A Plug for UNIX Time Limit: 1000MS   Memory Limit: 65536K T ...

  5. poj 1087 A Plug for UNIX(字符串编号建图)

    A Plug for UNIX Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14862   Accepted: 5026 ...

  6. 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 ...

  7. poj 1087 A Plug for UNIX 【最大流】

    题目连接:http://poj.org/problem? id=1087 题意: n种插座 ,m个电器,f组(x,y)表示插座x能够替换插座y,问你最多能给几个电器充电. 解法:起点向插座建边,容量1 ...

  8. poj 1087.A Plug for UNIX (最大流)

    网络流,关键在建图 建图思路在代码里 /* 最大流SAP 邻接表 思路:基本源于FF方法,给每个顶点设定层次标号,和允许弧. 优化: 1.当前弧优化(重要). 1.每找到以条增广路回退到断点(常数优化 ...

  9. 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 ...

随机推荐

  1. 【转】虚拟化(五):vsphere高可用群集与容错

    vsphere高级功能需要vcenter server和共享存储的支持才能实现.vsphere的高级功能有 vmotion.storage vmotion.vsphere HA.vsphere DRS ...

  2. js中的数组遍历

    js中的数组遍历是项目中经常用到的,在这里将几种方法做个对比. ! for循环:使用评率最高,也是最基本的一种遍历方式. let arr = ['a','b','c','d','e']; for (l ...

  3. 【深度学习框架】使用PyTorch进行数据处理

      在深度学习中,数据的处理对于神经网络的训练来说十分重要,良好的数据(包括图像.文本.语音等)处理不仅可以加速模型的训练,同时也直接关系到模型的效果.本文以处理图像数据为例,记录一些使用PyTorc ...

  4. 排序算法总结(C++)

    算法复杂度 稳定:如果a原本在b前面,而a=b,排序之后a仍然在b的前面. 不稳定:如果a原本在b的前面,而a=b,排序之后 a 可能会出现在 b 的后面. 时间复杂度:对排序数据的总的操作次数.反映 ...

  5. 50.常用的query查询方式

    主要知识点 match all match multi match range query term query terms query exist query         1.match all ...

  6. python爬虫03:那个叫做 Urllib 的库让我们的 python 假装是浏览器

    相信你已经摸清了 浏览器各种请求的套路 也知道了怎么在手机上进行请求和返回数据的抓取 那么接下来我们就开始来使用 python 了 代码 lu 起来 那么 怎么用 python 写各种请求呢? 今天要 ...

  7. Java设计模式之 — 适配器(Adapter)

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/9400141 今天一大早,你的leader就匆匆忙忙跑过来找到你:“快,快,紧急任务 ...

  8. (31)Spring Boot导入XML配置【从零开始学Spring Boot】

    [来也匆匆,去也匆匆,在此留下您的脚印吧,转发点赞评论: 您的认可是我最大的动力,感谢您的支持] Spring Boot理念就是零配置编程,但是如果绝对需要使用XML的配置,我们建议您仍旧从一个@Co ...

  9. 从命令行配置 Windows 防火墙

    从命令行配置 Windows 防火墙 高级用户可以使用命令行来配置 Windows 防火墙.您可以使用 netsh 命令行工具来进行配置. 下表中的 netsh 命令可用于 Microsoft Win ...

  10. caffe Solve函数

    下面来看Solver<Dtype>::Solve(const char* resume_file) solver.cpp template <typename Dtype> v ...