POJ 1087 A Plug for UNIX
题意:有n(1≤n≤100)个插座,每个插座用一个数字字母式字符串描述(至多有24 个字符)。有m(1≤m≤100)个设备,每个设备有名称,以及它使用的插头的名称;插头的名称跟它所使用的插座的名称是一样的;设备名称是一个至多包含24 个字母数字式字符的字符串;任何两个设备的名称都不同;有k(1≤k≤100)个转换器,每个转换器能将插座转换成插头。
样例:
4 A B C D 5 laptop B phone C pager B clock B comb X 3 B X X A X D
思路:建立一个汇点,每个插座和汇点连边,流量代表该种插座的个数。建立一个源点,源点和每个设备连边,流量为1。每个转换器将两个插座连边,流量为INF。然后求最大流。答案就是设备的个数减去最大流。
细节:这道题在建图的时候比较恶心。需要注意的是,在m个设备和插座以及k个转换器的数据中,可能有之前没有出现过的设备。所以必须将插座的信息存起来,可以用map来存插座对应的点。
代码:
/*
ID: wuqi9395@126.com
PROG:
LANG: C++
*/
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<fstream>
#include<cstring>
#include<ctype.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define INF (1<<30)
#define PI acos(-1.0)
#define mem(a, b) memset(a, b, sizeof(a))
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define eps 1e-6
#define debug puts("===============")
#define pb push_back
//#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define POSIN(x,y) (0 <= (x) && (x) < n && 0 <= (y) && (y) < m)
typedef long long ll;
typedef unsigned long long ULL;
const int maxn = 610;
const int maxm = 2000000;
int m, k;
struct node {
int v; // vertex
int cap; // capacity
int flow; // current flow in this arc
int nxt;
} e[maxm * 2];
int g[maxn], cnt;
int st, ed, n;
void add(int u, int v, int c) {
e[++cnt].v = v;
e[cnt].cap = c;
e[cnt].flow = 0;
e[cnt].nxt = g[u];
g[u] = cnt;
e[++cnt].v = u;
e[cnt].cap = 0;
e[cnt].flow = 0;
e[cnt].nxt = g[v];
g[v] = cnt;
}
map<string, int> mp;
map<string, int> :: iterator it;
void init() {
mem(g, 0);
cnt = 1;
mp.clear();
scanf("%d", &n);
char str[25];
ed = 0;
int tot = 1, has[111] = {0};
for (int i = 1; i <= n; i++) {
scanf("%s", str);
string ss(str);
int now;
if (mp[ss] == 0) {
mp[ss] = tot;
now = tot++;
} else now = mp[ss];
has[now]++;
}
for (int i = 1; i < tot; i++) add(i, ed, has[i]);
scanf("%d", &m);
char s[25];
vector<int> v;
for (int i = 0; i < m; i++) {
scanf("%s%s", str, s);
v.pb(tot);
if (mp[s]) add(tot++, mp[s], 1);
else {
mp[s] = tot + 1;
add(tot, tot + 1, 1);
tot += 2;
}
}
scanf("%d", &k);
for (int i = 0; i < k; i++) {
scanf("%s%s", str, s);
if (mp[str] == 0) mp[str] = tot++;
if (mp[s] == 0) mp[s] = tot++;
add(mp[str], mp[s], INF);
}
st = tot;
for (int i = 0; i < v.size(); i++) add(st, v[i], 1);
n = tot + 3;
}
int dist[maxn], numbs[maxn], q[maxn];
void rev_bfs() {
int font = 0, rear = 1;
for (int i = 0; i <= n; i++) { //n为总点数
dist[i] = maxn;
numbs[i] = 0;
}
q[font] = ed;
dist[ed] = 0;
numbs[0] = 1;
while(font != rear) {
int u = q[font++];
for (int i = g[u]; i; i = e[i].nxt) {
if (e[i ^ 1].cap == 0 || dist[e[i].v] < maxn) continue;
dist[e[i].v] = dist[u] + 1;
++numbs[dist[e[i].v]];
q[rear++] = e[i].v;
}
}
}
int maxflow() {
rev_bfs();
int u, totalflow = 0;
int curg[maxn], revpath[maxn];
for(int i = 0; i <= n; ++i) curg[i] = g[i];
u = st;
while(dist[st] < n) {
if(u == ed) { // find an augmenting path
int augflow = INF;
for(int i = st; i != ed; i = e[curg[i]].v)
augflow = min(augflow, e[curg[i]].cap);
for(int i = st; i != ed; i = e[curg[i]].v) {
e[curg[i]].cap -= augflow;
e[curg[i] ^ 1].cap += augflow;
e[curg[i]].flow += augflow;
e[curg[i] ^ 1].flow -= augflow;
}
totalflow += augflow;
u = st;
}
int i;
for(i = curg[u]; i; i = e[i].nxt)
if(e[i].cap > 0 && dist[u] == dist[e[i].v] + 1) break;
if(i) { // find an admissible arc, then Advance
curg[u] = i;
revpath[e[i].v] = i ^ 1;
u = e[i].v;
} else { // no admissible arc, then relabel this vertex
if(0 == (--numbs[dist[u]])) break; // GAP cut, Important!
curg[u] = g[u];
int mindist = n;
for(int j = g[u]; j; j = e[j].nxt)
if(e[j].cap > 0) mindist = min(mindist, dist[e[j].v]);
dist[u] = mindist + 1;
++numbs[dist[u]];
if(u != st)
u = e[revpath[u]].v; // Backtrack
}
}
return totalflow;
}
int main () {
init();
printf("%d\n", m - maxflow());
return 0;
}