思路

电器数1 ~ 100,附带100种接口,注意题目:You notice that some of the devices use plugs for which there is no receptacle.

墙上接口1 ~ 100,还有1 ~ 100转接口,转接口有两个接口。假设所有的接口类型都不相同,接口就有400种。所以汇点值 >= 501。

读入电器的时候,它的接口可能在墙上并没有,所以要判断在此之前是否已经给该接口分配过标号。

读入转接口的时候,先读入两个转接口,然后分别判断是否已经分配过标号,最后在 转接口入口 -> 转接口出口 ,这两个接口之间建立一条INF的边。

#include <iostream>
#include <stdio.h>
#include <map>
#include <queue>
#include <string>
#include <string.h>
using namespace std; const int INF=0x3f3f3f3f;
const int maxn=110;
map<string,int>rec,dev;
int dev_cnt=1,rec_cnt=101;
int n,m,k; struct Edge {
int to,cap,flow;
}; struct Dinic {
int cnt,s,t;
int head[6*maxn];
int deep[6*maxn];
int vis[6*maxn];
int cur[6*maxn];
int next[maxn*maxn*4];
Edge edge[maxn*maxn*4]; void addEdge(int u,int v,int w)
{
edge[cnt].to=v;
edge[cnt].cap=w;
edge[cnt].flow=0;
next[cnt]=head[u];
head[u]=cnt++; edge[cnt].to=u;
edge[cnt].cap=0;
edge[cnt].flow=0;
next[cnt]=head[v];
head[v]=cnt++;
} void init()
{
memset(head,-1,sizeof(head));
cnt=0;
s=0;
t=601;
} bool bfs()
{
memset(vis,0,sizeof(vis));
memset(deep,0,sizeof(deep));
deep[s]=0;
vis[s]=1;
queue<int> q;
q.push(s);
while (!q.empty()) {
int u=q.front();
q.pop();
for (int i=head[u];i!=-1;i=next[i]) {
Edge &e=edge[i];
if (!vis[e.to]&&e.cap>e.flow) {
vis[e.to]=1;
deep[e.to]=deep[u]+1;
q.push(e.to);
}
}
}
return vis[t];
} int dfs(int u,int in)
{
if (in==0||u==t) {
return in;
}
int f=0,out=0;
for (int &i=cur[u];i!=-1;i=next[i]) {
Edge &e=edge[i];
if (deep[e.to]==deep[u]+1&&(f=dfs(e.to,min(in,e.cap-e.flow)))>0) {
edge[i].flow+=f;
edge[i^1].flow-=f;
in-=f;
out+=f;
if (in==0) {
break;
}
}
}
return out;
} int maxflow()
{
int ans=0;
while (bfs()) {
for (int i=0;i<6*maxn;i++) {
cur[i]=head[i];
}
ans+=dfs(s,INF);
}
return ans;
} }DC; int main()
{
DC.init();
cin>>n;
string tmp;
for (int i=0;i<n;i++) {
cin>>tmp;
rec[tmp]=rec_cnt;
DC.addEdge(rec_cnt,601,1);
rec_cnt++;
}
cin>>m;
for (int i=0;i<m;i++) {
cin>>tmp;
dev[tmp]=dev_cnt;
DC.addEdge(0,dev_cnt,1);
cin>>tmp;
if (rec[tmp]==0) {
rec[tmp]=rec_cnt++;
}
DC.addEdge(dev_cnt,rec[tmp],1);
dev_cnt++;
}
cin>>k;
string in,out;
for (int i=0;i<k;i++) {
cin>>in;
cin>>out;
if (rec[in]==0) {
rec[in]=rec_cnt++;
}
if (rec[out]==0) {
rec[out]=rec_cnt++;
}
DC.addEdge(rec[in],rec[out],INF);
}
printf("%d\n",dev_cnt-1-DC.maxflow());
return 0;
}

POJ-1087 A Plug for UNIX (网络流)的更多相关文章

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

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

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

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

  4. kuangbin专题专题十一 网络流 POJ 1087 A Plug for UNIX

    题目链接:https://vjudge.net/problem/POJ-1087 题目:有n个插座,插座上只有一个插孔,有m个用电器,每个用电器都有插头,它们的插头可以一样, 有k个插孔转化器, a ...

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

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

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

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

  7. poj 1087 A Plug for UNIX

    题目描述:现在由你负责布置Internet联合组织首席执行官就职新闻发布会的会议室.由于会议室修建时被设计成容纳全世界各地的新闻记者,因此会议室提供了多种电源插座用以满足(会议室修建时期)各国不同插头 ...

  8. 【poj 1087 a plug for UNIX】

    在大米饼的帮助下,终于找到了大米饼程序中如同大米饼一般的错误! 考点在问题转化,然后就跑一个你喜欢的最大流算法(二分图可以啵?) 再来一个例子吧: [纯手绘大米饼图片] 其中有的边权是1,否则就是in ...

  9. poj 1087 C - A Plug for UNIX 网络流最大流

    C - A Plug for UNIXTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contes ...

  10. uva753 A Plug for UNIX 网络流最大流

    C - A Plug for UNIX    You are in charge of setting up the press room for the inaugural meeting of t ...

随机推荐

  1. HDU4714 Tree2cycle 解题报告

    题意 给定一棵无根树,删除或连接一条边的代价为\(1\),求把树变为环的最小代价. 前置思路 如果删除了\(k\)条边,使得树变成\((k+1)\)条链,再用\((k+1)\)次连接操作把树变成一个环 ...

  2. ansible笔记(6):常用模块之系统模块

    1.cron模块 cron命令是计划任务功能,与crontab功能类似. 示例:时间格式--->>>分  时  日  月 星期 10 12 27 * *  tar -cvzf log ...

  3. pyodbc-的一些说明

    cursor的description 可以获得一些关于表的信息 info=cursor.description 此时表中有多少列就有多少个元素,一个元素就是一列的信息(格式是tuple),所以这里的i ...

  4. git上传时出现ERROR: Repository not found.的解决办法

    今天在上传时出现错误,原因是之前更改了gitee上的个人空间地址,导致找不到.需要重新配置 https://gitee.com/help/articles/4114#article-header0

  5. C语言随笔5:函数、函数指针

    函数 C语言中函数参数传递时,将实参的值拷贝到函数参数的存储区中.这种传递参数的方式称为按值传递. 函数不会访问实参本身,访问的是函数存储在栈区的副本,不会改变实参.函数凋用结束,函数在栈区的内容释放 ...

  6. opencv:轮廓逼近与拟合

    轮廓逼近,本质上是减少编码点 拟合圆,生成最相似的圆或椭圆 #include <opencv2/opencv.hpp> #include <iostream> using na ...

  7. AcWing 858. Prim算法求最小生成树 稀疏图

    //稀疏图 #include <cstring> #include <iostream> #include <algorithm> using namespace ...

  8. 【C语言】判断学生成绩等级

    方法一:if-else #include<stdio.h> int main() { printf("请输入成绩:\n"); float score; scanf_s( ...

  9. CentOS7中Tomcat的安装和配置以及启动配置tomcat。启动过程中的易错点

    Tomcat运行需要设置JRE目录,全局变量配置,请参见: Linux下JDK的安装和配置   当然也可以直接修改Tomcat的配置文件,请自行度娘   1.下载并解压 请先去官网找到需要下载的tom ...

  10. Python 语法特点:注释/编写规则/命名规范

    1.注释 1)单行注释     # 2) 多行注释   前后三个单引号或双引号   ‘’‘  ...  '''    """  ...""" ...