最近开始刷网络流的题目了,先从紫书上的开始,这道题是P374上的,嘛,总之这道题最终还是参考了一下紫书。

中间是用了STL中map将字符串映射成编号,使用编号总比是用字符串简单的多。

超级源点S与各个设备对应插头类型连一条边,容量为1,

超级汇点T与各个插头连一条边,容量为1

然后如果有转换器,如果x->y,那么从点x连一条容量为正无穷的边到y (因为插头同类型的有无数个)

这样跑一发最大流即可,代码中间套用模板

 #include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<vector>
#include<queue>
#include<string>
#include<sstream>
#define MAXN 405
#define MAXM 805
#define INF ((1<<30)/2)
#define eps 0.000001
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
using namespace std;
int i,j,k,n,m,x,y,T,num,w; const int inf = 0x3f3f3f3f;
struct edgenode
{
int from,to,next;
int cap;
}edge[MAXM];
int Edge,head[MAXN],ps[MAXN],dep[MAXN]; void add_edge(int x,int y,int c)
{
edge[Edge].from=x;
edge[Edge].to=y;
edge[Edge].cap=c;
edge[Edge].next=head[x];
head[x]=Edge++; edge[Edge].from=y;
edge[Edge].to=x;
edge[Edge].cap=;
edge[Edge].next=head[y];
head[y]=Edge++;
} int dinic(int n,int s,int t)
{
int tr,res=;
int i,j,k,l,r,top;
while(){
memset(dep,-,(n+)*sizeof(int));
for(l=dep[ps[]=s]=,r=;l!=r;)//BFS部分,将给定图分层
{
for(i=ps[l++],j=head[i];j!=-;j=edge[j].next)
{
if (edge[j].cap&&-==dep[k=edge[j].to])
{
dep[k]=dep[i]+;ps[r++]=k;
if(k==t)
{
l=r;
break;
}
}
}
}
if(dep[t]==-)break; for(i=s,top=;;)//DFS部分
{
if(i==t)//当前点就是汇点时
{
for(k=,tr=inf;k<top;++k)
if(edge[ps[k]].cap<tr)tr=edge[ps[l=k]].cap; for(k=;k<top;++k)
edge[ps[k]].cap-=tr,edge[ps[k]^].cap+=tr; res+=tr;
i=edge[ps[top=l]].from;
} for(j=head[i];j!=-;j=edge[j].next)//找当前点所指向的点
if(edge[j].cap&&dep[i]+==dep[edge[j].to]) break; if(j!=-)
{
ps[top++]=j;//当前点有所指向的点,把这个点加入栈中
i=edge[j].to;
}
else
{
if (!top) break;//当前点没有指向的点,回溯
dep[i]=-;
i=edge[ps[--top]].from;
}
}
}
return res;
} int main()
{
int T,cas,m,t,n,maxflow,i;
int x,y,c;
double ans;
scanf("%d",&T);
map <string,int> rec;
string s,s2;
while(T--)
{
memset(head,-,sizeof(head));
Edge=;
scanf("%d",&n);
rec.clear();
num=;//设定,0为源点,1为汇点
for (i=;i<=n;i++)
{
cin>>s;
if (!rec.count(s)) rec[s]=++num;
add_edge(rec[s],,);
}
scanf("%d",&m);
for (i=;i<=m;i++)
{
cin>>s>>s2;
if (!rec.count(s2)) rec[s2]=++num;
add_edge(,rec[s2],);
}
scanf("%d",&k);
for (i=;i<=k;i++)
{
cin>>s>>s2;
if (!rec.count(s))
{
rec[s]=++num;
}
if (!rec.count(s2))
{
rec[s2]=++num;
}
add_edge(rec[s],rec[s2],INF);
}
maxflow=dinic(num,,);
printf("%d\n",m-maxflow);
if (T!=) printf("\n");
}
return ;
}

PS:这道题也是 poj 1087,但是poj中每组测试数据中一组数据,所以要把开始的scanf("%d",&T);去掉

