POJ-1087 A Plug for UNIX

来源:互联网 发布:java获取文件夹的路径 编辑:程序博客网 时间:2024/06/01 22:29

题目链接:A Plug for UNIX
题目大意:有n个插座,m个电器,每个电器有自己的名字和插头类型,还有k种转换器,每种转换器可以有无限个,转换器可以接转换器,问最少有多少电器不能插上插座。
解题思路:源点与电器,电器与插头建边,转换器输入接口与输出插头建边,插座与汇点建边,最后跑一遍dinic就行了。

代码如下:

#include <map>#include <set>#include <cmath>#include <queue>#include <stack>#include <cstdio>#include <cstdlib>#include <cstring>#include <iomanip>#include <iostream>#include <algorithm>using namespace std;typedef long long ll;typedef pair<int, int> P;const int inf  = 0x3f3f3f3f;const int maxn = 2e4 + 15;struct Edge {    int to, cap, next;};map<string, int> mp;int ecnt, st, ed;Edge es[maxn * 10];int cur[maxn], dep[maxn], head[maxn]; class Dinic {public:    void init(){        memset(head, -1, sizeof head);        ecnt = 0;    }    void add_edge(int u, int v, int cap) {        es[ecnt].to = v;        es[ecnt].next = head[u];        es[ecnt].cap = cap;        head[u] = ecnt++;    }    void add_double(int u, int v, int w1, int w2 = 0) {        add_edge(u, v, w1);        add_edge(v, u, w2);    }    bool BFS() {        queue<int> q; q.push(st);        memset(dep, 0x3f, sizeof dep); dep[st] = 0;        while(q.size() && dep[ed] == inf) {            int u = q.front(); q.pop();            for(int i = head[u]; ~i; i = es[i].next) {                Edge& e = es[i];                if(e.cap > 0 && dep[e.to] == inf) {                    dep[e.to] = dep[u] + 1;                    q.push(e.to);                }            }        }        return dep[ed] < inf;    }    int DFS(int u, int maxflow) {        if(u == ed) return maxflow;        int res = 0;        for(int i = cur[u]; ~i; i = es[i].next) {            Edge& e = es[i];            if(dep[e.to] == dep[u] + 1 && e.cap > 0) {                int flow = DFS(e.to, min(maxflow, e.cap));                  cur[u] = i;                 res += flow; maxflow -= flow;                es[i].cap -= flow;                 es[i ^ 1].cap += flow;                if(!maxflow) return res;            }        }               dep[u] = inf;        return res;    }    int MaxFlow(){        int ans = 0;            while(BFS()) {               for(int i = 0; i < ecnt; i++) cur[i] = head[i];            ans += DFS(st, inf);        }        return ans;    }};int main(){#ifdef NEKO    freopen("Nya.txt", "r", stdin);#endif    ios::sync_with_stdio(false);    Dinic dic;    dic.init(); st = 0, ed = 1;    int n, m, k, index = 2;     cin >> n;    string s, name;     for(int i = 1; i <= n; i++) {        cin >> s;        if(!mp.count(s)) mp[s] = index++;        int v = mp[s];        dic.add_double(v, ed, 1);    }    cin >> m;    for(int i = 1; i <= m; i++) {        cin >> name >> s;           if(!mp.count(name)) mp[name] = index++;        if(!mp.count(s)) mp[s] = index++;        int v1 = mp[name], v2 = mp[s];        dic.add_double(st, v1, 1);        dic.add_double(v1, v2, 1);    }    cin >> k; string l, r;    for(int i = 1; i <= k; i++) {        cin >> l >> r;        if(!mp.count(l)) mp[l] = index++;        if(!mp.count(r)) mp[r] = index++;        int v1 = mp[l], v2 = mp[r];             dic.add_double(v1, v2, inf);    }    cout << m - dic.MaxFlow() << endl;    return 0;}
0 0