Submission #145327

# Submission time Handle Problem Language Result Execution time Memory
145327 2019-08-19T15:34:21 Z MKopchev Homecoming (BOI18_homecoming) C++14
13 / 100
506 ms 262148 KB
#include <bits/stdc++.h>
#include "homecoming.h"
#define endl '\n'

using namespace std;
const int MAXN = (1 << 22);

template<class FlowT>
struct max_flow
{
    const static FlowT finf = 1e18 + 42 + 17;
    const static FlowT feps = 0;

    struct edge
    {
        FlowT flow, cap;
        int idx, rev, to;
        edge() { flow = 0; cap = 0; rev = 0; idx = 0; to = 0; }
        edge(int _to, int _rev, FlowT _flow, FlowT _cap, int _idx)
        {
            to = _to; rev = _rev;
            flow = _flow; cap = _cap;
            idx = _idx;
        }
    };

    vector<edge> G[MAXN];
    int n, dist[MAXN], po[MAXN];

    bool bfs(int s, int t)
    {
        dist[s] = -1, po[s] = 0;
        dist[t] = -1, po[t] = 0;
        for(int v = 0; v <= n; v++)
            dist[v] = -1, po[v] = 0;

        queue<int> Q;
        Q.push(s);
        dist[s] = 0;

        while(!Q.empty())
        {
            int u = Q.front();
            Q.pop();

            for(edge e: G[u])
                if(dist[e.to] == -1 && e.flow < e.cap)
                {
                    dist[e.to] = dist[u] + 1;
                    Q.push(e.to);
                }
        }

        return dist[t] != -1;
    }

    FlowT dfs(int u, int t, FlowT fl = finf)
    {
        if(u == t)
            return fl;

        for(; po[u] < G[u].size(); po[u]++)
        {
            auto &e = G[u][po[u]];
            if(dist[e.to] == dist[u] + 1 && e.flow < e.cap)
            {
                FlowT f = dfs(e.to, t, min(fl, e.cap - e.flow));

                e.flow += f;
                G[e.to][e.rev].flow -= f;

                if(f > 0)
                    return f;
            }
        }

        return 0;
    }

    void init(int _n) { n = _n; for(int i = 0; i <= n; i++) G[i].clear(); }

    void add_edge(int u, int v, FlowT w, int idx = -1)
    {
        G[u].push_back(edge(v, G[v].size(), 0, w, idx));
        G[v].push_back(edge(u, G[u].size() - 1, 0, 0, -1));
    }

    FlowT flow(int s, int t)
    {
        if(s == t) return finf;

        FlowT ret = 0, to_add;
        while(bfs(s, t))
            while((to_add = dfs(s, t)))
                ret += to_add;

        return ret;
    }
};

max_flow<long long> f;

long long int solve(int N, int K, int *A, int *B)
{
    f.init(2*N+5);
    for(int i=1;i<=N;i++)
        f.add_edge(0,i,A[i-1]);
    for(int i=1;i<=N;i++)
        f.add_edge(N+i,2*N+1,B[i-1]);

    for(int i=0;i<N;i++)
        for(int add=0;add<K;add++)
        {
            int j=(i+add)%N;
            f.add_edge(i+1,N+j+1,1e18);
        }
    long long ret=0;
    for(int i=0;i<N;i++)
        ret=ret+A[i];
    return ret-f.flow(0,2*N+1);
}

Compilation message

homecoming.cpp: In instantiation of 'FlowT max_flow<FlowT>::dfs(int, int, FlowT) [with FlowT = long long int]':
homecoming.cpp:94:32:   required from 'FlowT max_flow<FlowT>::flow(int, int) [with FlowT = long long int]'
homecoming.cpp:120:30:   required from here
homecoming.cpp:62:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(; po[u] < G[u].size(); po[u]++)
# Verdict Execution time Memory Grader output
1 Correct 94 ms 99320 KB Output is correct
2 Correct 126 ms 115900 KB Output is correct
3 Correct 93 ms 98948 KB Output is correct
4 Correct 101 ms 103132 KB Output is correct
5 Correct 102 ms 102264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 94 ms 99320 KB Output is correct
2 Correct 126 ms 115900 KB Output is correct
3 Correct 93 ms 98948 KB Output is correct
4 Correct 101 ms 103132 KB Output is correct
5 Correct 102 ms 102264 KB Output is correct
6 Runtime error 506 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 377 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 94 ms 99320 KB Output is correct
2 Correct 126 ms 115900 KB Output is correct
3 Correct 93 ms 98948 KB Output is correct
4 Correct 101 ms 103132 KB Output is correct
5 Correct 102 ms 102264 KB Output is correct
6 Runtime error 506 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
7 Halted 0 ms 0 KB -