没啥好说的,慢慢建图

Sample Input

4
A
B
C
D
5
laptop B
phone C
pager B
clock B
comb X
3
B X
X A
X D

Sample Output

1

题意:有n个不同的插座,有m台不同的机器需要m种插头,有k组转换:插头A能由插头B转换而来。问这些机器最少有几台不能插上插座。

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
using namespace std;
const int MAXN=;
const int INF=0x3fffffff;
int g[MAXN][MAXN];//存边的容量,没有边的初始化为0
int path[MAXN],flow[MAXN],start,end;
int n;//点的个数,编号0-n.n包括了源点和汇点。
queue<int>q;
int bfs()
{
int i,t;
while(!q.empty())q.pop();//把清空队列
memset(path,-,sizeof(path));//每次搜索前都把路径初始化成-1
path[start]=;
flow[start]=INF;//源点可以有无穷的流流进
q.push(start);
while(!q.empty())
{
t=q.front();
q.pop();
if(t==end)break;
//枚举所有的点,如果点的编号起始点有变化可以改这里
for(i=;i<=n;i++)
{
if(i!=start&&path[i]==-&&g[t][i])
{
flow[i]=flow[t]<g[t][i]?flow[t]:g[t][i];
q.push(i);
path[i]=t;
}
}
}
if(path[end]==-)return -;//即找不到汇点上去了。找不到增广路径了
return flow[end];
}
int Edmonds_Karp()
{
int max_flow=;
int step,now,pre;
while((step=bfs())!=-)
{
max_flow+=step;
now=end;
while(now!=start)
{
pre=path[now];
g[pre][now]-=step;
g[now][pre]+=step;
now=pre;
}
}
return max_flow;
} map<string,int> hash;
int main()
{
int i,j,k,m;
//freopen("1.in","r",stdin);
while(scanf("%d",&n)!=EOF)
{
hash.clear();
memset(g,,sizeof(g));
string s1,s2,s3;
start=;
end=;
int tot=;
while(n--)
{
cin>>s1;
hash[s1]=tot;
g[][hash[s1]]=;
tot++;
}
scanf("%d",&m);
for(i=;i<m;i++)
{
cin>>s1>>s2;
if(hash[s1]==) hash[s1]=tot++;
if(hash[s2]==) hash[s2]=tot++;
g[hash[s1]][end]=;
g[hash[s2]][hash[s1]]=;
}
scanf("%d",&k);
while(k--)
{
cin>>s1>>s2;
if(hash[s1]==) hash[s1]=tot++;
if(hash[s2]==) hash[s2]=tot++;
g[hash[s2]][hash[s1]]=INF;
}
n=tot-; //总点数
printf("%d\n",m-Edmonds_Karp());
}
return ;
}

poj 1087 最大流的更多相关文章

  1. POJ 1087 最大流裸题 + map

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

  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 3281 最大流+建图

    很巧妙的思想 转自:http://www.cnblogs.com/kuangbin/archive/2012/08/21/2649850.html 本题能够想到用最大流做,那真的是太绝了.建模的方法很 ...

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

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

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

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

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

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

  8. C - A Plug for UNIX - poj 1087(最大流)

    题目大意:这个题意有些蛋疼,看了很大会才明白什么意思,有N个插座,这些插座都是有类型的只能给这种类型的电器充电,下面接着给了M种电器,和电器的插头类型,还有K种转换器,可以把一种类型转换成另一种,转换 ...

  9. (网络流 模板)A Plug for UNIX -- poj -- 1087

    链接: http://poj.org/problem?id=1087 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82835#probl ...

随机推荐

  1. nginx负载均衡的配置方法

    upstream www.***.com { server ; server ; server ; } server { listen ; server_name www.***.com; #char ...

  2. Java统计数据库表中记录数

    public static int count(String txyl_table) {// 获取用户数量 int i = 0;// Store_Information Connection con ...

  3. IE7下,input元素相对父级元素错位解决办法

    原因: 当input仅仅包含父元素,父元素拥有margin属性时,IE7的input就会错误的继承margin属性. 解决办法: 给input元素外面套一个span,label这样的内联元素,这样就会 ...

  4. 陷阱~EF中的Update与Insert共用一个数据上下文

    事情是这样的,有一个列表,里面有很多用户信息,可能会有重复的用户,将这个列表的用户插入到数据表中,如果用户已经存在,就更新这个用户的FillTimes 字段,让它加1,使用的底层ORM是entity ...

  5. 用C语言画一个“爱心”

    /*每个main函数代表一种形状*/#include<iostream> using namespace std; #include<stdio.h> #include< ...

  6. 一张图解释Hadoop IPC

    基于hadoop2.6.2.... 一张图Server启动,Client访问..... RPC是IPC的一种,IPC还有另外一种LPC,相关请看参考中的3 使用hadoop ipc步骤: 1.定义RP ...

  7. 抓包工具 wireshark

    http://fangxin.blog.51cto.com/1125131/735178

  8. codeforces 258div2C Predict Outcome of the Game

    题目链接:http://codeforces.com/contest/451/problem/C 解题报告:三个球队之间一共有n场比赛,现在已经进行了k场,不知道每个球队的胜场是多少,如三个球队的胜场 ...

  9. Linux Tomcat 简介

    如今,基于Web的应用越来越多,传统的Html已经满足不了如今的需求.我们需要一个交互式的Web,于是便诞生了各种Web语言.如Asp,Jsp,Php等.当然,这些语言与传统的语言有着密切的联系,如P ...

  10. Floyd算法 及其运用

    #include<stdio.h> ][]; ][]; void floyd(int n) { ;k<=n;k++) { ;i<=n;i++) { ;j<=n;j++) ...