poj1087 最大流
来源:互联网 发布:linux开机自启动mysql 编辑:程序博客网 时间:2024/06/13 20:08
如题:http://poj.org/problem?id=1087
Description
Since the room was designed to accommodate reporters and journalists from around the world, it is equipped with electrical receptacles to suit the different shapes of plugs and voltages used by appliances in all of the countries that existed when the room was built. Unfortunately, the room was built many years ago when reporters used very few electric and electronic devices and is equipped with only one receptacle of each type. These days, like everyone else, reporters require many such devices to do their jobs: laptops, cell phones, tape recorders, pagers, coffee pots, microwave ovens, blow dryers, curling
irons, tooth brushes, etc. Naturally, many of these devices can operate on batteries, but since the meeting is likely to be long and tedious, you want to be able to plug in as many as you can.
Before the meeting begins, you gather up all the devices that the reporters would like to use, and attempt to set them up. You notice that some of the devices use plugs for which there is no receptacle. You wonder if these devices are from countries that didn't exist when the room was built. For some receptacles, there are several devices that use the corresponding plug. For other receptacles, there are no devices that use the corresponding plug.
In order to try to solve the problem you visit a nearby parts supply store. The store sells adapters that allow one type of plug to be used in a different type of outlet. Moreover, adapters are allowed to be plugged into other adapters. The store does not have adapters for all possible combinations of plugs and receptacles, but there is essentially an unlimited supply of the ones they do have.
Input
characters. No two devices will have exactly the same name. The plug type is separated from the device name by a space. The next line contains a single positive integer k (1 <= k <= 100) indicating the number of different varieties of adapters that are available. Each of the next k lines describes a variety of adapter, giving the type of receptacle provided by the adapter, followed by a space, followed by the type of plug.
Output
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
Source
题目大意:有n个插头,m个电器,k种转换器可以无限买,问最终会有多少电器没法使用(没有合适的插头)。
题目思路:从源点到n个插头建编,权值1,m种电器向终点建边,权值为1,将转换器建边,权值是INF。输出m-最大流就是有多少电器无法连接。
给出样例的图。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#define MAXN 50005
#define INF 0x0fffffff
#define Min(a,b)(a<b?a:b)
using namespace std;
int n,m,k;
int t;
char s[5000][30];
int st,ed;
struct edge
{
int to,w,rev;
edge(){}
edge(int a,int b,int c):to(a),w(b),rev(c){}
};
vector<edge>G[MAXN];
int used[MAXN];
int get_id(char *str)
{
int i;
if(t==0)
{
t=1;
strcpy(s[t],str);
return t;
}
for(i=1;i<=t;i++)
{
if(strcmp(s[i],str)==0)
return i;
}
t++;
strcpy(s[t],str);
return t;
}
void add(int u,int v,int w)
{
G[u].push_back(edge(v,w,G[v].size()));
G[v].push_back(edge(u,0,G[u].size()-1));
}
int dfs(int v,int t,int f)
{
if(v==t)
return f;
used[v]=1;
int i;
for(i=0;i<G[v].size();i++)
{
edge &e=G[v][i];
if(!used[e.to]&&e.w>0)
{
int d=dfs(e.to,t,Min(f,e.w));
if(d>0)
{
e.w-=d;
G[e.to][e.rev].w+=d;
return d;
}
}
}
return 0;
}
int max_flow(int s,int t)
{
int flow=0;
while(1)
{
memset(used,0,sizeof(used));
int f=dfs(s,t,INF);
if(f==0)
return flow;
flow+=f;
}
return flow;
}
int main()
{
// freopen("C:\\1.txt","r",stdin);
st=0;
ed=5000;
t=0;
char s1[30],s2[30];
cin>>n;
int i;
for(i=1;i<=n;i++)
{
scanf("%s",s1);
int a=get_id(s1);
add(a,ed,1);
}
cin>>m;
for(i=1;i<=m;i++)
{
scanf("%s%s",s1,s2);
int a=get_id(s1);
int b=get_id(s2);
add(st,a,1);
add(a,b,1);
}
cin>>k;
for(i=1;i<=k;i++)
{
scanf("%s%s",s1,s2);
int a=get_id(s1);
int b=get_id(s2);
add(a,b,INF);
}
// for(i=0;i<=10;i++)
// for(int j=0;j<G[i].size();j++)
// printf("(%d->%d %d)\n",i,G[i][j].to,G[i][j].w);
printf("%d\n",m-max_flow(st,ed));
}
- poj1087 网络最大流
- poj1087 最大流
- 【POJ1087】【最大流建模】
- poj1087(最大流)
- poj1087(最大流)
- poj1087&hdu1526A Plug for UNIX(最大流)
- POJ1087 A Plug for UNIX 【最大流】
- poj1087 A Plug for UNIX (最大流)
- POJ1087 A Plug for UNIX(最大流)
- poj1087 A Plug for UNIX 最大流
- [最大流] POJ1087 ZOJ1157 A Plug for UNIX
- poj1087解题报告(最大流 EK算法)
- poj1087 A Plug for UNIX(最大流)
- ZOJ1157, POJ1087,UVA 753 A Plug for UNIX (最大流)
- POJ1087 A Plug for UNIX(map+dinic最大流)
- POJ1087:A Plug for UNIX(最大流)
- poj1087(网络流 + EK)
- 【网络流】poj1087
- DNS解析与负载均衡
- [算法第一轮复习] 最短路算法之dijkstra
- 回朔法与动态规划的思考
- 黑马程序员_OC面向对象的三大特性
- 生活杂谈(一)
- poj1087 最大流
- 设计模式实例-抽象工厂模式
- Netty系列之Netty高性能之道
- AppCompat v7 No resource found that matches the given 错误
- Java 序列化的高级认识
- AspNet MVC4 教学-6:AspNet MVC4 CheckBox控件测试演示
- MVC上传文件后台获取不到值
- 金额转换为大写
- 事务隔离级别