【网络流#4】UVA 753 最大流的更多相关文章

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

  2. Libre 6013 「网络流 24 题」负载平衡 (网络流,最小费用最大流)

    Libre 6013 「网络流 24 题」负载平衡 (网络流,最小费用最大流) Description G 公司有n 个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等.如何用最少搬运量可以使n ...

  3. Libre 6011 「网络流 24 题」运输问题 (网络流,最小费用最大流)

    Libre 6011 「网络流 24 题」运输问题 (网络流,最小费用最大流) Description W 公司有m个仓库和n个零售商店.第i个仓库有\(a_i\)个单位的货物:第j个零售商店需要\( ...

  4. Libre 6010「网络流 24 题」数字梯形 (网络流,最大费用最大流)

    Libre 6010「网络流 24 题」数字梯形 (网络流,最大费用最大流) Description 给定一个由n 行数字组成的数字梯形如下图所示.梯形的第一行有m 个数字.从梯形的顶部的m 个数字开 ...

  5. Libre 6008 「网络流 24 题」餐巾计划 (网络流,最小费用最大流)

    Libre 6008 「网络流 24 题」餐巾计划 (网络流,最小费用最大流) Description 一个餐厅在相继的N天里,第i天需要Ri块餐巾(i=l,2,-,N).餐厅可以从三种途径获得餐巾. ...

  6. POJ 2135 Farm Tour (网络流,最小费用最大流)

    POJ 2135 Farm Tour (网络流,最小费用最大流) Description When FJ's friends visit him on the farm, he likes to sh ...

  7. 紫书 例题11-7 UVa 753 (网络流最大流)

    设一个源点, 到所有设备连一条弧, 容量为1, 然后设一个汇点, 所有插座到汇点连弧, 容量为1, 然后 转换器也连一条弧, 容量为1. 最后最大流就是答案.其中注意节点数要开大一些. #includ ...

  8. 【uva 753】A Plug for UNIX(图论--网络流最大流 Dinic)

    题意:有N个插头,M个设备和K种转换器.要求插的设备尽量多,问最少剩几个不匹配的设备. 解法:给读入的各种插头编个号,源点到设备.设备通过转换器到插头.插头到汇点各自建一条容量为1的边.跑一次最大流就 ...

  9. UVA 753 A Plug for UNIX 电器插座(最大基数匹配,网络流)

    题意: 给n个插座,m个设备(肯定要插电了),k种转换头可无限次使用(注意是单向的),问有多少设备最终是不能够插上插座的? 分析: 看起来就是设备匹配插座,所以答案不超过m.这个题适合用网络流来解. ...

随机推荐

  1. linux之umask函数解析

    [lingyun@localhost umask_1]$ vim umask.c  + umask.c                                                 ...

  2. Unique Binary Search Tree

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  3. php中const定义常量

    const 常量 1.在定义时必须被初始值,2.前面不加任何修饰符3.变量名字母一般都大写4.常量可以被子类继承5.一个常量是属于一个类的,而不是某个对象的 作用:当某些值是固定不变的,就用const ...

  4. php设计模式之简单工厂模式

    ①抽象基类:类中定义抽象一些方法,用以在子类中实现 ②继承自抽象基类的子类:实现基类中的抽象方法 ③工厂类:用以实例化所有相对应的子类 /** * * 定义个抽象的类,让子类去继承实现它 * */ a ...

  5. matplotlib curve.py

    import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 2*np.pi, 100) sinX = np.sin(x) ...

  6. C题

    C - C Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Description   Ass ...

  7. INDEX RANG SCAN无需回表的情况

    create table a3 as select * from dba_objects create index a3_idx1 on a3(owner); select owner from a3 ...

  8. CentOS安装搜狗词库

    中文输入使用ibus-pinyin. 在ibus-pinyin里使用搜狗词库 # wget http://hslinuxextra.googlecode.com/files/sougou-phrase ...

  9. C语言中for循环的使用

    for循环的作用: 注意:要主要满足条件一和二后是先执行语句,后再执行条件三. 简单重复的输出 for(int i=0:i<10;i++){ printf("对一句话简单重复输出输出1 ...

  10. 用python爬虫抓站的一些技巧总结

    1. [代码]最基本的抓站     ? 1 2 import urllib2 content = urllib2.urlopen('http://XXXX').read() 2. [代码]使用代理服务 ...