题意:
有若干个电器设备需要不同的适配器才能接上电源,现在你要让尽可能多的电气设备接上电源。
首先你手中有n个适配器和适配器的型号,再告诉你有m个电器和他们分别对应的适配器的型号,最后还有一个商店提供买不同型号的适配器转换器,转换是单向的A B表示能把A接口转换成B接口(就是原来需要用A适配器的现在可以用B适配器当然也可以用原来的不变)超市提供的转换器数量是没有限制的,可以无限买。
思路:
有若干个电器设备需要不同的适配器才能接上电源,现在你要让尽可能多的电气设备接上电源。
首先你手中有n个适配器和适配器的型号,再告诉你有m个电器和他们分别对应的适配器的型号,最后还有一个商店提供买不同型号的适配器转换器,转换是单向的A B表示能把A接口转换成B接口(就是原来需要用A适配器的现在可以用B适配器当然也可以用原来的不变)超市提供的转换器数量是没有限制的,可以无限买。
思路:
这道题很容易就转化为最大流问题首先一个源点连接不同的电器流量为1,不同的电器根据需要连上不同的适配器流量也为1,再根据不同适配器中能转换建立流量为INF的单向边,再根据每个每个适配器拥有的数量从没个适配器连接一条流量为其数量的边至汇点。这样图就建完了,接下来是EK算法解决问题。
由于我这题是 从插头到设备,所以失配器转换时是 cap[v][u] = INF;,而不是cap[u][v] = INF;
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 500;
int flow[N][N],cap[N][N];
int p[N],a[N],out[N];
map<string,int> hash;
int n,m,k;
int tot; //总插头数
int maxflow(int s,int t) {
queue<int> que;
memset(flow,0,sizeof(flow));
int f = 0;
while(true) {
memset(a,0,sizeof(a));
a[s] = INF;
que.push(s);
while(!que.empty()) {
int u = que.front();
que.pop();
for(int v = s; v <= t; v++) {
if(!a[v] && cap[u][v] > flow[u][v]) {
p[v] = u;
que.push(v);
a[v] = min(a[u], cap[u][v] - flow[u][v]);
}
}
}
if(a[t] == 0) {
break;
}
for(int u = t; u != s; u = p[u]) {
flow[p[u]][u] += a[t];
flow[u][p[u]] -= a[t];
}
f += a[t];
}
return f;
}
void init() {
hash.clear();
memset(cap,0,sizeof(cap));
memset(out,0,sizeof(out));
tot = 1;
}
int find(char str[]) {
int tmp = hash[str]; //读取当前插头的编号
if(tmp == 0) {//如果编号为0,则表示该插头不存在,需要加入新插头
hash[str] = tot++;
return hash[str];
}else { //否则返回该插头的下标
return tmp;
}
}
void read() {
char str1[N],str2[N];
scanf("%d",&n); //输入插头
for(int i = 0; i < n; i++) { //将已经有的插头和源点连通
scanf("%s",str1);
cap[0][find(str1)] += 1;
}
scanf("%d",&m);
for(int i = 0; i < m; i++) { //保存每个插头,如果不存在插头增加插头
scanf("%s%s",str1,str2);
out[i] = find(str2);
}
scanf("%d",&k);
for(int i = 0; i < k; i++) { //将交换器改为无穷大
scanf("%s%s",str1,str2);
int u = find(str1) ,v = find(str2);
cap[v][u] = INF;
}
for(int i = 0; i < m; i++) {
cap[out[i]][tot] += 1;
}
}
int main() {
int T;
char str[N];
scanf("%d",&T);
while(T--) {
init();
read();
int ans = maxflow(0,tot);
printf("%d\n",m - ans);
if(T) {
printf("\n");
}
}
return 0;
}