题目连接:http://poj.org/problem?

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;
}

转载于:https://www.cnblogs.com/zsychanpin/p/7090694.html

poj 1087 A Plug for UNIX 【最大流】相关推荐

  1. POJ - 1087 A Plug for UNIX(最大流)

    题目链接:点击查看 题目大意:给出n个互不相同的设备,m个插座以及k种适配器,每种适配器都有无限个,适配器可以互相搭配,问如何匹配可以让尽可能多的设备用上电 题目分析:裸的最大流,就是加上了个字符串把 ...

  2. POJ 1087 A Plug for UNIX 会议室插座问题 构图+最大流

    题目链接:POJ 1087 A Plug for UNIX A Plug for UNIX Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  3. POJ 1087 -- A Plug for UNIX(最大流,建图)(文末有极限数据)

    题目链接 Description You are in charge of setting up the press room for the inaugural meeting of the Uni ...

  4. poj 1087 A Plug for UNIX

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

  5. 解题报告 之 POJ1087 A Plug for UNIX

    解题报告 之 POJ1087 A Plug for UNIX Description You are in charge of setting up the press room for the in ...

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

  7. POJ1087A Plug for UNIX(会议室的插座)——最大流

    http://poj.org/problem?id=1087 题目描述: 现在由你负责布置Internet 联合组织首席执行官就职新闻发布会的会议室. 由于会议室修建时被设计成容纳全世界各地的新闻记者 ...

  8. UVA 753 A Plug for UNIX (最大流)

    关键在建图,转换器连一条容量无限的边表示可以转化无数次,设备的插头连源点,插座连汇点. dinic手敲已熟练,输出格式又被坑,总结一下,输出空行多case的,一个换行是必要的,最后一个不加空行,有Te ...

  9. POJ 2112 Optimal Milking(二分+最大流)

    POJ 2112 Optimal Milking 题目链接 题意:给定一些机器和奶牛,在给定距离矩阵,(不在对角线上为0的值代表不可达),每一个机器能容纳m个奶牛.问全部奶牛都能挤上奶,那么走的距离最 ...

最新文章

  1. 【转】mysql增量备份恢复实战企业案例
  2. 阿里云linux CentOS6.5(nginx+PHP-fpm)及RDS初级使用指南和简单安全设置
  3. 非大学生学计算机,浅谈非计算机专业大学生的计算机教学
  4. 网速dns怎么调快_怎么设置dns?教你快速解决网速慢的问题
  5. cesium米转换经纬度_Cesium 坐标系转换
  6. android java 静态库_android的android.mk,和application.mk文件编写
  7. 运维工程师之-MySQL的故障问题总结
  8. tomcat安装启动startup.bat文件命令行界面出现乱码的问题解决
  9. 剑指offer面试题34. 二叉树中和为某一值的路径(先序遍历)(回溯)
  10. ecshop 邮件模板 html,给ecshop后台增加新的邮件模板
  11. 电脑中的打印驱动程序如何打包_旧驱动程序会教您如何处理笔记本电脑上的黑屏...
  12. 描述性物理海洋学--第四章学习笔记
  13. 用计算机计算三角函数角度,角度计算器
  14. html css 怎么画星形,使用CSS如何绘制五角星?(附代码)
  15. python发送图片邮件exchangelib_Python3.5 执行发邮件Exchangelib(=)
  16. win7电脑变身WiFi热点
  17. MySQL 中的boolean/bool/tinyint(1)表示布尔类型
  18. Unity3D Texture2D转换成Sprite格式
  19. RSSI 平面 三点定位算法(C语言、JS源码例程)
  20. 直播平台搭建iOS 版本打包上架

热门文章

  1. k8s pod的三类调度策略:资源限制策略、nodeSelector标签选择器、硬/软亲和性
  2. Hadoop MapReduce实例:按手机上网总流量降序排序代码实现及结果演示
  3. Python Django 惰性查询(懒加载)
  4. 游戏型计算机的配置表,爽玩游戏型详细配置列表及推荐理由
  5. centos7.9更改root账号密码
  6. StringBuilder的原理
  7. 移动开发平台性能比较
  8. android 5.0 ios 8,Android 5.0和iOS8.1哪个好?安卓5.0与iOS8.1区别对比
  9. java字母反过来_java之字母反转~~ - Plight - 51Testing软件测试网 51Testing软件测试网-软件测试人的精神家园...
  10. python获取当前线程名称_python使用标准库根据进程名如何获取进程的pid详解