POJ-1087 A Plug for UNIX (网络流)
思路
电器数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 (网络流)的更多相关文章
- 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 / 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 ...
- poj 1087 A Plug for UNIX(字符串编号建图)
A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14862 Accepted: 5026 ...
- kuangbin专题专题十一 网络流 POJ 1087 A Plug for UNIX
题目链接:https://vjudge.net/problem/POJ-1087 题目:有n个插座,插座上只有一个插孔,有m个用电器,每个用电器都有插头,它们的插头可以一样, 有k个插孔转化器, a ...
- 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.每找到以条增广路回退到断点(常数优化 ...
- poj 1087 A Plug for UNIX
题目描述:现在由你负责布置Internet联合组织首席执行官就职新闻发布会的会议室.由于会议室修建时被设计成容纳全世界各地的新闻记者,因此会议室提供了多种电源插座用以满足(会议室修建时期)各国不同插头 ...
- 【poj 1087 a plug for UNIX】
在大米饼的帮助下,终于找到了大米饼程序中如同大米饼一般的错误! 考点在问题转化,然后就跑一个你喜欢的最大流算法(二分图可以啵?) 再来一个例子吧: [纯手绘大米饼图片] 其中有的边权是1,否则就是in ...
- 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 ...
- 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 ...
随机推荐
- 【译】PHP 内核 — 字符串管理
[译]PHP 内核 - 字符串管理 (Strings management: zend_string 译文) 原文地址:http://www.phpinternalsbook.com/php7/int ...
- python 操作 word 图片 消失
问题描述修改word中文本,如下代码,保存时会导致word中的部分图片消失 from docx import Document path1 = 'test_in.docx' path2 = 'test ...
- 浅谈ABB机器人(工具坐标,工件坐标,有效载荷)
工具坐标(tool): 使tcl坐标偏移到工具上,例如焊接工作,使机器人工作点切入焊枪点上 mass:工具的重量 xyz:偏移距离的大小 验证:通过手动模式,切换至自定义工具,重定向 工件坐标(wob ...
- command failed: npm install --loglevel error --registry=https://registry.npm 用vue-cli 4.0 新建项目总是报错
昨天新买的本本,今天布环境,一安装vue-cli发现都4.0+的版本了,没管太多,就开始新建个项目感受哈,一切运行顺利,输入 "vue create app" 的时候,一切貌似进展 ...
- c# excel 读写 64位操作系统 64位excel
用c#读写excel时,会出现 “本机未注册Microsoft.ACE.OLEDB.12.0 驱动(什么的,忘了)” 读写 64位的excel 时,要在项目属性里改一下目标平台,默认的为*86, 改为 ...
- android 使用系统级别权限
java.lang.SecurityException: Neither user 10027 nor current process has android.permission.MODIFY_PH ...
- 情人节用Python智能聊天机器人的实现|制作一个虚拟恋人
首先项目需要的包 import urllib.request import urllib.parse from tkinter import * import time PS:另外很多人在学习Pyth ...
- ES+VBA 实现批量添加网络图片
需求:通过自动读取相对应列的图片网址,自动添加到图片列,从而完成添加图片 案例:需要将备注列的图片网址添加到图片列的内容 关键代码 '引入API Private Declare Function UR ...
- 搭建 Kubernetes 高可用集群
使用 3 台阿里云服务器(k8s-master0, k8s-master1, k8s-master2)作为 master 节点搭建高可用集群,负载均衡用的是阿里云 SLB ,需要注意的是由于阿里云负载 ...
- IIS的部署(二)------虚拟目录的使用
IIS的虚拟目录 一个站点的网页的存储位置目录是固定的,而且结构和物理保存网页的磁盘路径相同.例如:默认网页的存储位置是c:\inetpub\wwwroot,当访问localhost即访问c:\ine ...