ACM/ICPC 之 Unix会议室(POJ1087)
采用EK算法解网络流经典题,本题构图思路比较明确。
//Unix会议室插座转换
//网络流-EK算法
//Time:47Ms Memory:1188K
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std; #define MAX 505
#define MAXS 25
#define INF 0x3f3f3f3f int n,m,k;
int s,t;
int res[MAX][MAX]; //残留网络
int pre[MAX];
int len; //插头种类数
int no[MAX]; //插头下标编号
char trans[MAX][MAXS]; //插头名称 int getNum(char *str) //插头名称->编号
{
for(int i = 0; i < len; i++)
{
if(!strcmp(trans[i], str))
return no[i];
}
return -1;
} bool bfs()
{
memset(pre,-1,sizeof(pre));
queue<int> q;
q.push(s); pre[s] = 0;
while(!q.empty()){
int cur = q.front();
q.pop();
for(int i = 1; i <= t; i++)
{
if(pre[i] == -1 && res[cur][i])
{
pre[i] = cur;
if(i == t) return true;
q.push(i);
}
}
}
return false;
} int EK()
{
int maxFlow = 0;
while(bfs())
{
int mind = INF;
for(int i = t; i != s; i = pre[i])
mind = min(mind, res[pre[i]][i]);
for(int i = t; i != s; i = pre[i])
{
res[pre[i]][i] -= mind;
res[i][pre[i]] += mind;
}
maxFlow += mind;
}
return maxFlow;
} int main()
{
//freopen("in.txt", "r", stdin); memset(res,0,sizeof(res));
scanf("%d", &n);
s = 0; t = n+1; //汇点在不断更新
len = 0;
for(int i = 1; i <= n; i++)
{
no[len] = i;
scanf("%s", trans[len++]);
}
scanf("%d", &m);
t += m;
int num[2];
char str[2][MAXS];
for(int i = n+1; i <= n+m; i++)
{
scanf("%s%s", str[0], str[1]);
int num = getNum(str[1]);
if(num == -1)
{
no[len] = num = t++;
strcpy(trans[len++], str[1]);
}
res[s][i] = res[i][num] = 1;
}
scanf("%d", &k);
for(int i = 0; i < k; i++)
{
scanf("%s%s", str[0], str[1]);
for(int j = 0; j < 2; j++){
num[j] = getNum(str[j]);
if(num[j] == -1)
{
no[len] = num[j] = t++;
strcpy(trans[len++], str[j]);
}
}
res[num[0]][num[1]] = INF;
}
//汇点已固定-更新汇点的邻接边
for(int i = 1; i <= n; i++)
res[i][t] = 1;
printf("%d\n", m - EK()); return 0;
}
ACM/ICPC 之 Unix会议室(POJ1087)的更多相关文章
- 2014嘉杰信息杯ACM/ICPC湖南程序设计邀请赛暨第六届湘潭市程序设计竞赛
比赛链接: http://202.197.224.59/OnlineJudge2/index.php/Contest/problems/contest_id/36 题目来源: 2014嘉杰信息杯ACM ...
- ACM/ICPC 之 BFS(离线)+康拓展开(TSH OJ-玩具(Toy))
祝大家新年快乐,相信在新的一年里一定有我们自己的梦! 这是一个简化的魔板问题,只需输出步骤即可. 玩具(Toy) 描述 ZC神最擅长逻辑推理,一日,他给大家讲述起自己儿时的数字玩具. 该玩具酷似魔方, ...
- ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 G. Garden Gathering
Problem G. Garden Gathering Input file: standard input Output file: standard output Time limit: 3 se ...
- ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 D. Delay Time
Problem D. Delay Time Input file: standard input Output file: standard output Time limit: 1 second M ...
- hduoj 4710 Balls Rearrangement 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4710 Balls Rearrangement Time Limit: 6000/3000 MS (Java/Ot ...
- 【转】lonekight@xmu·ACM/ICPC 回忆录
转自:http://hi.baidu.com/ordeder/item/2a342a7fe7cb9e336dc37c89 2009年09月06日 星期日 21:55 初识ACM最早听说ACM/ICPC ...
- 【转】ACM/ICPC生涯总结暨退役宣言—alpc55
转自:http://hi.baidu.com/accplaystation/item/ca4c2ec565fa0b7fced4f811 ACM/ICPC生涯总结暨退役宣言—alpc55 前言 早就该写 ...
- hduoj 4708 Rotation Lock Puzzle 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4708 Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/O ...
- hduoj 4715 Difference Between Primes 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4715 Difference Between Primes Time Limit: 2000/1000 MS (J ...
随机推荐
- JS组件系列——使用HTML标签的data属性初始化JS组件
前言:最近使用bootstrap组件的时候发现一个易用性问题,很多简单的组件初始化都需要在JS里面写很多的初始化代码,比如一个简单的select标签,因为仅仅只是需要从后台获取数据填充到option里 ...
- .NET Core 1.0 RC2 历险之旅
文章背景:对于.NET Core大家应该并不陌生, 从它被 宣布 到现在已经有1-2年的时间了,其比较重要的一个版本1.0 RC2 也即将发布..Net Core从一个一个的测试版到现在的RC2,经历 ...
- 回答阿里社招面试如何准备,顺便谈谈对于Java程序猿学习当中各个阶段的建议
引言 其实本来真的没打算写这篇文章,主要是LZ得记忆力不是很好,不像一些记忆力强的人,面试完以后,几乎能把自己和面试官的对话都给记下来.LZ自己当初面试完以后,除了记住一些聊过的知识点以外,具体的内容 ...
- ConvertHelper 通用类
public class ConvertHelper<T> where T : new() { private static Dictionary<Type, List<IPr ...
- checking for fcc ....no checking for cc .. no
源码编译,提示缺少gcc cc cl.exe 解决方案: yum install -y gcc glibc
- mtr命令详解
原文地址:http://blog.hexu.org/archives/1050.shtml 一般在windows 来判断网络连通性用ping 和tracert,ping的话可以来判断丢包率,trace ...
- Android studio2.2 ndk 错误 :format not a string literal and no format arguments!
在Android Studio2.2 进行NDK编程,在对*char 字符串 进行日志输出时,报错: error: format not a string literal and no format ...
- 用类(function(){})()实现点击显示index索引值的详解
code: <script type="text/javascript"> ; i < ; i++){ var btn = document.createElem ...
- VBScript使用CDO.Message发送邮件
Const Email_From = "from@163.com" Const Password = "password" Const Email_To = & ...
- Python【第三章】:python 面向对象 (new)
面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使用(可以讲多函数中公用的变量封装到对象中) 对象,根据模板创建的实例(即:对象),实 ...