QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#166928#6870. CoinPPP#AC ✓176ms4928kbC++174.7kb2023-09-06 21:05:452023-09-06 21:05:46

Judging History

你现在查看的是最新测评结果

  • [2023-09-06 21:05:46]
  • 评测
  • 测评结果:AC
  • 用时:176ms
  • 内存:4928kb
  • [2023-09-06 21:05:45]
  • 提交

answer

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
//#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int mod = 998244353;
int mult(int a, int b) {
    return (1LL * a * b) % mod;
}
int sub(int a, int b) {
    int s = a - b;
    if (s < 0) s += mod;
    return s;
}
int pw(int a, int b) {
    if (b == 0) return 1;
    if (b & 1) return mult(a, pw(a, b - 1));
    int res = pw(a, b / 2);
    return mult(res, res);
}
int sum(int a, int b) {
    int s = a + b;
    if (s >= mod) s -= mod;
    return s;
}
const int infinity = 1e9;
struct DinitzFlow{
    struct Edge {
        int src, dst, cap, flow;
        Edge() {
        }
        Edge(int src, int dst, int cap, int flow)
                : src(src), dst(dst), cap(cap), flow(flow) {
        }
    };
    int n, s, t;
    vector<Edge> edges;
    vector< vector<int> > g;
    vector<int> layer;
    vector<int> ptr;
    inline bool bfs() {
        layer.assign(n, -1);
        queue<int> q;
        layer[s] = 0;
        q.push(s);
        while (!q.empty() && layer[t] < 0) {
            int v = q.front(); q.pop();
            for (int eid: g[v]) {
                int to = edges[eid].dst;
                if (edges[eid].cap > edges[eid].flow) {
                    if (layer[to] < 0) {
                        layer[to] = layer[v] + 1;
                        q.push(to);
                    }
                }
            }
        }
        return layer[t] >= 0;
    }
    int dfs(int v, int flow = infinity) {
        if (v == t) {
            return flow;
        }
        if (flow == 0) {
            return 0;
        }
        for (; ptr[v] < (int)g[v].size(); ptr[v]++) {
            int eid = g[v][ptr[v]];
            int to = edges[eid].dst;
            if (layer[to] == layer[v] + 1) {
                int can = edges[eid].cap - edges[eid].flow;
                int pushed = dfs(to, min(flow, can));
                if (pushed > 0) {
                    edges[eid].flow += pushed;
                    edges[eid^1].flow -= pushed;
                    return pushed;
                }
            }
        }
        return 0;
    }
    inline void changeSrcDst(int ns, int nt) {
        s = ns; t = nt;
        for (Edge &e : edges) {
            e.flow = 0;
        }
    }
    inline vector<char> getCut() {
        // side[i] = 1 if the vertex is in component of s after the cut
        // side[i] = 0 otherwise (even if the vertex is not in component of t)
        vector<char> side(n, 0);
        function<void(int)> dfs = [&](int v) {
            if (side[v]) {
                return;
            }
            side[v] = 1;
            for (int eid : g[v]) {
                if (edges[eid].cap == edges[eid].flow) {
                    continue;
                }
                dfs(edges[eid].dst);
            }
        };
        dfs(s);
        return side;
    }
    inline int flow() {
        int ans = 0;
        while (bfs()) {
            ptr.assign(n, 0);
            int pushed = dfs(s);
            while (pushed != 0) {
                ans += pushed;
                pushed = dfs(s);
            }
        }
        return ans;
    }
    inline void addEdge(int src, int dst, int cap) {
        g[src].push_back(edges.size());
        edges.emplace_back(src, dst, cap, 0);
        g[dst].push_back(edges.size());
        edges.emplace_back(dst, src, 0, 0);
    }
    DinitzFlow(int n, int s, int t)
            : n(n), s(s), t(t), g(n), layer(n), ptr(n) {
    }
};
int n, m;
const int maxN = 9005;
int k;
int where[maxN];
int a[maxN];
void solve() {
    cin >> n >> m >> k;
    int s = n + 2 * m + 5;
    int t = n + 2 * m + 6;
    DinitzFlow ds(n + 2 * m + 7, s, t);
    for (int i = 1; i <= n; i++) {
        where[i] = i;
        ds.addEdge(s, where[i], 1);
        cin >> a[i];
    }
    int SZ = n;
    for (int i = 1; i <= m; i++) {
        int x, y;
        cin >> x >> y;
        int prv_x = where[x];
        int prv_y = where[y];
        SZ++;
        where[x] = SZ;
        SZ++;
        where[y] = SZ;
        assert(SZ < s);
        ds.addEdge(prv_x, prv_y, 1);
        ds.addEdge(prv_y, prv_x, 1);
        ds.addEdge(prv_x, where[x], a[x]);
        ds.addEdge(prv_y, where[y], a[y]);
    }
    for (int i = 1; i <= k; i++) {
        int p;
        cin >> p;
        ds.addEdge(where[p], t, a[p]);
    }
    cout << ds.flow() << '\n';
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#ifdef DEBUG
    freopen("input.txt", "r", stdin);
#endif
    int tst;
    cin >> tst;
    while (tst--) {
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 176ms
memory: 4928kb

input:

10
2621 3000 873
542 5 3 3 1 2 8 2 2 4 1 1 2 5 3 1 5 1 2 4 3 1 1 6 3 3 3 1 1 1 1 5 6 2 4 2 2 2 1 5 2 1 3 3 2 2 1 3 1 1 4 3 3 2 4 2 2 1 1 3 1 1 1 2 2 1 2 3 3 2 1 1 5 1 3 3 1 1 3 2 2 2 3 4 4 4 3 1 1 1 1 2 1 1 1 1 1 1 4 2 2 2 2 1 6 2 3 1 3 2 3 1 3 2 1 2 1 3 1 2 1 6 1 3 1 3 2 1 2 2 2 2 4 3 1 3 1 4 1 2 1...

output:

1584
1441
1691
1673
1444
1481
1573
1399
1839
1788

result:

ok 10 lines