本文主要是介绍POJ 1087 A Plug For Unix (最大流/匹配),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
2020.7.10
终于把这道题A了,我早上起来来到图书馆就看见这一道题,去年想过但没敢写,今天学了网络流就来搞一搞。
一看这题,傻X题,切了它。拍了个板子,然后开始建模,这题说每个设备都有自己适配的插头,然后又有一些(无限个)适配特定两个插头的转换插头,又有能插若干个型号插头的插座,每种有一个,问最多能有多少个设备插进去。思路很白板啊,就是源点连设备,设备连插头,插头连插座就完事儿了。一发过样例,一发就wa了,然后自闭了两个小时,感觉程序怎么调样例都是对的,然而怎么调都过不去。后来跟群内大佬讨论了下思路,才发现自己忘掉了转换插头有无数个,而且这时候我想起来,转换插头属于插头的内部转换,需要同一层连边,不能跨层,因为有无数个所以边的大小是INF。那么就很好想了,连边,用dinic跑一边,看看能匹配多少,用设备总数减掉之后就是最少不能配对的数量,完美。题不难,但有几个关键细节和这个输入的模式很坑人,害得我调了一早上才过。开FAST IO了就只能用cin了。另外有人问我为什么我的源点和汇点那么随机?每次都是一个常数。其实这个没啥讲究,你觉得这边在添加边的时候碰不到,就可以拿来用作超级源点和汇点。
代码:
#include <bits/stdc++.h>
using namespace std;
#define limit (100000 + 5)//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-6
#define FASTIO ios::sync_with_stdio(false);cin.tie(0);
#define ff(a) printf("%d\n",a );
#define pi(a,b) pair<a,b>
#define rep(i, a, b) for(ll i = a; i <= b ; ++i)
#define per(i, a, b) for(ll i = b ; i >= a ; --i)
#define MOD 998244353
#define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next)
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\acm_01\\data.txt", "rt", stdin)
#define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\acm_01\\dabiao.txt", "wt", stdout)
typedef long long ll;
typedef unsigned long long ull;
ll read(){ll sign = 1, x = 0;char s = getchar();while(s > '9' || s < '0' ){if(s == '-')sign = -1;s = getchar();}while(s >= '0' && s <= '9'){x = (x << 3) + (x << 1) + s - '0';s = getchar();}return x * sign;
}//快读
void write(ll x){if(x < 0) putchar('-'),x = -x;if(x / 10) write(x / 10);putchar(x % 10 + '0');
}
int n,m,vs,ve,k;
int layer[limit],head[limit], cnt;
struct node{int to ,next;ll flow;
}edge[limit];
ll max_flow;
void add_one(int u , int v, ll flow){edge[cnt].to = v;edge[cnt].next = head[u];edge[cnt].flow = flow;head[u] = cnt++;
}
inline void add(int u, int v, ll flow){add_one(u,v,flow);add_one(v, u,0);
}
inline void init(bool flag = true){if(flag){memset(head, -1, sizeof(head));max_flow = cnt = 0;}else{memset(layer, -1, sizeof(layer));}
}
inline bool bfs(){init(false);queue<int>q;layer[vs] = 0;//从第0层开始q.push(vs);while (q.size()){int u = q.front();q.pop();traverse(u){int v = edge[i].to,flow = edge[i].flow;if(layer[v] == -1 && flow > 0){layer[v] = layer[u] + 1;//迭代加深q.push(v);}}}return ~layer[ve];
}
ll dfs(int u, ll flow){if(u == ve)return flow;ll rev_flow = 0,min_flow;traverse(u){int v =edge[i].to;ll t_flow = edge[i].flow;if(layer[v] == layer[u] + 1 && t_flow > 0){min_flow = dfs(v, min(flow, t_flow));flow -= min_flow;edge[i].flow -= min_flow;rev_flow += min_flow;edge[i^1].flow += min_flow;if(!flow)break;}}if(!rev_flow)layer[u] = -1;return rev_flow;
}
void dinic(){while (bfs()){max_flow += dfs(vs,inf);}
}
map<string,int>device,plug;//插头和插座
int main() {
#ifdef LOCALFOPEN;
#endifFASTIOwhile (cin>>n){device.clear();//初始化plug.clear();init();//初始化int tot = 0, num = 0;//分别记录插头的数目和设备的数目vs = 80001, ve = vs + 1;rep(i ,1,n){string str;cin>>str;plug[str] = ++tot;//编号}cin>>m;rep(i ,1,m){string item, match;cin>>item>>match;device[item] = ++num;//编号if(plug.find(match) == plug.end())plug[match] = ++tot;add(device[item], m + plug[match],1);add(vs, device[item], 1);//添加便}cin>>k;rep(i ,1, n){add(m + i , m + tot + i, 1);add(m + tot + i , ve, 1);}rep(i ,1, k){string fst, scd;cin>>fst>>scd;add(plug[fst] + m , plug[scd] + m ,INF);}dinic();cout<<(m - max_flow)<<endl;}return 0;
}
这篇关于POJ 1087 A Plug For Unix (最大流/匹配)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!