ZOJ1157, POJ1087,UVA 753 A Plug for UNIX (最大流)
链接 : http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?
id=26746
题目意思有点儿难描写叙述 用一个别人描写叙述好的。
我的建图方法:一个源点一个汇点,和全部种类的插座。输入的n个插座直接与源点相连,容量为1,m个物品输入里 记录每一个插座相应的物品个数。物品数然后大于0的插座直接连到汇点。意味着终于的物品仅仅能由这些插座流出。中间的插座转换容量都是INF a b表示 不管多少b都能够选择转化到a。
/*--------------------- #headfile--------------------*/
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
/*----------------------#define----------------------*/
#define DRII(X,Y) int (X),(Y);scanf("%d%d",&(X),&(Y))
#define EXP 2.7182818284590452353602874713527
#define CASET int _;cin>>_;while(_--)
#define RII(X, Y) scanf("%d%d",&(X),&(Y))
#define DRI(X) int (X);scanf("%d", &X)
#define mem(a,b) memset(a,b,sizeof(a))
#define rep(i,n) for(int i=0;i<n;i++)
#define ALL(X) (X).begin(),(X).end()
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define RI(X) scanf("%d",&(X))
#define SZ(X) ((int)X.size())
#define PDI pair<double,int>
#define rson o<<1|1,m+1,r
#define PII pair<int,int>
#define MAX 0x3f3f3f3f
#define lson o<<1,l,m
#define MP make_pair
#define PB push_back
#define SE second
#define FI first
typedef long long ll;
template<class T>T MUL(T x,T y,T P){T F1=0;while(y){if(y&1){F1+=x;if(F1<0||F1>=P)F1-=P;}x<<=1;if(x<0||x>=P)x-=P;y>>=1;}return F1;}
template<class T>T POW(T x,T y,T P){T F1=1;x%=P;while(y){if(y&1)F1=MUL(F1,x,P);x=MUL(x,x,P);y>>=1;}return F1;}
template<class T>T gcd(T x,T y){if(y==0)return x;T z;while(z=x%y)x=y,y=z;return y;}
#define DRIII(X,Y,Z) int (X),(Y),(Z);scanf("%d%d%d",&(X),&(Y),&(Z))
#define RIII(X,Y,Z) scanf("%d%d%d",&(X),&(Y),&(Z))
const double pi = acos(-1.0);
const double eps = 1e-6;
const ll mod = 1000000007ll;
const int M = 1005;
const int N = 605;
using namespace std; /*----------------------Main-------------------------*/
struct Edge {
int to, c, rev;
Edge() {}
Edge(int _to, int _c, int _rev) {
to = _to, c = _c, rev = _rev;
}
};
vector<Edge> G[N];
int lv[N], iter[N];
int n, m;
void BFS(int s) {
mem(lv, -1);
queue<int> q;
lv[s] = 0;
q.push(s);
while(!q.empty()) {
int v = q.front(); q.pop();
for(int i = 0; i < SZ(G[v]); i++) {
Edge &e = G[v][i];
if(e.c > 0 && lv[e.to] < 0) {
lv[e.to] = lv[v] + 1;
q.push(e.to);
}
}
}
}
int dfs(int v, int t, int f) {
if(v == t) return f;
for(int &i = iter[v]; i < SZ(G[v]); i++) {
Edge &e = G[v][i];
if(e.c > 0 && lv[v] < lv[e.to]) {
int d = dfs(e.to, t, min(f, e.c));
if(d > 0) {
e.c -= d;
G[e.to][e.rev].c += d;
return d;
}
}
}
return 0;
}
int MF(int s, int t) {
int res = 0;
for( ; ; ) {
BFS(s);
if(lv[t] < 0) return res;
mem(iter, 0);
int f;
while((f = dfs(s, t, 1e9)) > 0) {
res += f;
}
}
}
void add(int from, int to, int c) {
G[from].PB( Edge(to, c, SZ(G[to])) );
G[to].PB( Edge(from, 0, SZ(G[from]) - 1) );
}
int num[N];
int FF = 0;
void solve() {
if(FF) puts(""); FF = 1;
RI(n);
for(int i = 0; i < 300; i++) G[i].clear();
mem(num, 0);
int s = 0, k = 0;
map<string, int> vis;
for(int i = 1; i <= n; i++) {
string s1; cin >> s1;
vis[s1] = ++k;
add(s, i, 1);
}
RI(m);
for(int i = 1; i <= m; i++) {
string s1, s2;
cin >> s1 >> s2;
if(vis[s2] == 0) vis[s2] = ++k;
num[ vis[s2] ]++;
}
int t = k + 1;
for(int i = 1; i <= k; i++) {
if(num[i]) add(i, t, num[i]);
}
DRI(x);
k++;
for(int i = 1; i <= x; i++) {
string s1, s2;
cin >> s1 >> s2;
if(vis[s2] == 0) vis[s2] = ++k;
if(vis[s1] == 0) vis[s1] = ++k;
int u = vis[s2], v = vis[s1];
add(u, v, 1e9);
}
int ans = MF(s, t);
printf("%d\n", m - ans);
} int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
CASET
solve();
return 0;
}
ZOJ1157, POJ1087,UVA 753 A Plug for UNIX (最大流)的更多相关文章
- UVa 753 A Plug for UNIX (最大流)
题意:给定 n 种插座,m种设备,和k个转换器,问你最少有几台设备不能匹配. 析:一个很裸的网络流,直接上模板就行,建立一个源点s和汇点t,源点和每个设备连一条边,每个插座和汇点连一条边,然后再连转换 ...
- 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 ...
- UVA 753 A Plug for UNIX(二分图匹配)
A Plug for UNIX You are in charge of setting up the press room for the inaugural meeting of the Unit ...
- UVA 753 - A Plug for UNIX(网络流)
A Plug for UNIX You are in charge of setting up the press room for the inaugural meeting of the U ...
- 【poj1087/uva753】A Plug for UNIX(最大流)
A Plug for UNIX Description You are in charge of setting up the press room for the inaugural meeti ...
- UVA 753 A Plug for UNIX 电器插座(最大基数匹配,网络流)
题意: 给n个插座,m个设备(肯定要插电了),k种转换头可无限次使用(注意是单向的),问有多少设备最终是不能够插上插座的? 分析: 看起来就是设备匹配插座,所以答案不超过m.这个题适合用网络流来解. ...
- UVa 753 - A Plug for UNIX(最大流)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 753 A Plug for UNIX
最大流解决 . 设置源点 0,连接所有设备(device) .设备-插头 -汇点 #include <map> #include <set> #include <list ...
- UVA - 753 A Plug for UNIX(网络流)
题意 给定一些插头设备和插座,有一些方法可以把其中一些插头变成另一种插头.求无法匹配插座的插头设备个数. 题解 用\(map\)给每个字符串标号为\(a_i\)和\(b_i\). 读入每种改变插头的方 ...
随机推荐
- bzoj2441 [中山市选2011]小W的问题(debug中)
2441: [中山市选2011]小W的问题 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 487 Solved: 186[Submit][Statu ...
- Topcoder SRM 601 div1题解
日常TC计划- Easy(250pts): 题目大意:有n个篮子,每个篮子有若干个苹果和橘子,先任取一个正整数x,然后从每个篮子中选出x个水果,把nx个水果放在一起,输出一共有多少种不同的组成方案.其 ...
- [网络流24题] COGS 750 栅格网络流
750. 栅格网络流 ★★☆ 输入文件:flowa.in 输出文件:flowa.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述] Bob 觉得一般图的最大流问题太 ...
- 关于SpringMVC的全局异常处理器
近几天又温习了一下SpringMVC的运行机制以及原理 我理解的springmvc,是设计模式MVC中C层,也就是Controller(控制)层,常用的注解有@Controller.@RequestM ...
- Android 横向列表GridView 实现横向滚动
Android 横向列表实现,可左右滑动,如下图 1.主界面布局代码:activity_main.xml a.包裹HorizontalScrollView控件是GirdView横向滚动的基本条件b.G ...
- 【转载】无需图片,使用CSS3实现圆角按钮
原文地址:http://www.open-open.com/home/space-37924-do-blog-id-5789.html 首先来看看效果: 事例HTML代码: <a href=&q ...
- Android_html5交互 弹框localstorage 存值 整体案例
经历2周多的时间 终于是完成了还算可以的android 整体案例了,分享下给大家 也希望自己有时间回过头来看看当初研究android的纠结心情.痛苦的经历是开发android 大部分都是在网上找解决 ...
- Spfa【P1813】拯救小tim_NOI导刊2011提高(02)
Description 小tim在游乐场,有一天终于逃了出来!但是不小心又被游乐场的工作人员发现了„„所以你的任务是安全地把小tim护送回家.但是,A市复杂的交通状况给你出了一大难题. A市一共有n个 ...
- jcl sort comp3 to 表示型
Lets say your packed data is at 10th column and is of length 6, S9(4)V99 You could try the following ...
- 设计模式之不变模式(Immutable Pattern)分析
http://www.iteye.com/topic/959751 最近老有人问我不变模式,我其实也理解得不深,于是花了一些时间进行学习总结,分析了一下不变模式(immutable pattern), ...