A Plug for UNIX POJ
来源:互联网 发布:金字塔软件官网 编辑:程序博客网 时间:2024/05/21 18:46
题目链接POJ - 1087
题目大意
不同类型的接口(类型用一个字符串表示), 有n个插座, 每个插座类型都不相同, m个设备, 每个设备有各自的接口类型, k种转换器, 可以将s1接口转换成s2, 转换器数量无限供应, 求最多能有多少能接入插座
思路
建图, 最大流
插座为源点, 设备为汇点, 设置超级源点S, 超级汇点T
S与每个插座连边, 容量为1(只有一个插座)
每个设备与T连边, 容量为1
对于每类转换器s1-s2, 因为使用转换器时, 电源从s2流向s1, 所以s2向s1连边, 容量INF
最后求S-T最大流就好了
代码
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <queue>#include <vector>#include <map>using namespace std;const int MAXV = 1100, INF = 0X3F3F3F3F;struct edge{ int to, cap, rev; edge(int a, int b, int c) :to(a), cap(b), rev(c){}};vector<edge> G[MAXV];int iter[MAXV], level[MAXV], S, T;void add_edge(int from, int to, int cap){// cout << from << " " <<to << " " << cap <<endl; G[from].push_back(edge(to, cap, G[to].size())); G[to].push_back(edge(from, 0, G[from].size()-1));}bool bfs(){ memset(level, -1, sizeof(level)); level[S] = 0; queue<int> que; que.push(S); while(!que.empty()) { int v = que.front(); que.pop(); for(int i=0; i<(int)G[v].size(); ++i) { edge &e = G[v][i]; if(e.cap>0 && level[e.to]==-1) { level[e.to] = level[v] + 1; que.push(e.to); } } } return level[T]!=-1;}int dfs(int v, int f){ if(v == T) return f; for(int &i=iter[v]; i<(int)G[v].size(); ++i) { edge &e = G[v][i]; if(e.cap>0 && level[e.to]>level[v]) { int d = dfs(e.to, min(f, e.cap)); if(d) { e.cap -= d; G[e.to][e.rev].cap+= d; return d; } } } return 0;}int max_flow(){ int flow = 0; while(bfs()) { memset(iter, 0, sizeof(iter)); int f; while((f=dfs(S, INF))) flow += f; } return flow;}int n, out[110], m, in[110], k;string s1, s2;map<string, int> mp;int need[MAXV];int main(){ cin >> n; int cnt = 1; S = 0; for(int i=0; i<n; ++i) { cin >> s1; mp[s1] = cnt++; add_edge(S, mp[s1], 1); } cin >> m; for(int i=0; i<m; ++i) { cin >> s1 >> s2; if(mp.find(s2)==mp.end()) mp[s2] = cnt++; need[mp[s2]]++; } cin >> k; for(int i=0; i<k; ++i) { cin >> s1 >> s2; if(mp.find(s1)==mp.end()) mp[s1] = cnt++; if(mp.find(s2)==mp.end()) mp[s2] = cnt++; add_edge(mp[s2], mp[s1], INF); } T = cnt+1; for(int i=1; i<=cnt; ++i) { if(need[i]!=0) add_edge(i, T, need[i]); } int ans = max_flow();// cout << ans << endl; cout << m - ans <<endl; return 0;}
阅读全文
0 0
- A Plug for UNIX POJ
- A Plug for UNIX POJ
- A Plug for UNIX POJ
- POJ 1087 A Plug for UNIX //MAXMATCH
- POJ 1087 A Plug for Unix
- POJ 1087A Plug for UNIX
- poj 1087 a plug for unix
- POJ-1087-A Plug for UNIX
- poj 1087 A Plug for UNIX
- POJ-1087-A Plug for UNIX
- POJ 1087 A Plug for UNIX
- poj 1087 A Plug for UNIX
- Poj 1087 A Plug For UNIX
- POJ 1087 A Plug for UNIX
- POJ-1087-A Plug for UNIX
- POJ 1087 A Plug for UNIX
- poj 1087 A Plug for UNIX isap
- POJ-1087 A Plug for UNIX
- 版本管理之gitlab实践教程:基础篇(2)
- spring配置数据源几种方式
- SynchronousQueue 实现 二
- Servlet
- 分治法:二分搜索
- A Plug for UNIX POJ
- linux udp program
- HDU
- MemberCache学习
- LeetCode系列之Two sum
- spring boot1.5.X以上版本@ConfigurationProperties取消location注解后的替代方案
- HibernateDaoSupport和HibernateTemplate
- Hard problem CodeForces
- Mysql 中有关日期的函数(sql)