poj 1087 A Plug for UNIX 【最大流】
id=1087
题意:
n种插座 ,m个电器,f组(x,y)表示插座x能够替换插座y,问你最多能给几个电器充电。
解法:起点向插座建边,容量1,电器向汇点建边。容量1,插座向电器建边。容量1,能够替换的插座间建边。容量无穷大。然后套板子。
。。求最大流。
代码:
#include <stdio.h>
#include <ctime>
#include <math.h>
#include <limits.h>
#include <complex>
#include <string>
#include <functional>
#include <iterator>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <ctime>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <time.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
using namespace std;
const int MAXN = 1010;//点数的最大值
const int MAXM = 400010;//边数的最大值
const int INF = 0x3f3f3f3f;
struct Edge
{
int to, next, cap, flow;
}edge[MAXM];//注意是MAXM
int tol;
int head[MAXN];
int gap[MAXN], dep[MAXN], pre[MAXN], cur[MAXN];
void init()
{
tol = 0;
memset(head, -1, sizeof(head));
}
//加边。单向图三个參数,双向图四个參数
void addedge(int u, int v, int w, int rw = 0)
{
edge[tol].to = v; edge[tol].cap = w; edge[tol].next = head[u];
edge[tol].flow = 0; head[u] = tol++;
edge[tol].to = u; edge[tol].cap = rw; edge[tol].next = head[v];
edge[tol].flow = 0; head[v] = tol++;
}
//输入參数:起点、终点、点的总数
//点的编号没有影响,仅仅要输入点的总数
int sap(int start, int end, int N)
{
memset(gap, 0, sizeof(gap));
memset(dep, 0, sizeof(dep));
memcpy(cur, head, sizeof(head));
int u = start;
pre[u] = -1;
gap[0] = N;
int ans = 0;
while (dep[start] < N)
{
if (u == end)
{
int Min = INF;
for (int i = pre[u]; i != -1; i = pre[edge[i ^ 1].to])
if (Min > edge[i].cap - edge[i].flow)
Min = edge[i].cap - edge[i].flow;
for (int i = pre[u]; i != -1; i = pre[edge[i ^ 1].to])
{
edge[i].flow += Min;
edge[i ^ 1].flow -= Min;
}
u = start;
ans += Min;
continue;
}
bool flag = false;
int v;
for (int i = cur[u]; i != -1; i = edge[i].next)
{
v = edge[i].to;
if (edge[i].cap - edge[i].flow && dep[v] + 1 == dep[u])
{
flag = true;
cur[u] = pre[v] = i;
break;
}
}
if (flag)
{
u = v;
continue;
}
int Min = N;
for (int i = head[u]; i != -1; i = edge[i].next)
if (edge[i].cap - edge[i].flow && dep[edge[i].to] < Min)
{
Min = dep[edge[i].to];
cur[u] = i;
}
gap[dep[u]]--;
if (!gap[dep[u]])return ans;
dep[u] = Min + 1;
gap[dep[u]]++;
if (u != start) u = edge[pre[u] ^ 1].to;
}
return ans;
}
int m, n, f;
map<string, int> Hash;
string x, y;
int main()
{
while (cin >> n)
{
init();
Hash.clear();
int num1 = 2;
int from = 0;
int end = 1;
for (int i = 1; i <= n; i++)
{
cin >> x;
Hash[x] = num1;
addedge(0, num1, 1);
num1++;
}
cin >> m;
for (int i = 1; i <= m; i++)
{
cin >> x >> y;
if (Hash[x] == 0) Hash[x] = num1++;
if (Hash[y] == 0) Hash[y] = num1++;
addedge(Hash[x], end, 1);
addedge(Hash[y], Hash[x], 1);
}
cin >> f;
for (int i = 1; i <= f; i++)
{
cin >> x >> y;
if (Hash[x] == 0) Hash[x] = num1++;
if (Hash[y] == 0) Hash[y] = num1++;
addedge(Hash[y], Hash[x], 10000000);
}
int ans = sap(from, end, num1);
printf("%d\n", m - ans);
}
return 0;
}
poj 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 ...
- 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 (最大流)
网络流,关键在建图 建图思路在代码里 /* 最大流SAP 邻接表 思路:基本源于FF方法,给每个顶点设定层次标号,和允许弧. 优化: 1.当前弧优化(重要). 1.每找到以条增广路回退到断点(常数优化 ...
- kuangbin专题专题十一 网络流 POJ 1087 A Plug for UNIX
题目链接:https://vjudge.net/problem/POJ-1087 题目:有n个插座,插座上只有一个插孔,有m个用电器,每个用电器都有插头,它们的插头可以一样, 有k个插孔转化器, a ...
- poj 1087 A Plug for UNIX
题目描述:现在由你负责布置Internet联合组织首席执行官就职新闻发布会的会议室.由于会议室修建时被设计成容纳全世界各地的新闻记者,因此会议室提供了多种电源插座用以满足(会议室修建时期)各国不同插头 ...
- 【poj 1087 a plug for UNIX】
在大米饼的帮助下,终于找到了大米饼程序中如同大米饼一般的错误! 考点在问题转化,然后就跑一个你喜欢的最大流算法(二分图可以啵?) 再来一个例子吧: [纯手绘大米饼图片] 其中有的边权是1,否则就是in ...
- hdu 1087 A Plug for UNIX 最大流
题意:http://www.phpfans.net/article/htmls/201012/MzI1MDQw.html 1.在一个会议室里有n种插座,每种插座一个: 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 ...
随机推荐
- Linux学习(十九)软件安装与卸载(二)更换yum源
一.简介 系统自带的源数量有限,而且是国外的源,速度肯定不如国内的.而断网的时候,本地源就可以派得上用处.而RPMForge源是传说中规模最大的一个源.那么接下来我们就来分别配一下本地源,国内源,RP ...
- DataProtection Key的选择
代码位于: Microsoft.AspNetCore.DataProtection.KeyManagement.DefaultKeyResolver.cs private IKey FindDefau ...
- C# 面向对象基础&封装&继承&多态&加深一下冒泡排序写法
(一)面向对象是什么? 面向对象是一种编程思想 (二)为什么要用面向对象? 1.结构清晰 2.易于维护 3.方便扩展 (三)new一个对象是什么过程? 实例化构造函数创建对象的过程就是将类实例化的过程 ...
- freeMarker遍历map的正确方式
假设selectDateModel 是我们后台返回的map<String, String>; <#list selectDateModel?keys as key> <o ...
- Java微信公众平台开发_06_素材管理
一.本节要点 1.官方文档的media 这个media可以理解为文件,即我们需要以POST方式提交一个文件 2.媒体文件有效期 媒体文件在微信后台保存时间为3天,即3天后media_id失效. 二.代 ...
- app.config 配置多项 配置集合 自定义配置
C#程序的配置文件,使用的最多的是appSettings 下的<add key="Interval" value="30"/>,这种配置单项的很方便 ...
- Intrumentation类:ActivityInstrumentationTestCase2学习(1)
public abstract class ActivityInstrumentationTestCase2 extends ActivityTestCase//继承自ActivityTestCase ...
- C# 跨平台的支付类库ICanPay
随着微软的开源,越来越多的项目支持跨平台,但是各种支付平台提供的类库,又老又不支持跨平台,吐槽下,尤其是微信,还有好多坑,于是ICanPay诞生了,今天就来讲ICanPay是什么,怎么使用? ICan ...
- Not found org.springframework.http.converter.json.MappingJacksonHttpMessageConverter
原因spring3跟spring4的jackson不一样. 解决方案: 1)spring3.x是org.springframework.http.converter.json.MappingJacks ...
- spring boot 错误,求大神帮解决
Exception in thread "main" java.lang.IllegalStateException: Failed to read Class-Path attr ...