Submission #1330918


Source Code Expand

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
//---------------------------------------------------------------------------------------------------
template<int NV, class V> struct MinCostFlow {
    struct edge { int to, capacity; V cost; int reve;
        edge(int a, int b, V c, int d) { to = a; capacity = b; cost = c; reve = d; } };
    vector<edge> E[NV];
    int prev_v[NV], prev_e[NV]; V dist[NV];
    void add_edge(int x, int y, int cap, V cost) {
        E[x].push_back(edge(y, cap, cost, (int)E[y].size()));
        E[y].push_back(edge(x, 0, -cost, (int)E[x].size() - 1)); /* rev edge */
    }
    V mincost(int from, int to, int flow) { // 流せないなら-1が返る
        V res = 0; int i, v; rep(i, 0, NV) prev_v[i] = 0; rep(i, 0, NV) prev_e[i] = 0;
        while (flow>0) {
            fill(dist, dist + NV, numeric_limits<V>::max() / 2); dist[from] = 0;
            priority_queue<pair<int, int> > Q; Q.push(make_pair(0, from));
            while (Q.size()) { int d = -Q.top().first, cur = Q.top().second; Q.pop();
                if (dist[cur] != d) continue; if (d == numeric_limits<V>::max() / 2) break;
                rep(i, 0, E[cur].size()) {
                    edge &e = E[cur][i];
                    if (e.capacity>0 && dist[e.to]>d + e.cost) {
                        dist[e.to] = d + e.cost; prev_v[e.to] = cur; prev_e[e.to] = i;
                        Q.push(make_pair(-dist[e.to], e.to)); } } }
            if (dist[to] == numeric_limits<V>::max() / 2) return -1; int lc = flow;
            for (v = to; v != from; v = prev_v[v]) lc = min(lc, E[prev_v[v]][prev_e[v]].capacity);
            flow -= lc; res += lc*dist[to];
            for (v = to; v != from; v = prev_v[v]) { edge &e = E[prev_v[v]][prev_e[v]];
                e.capacity -= lc; E[v][e.reve].capacity += lc; } } return res;}
};
/*---------------------------------------------------------------------------------------------------
            ∧_∧  
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     
    /   \     | |     
    /   / ̄ ̄ ̄ ̄/  |  
  __(__ニつ/     _/ .| .|____  
     \/____/ (u ⊃  
---------------------------------------------------------------------------------------------------*/







int N, M;
pair<int, int> A[50], B[50];
int D[50];
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> N >> M;
    rep(i, 0, N) {
        string s; int x, y;
        cin >> s >> x >> y;
        A[i] = { x, y };
    }
    map<int, vector<int>> dic;
    rep(i, 0, M) {
        string s; int m, x, y;
        cin >> s >> m >> x >> y;
        B[i] = { x, y };
        D[i] = m;
        dic[m].push_back(i);
    }

    int K = min(N, M);
    int cnt = 0;

    MinCostFlow<110, int> mcf;
    int s = N + M, t = N + M + 1, tt = N + M + 2;

    for (auto ite = dic.rbegin(); ite != dic.rend(); ite++) {
        int ma = ite->first;
        auto v = ite->second;


        if (cnt + v.size() < K) {
            for (int i : v) mcf.add_edge(i + N, t, 1, 0);
            cnt += v.size();
        }
        else if (cnt < K) {
            for (int i : v) mcf.add_edge(i + N, tt, 1, 0);
            mcf.add_edge(tt, t, K - cnt, 0);
            cnt += v.size();
        }
    }

    rep(i, 0, N) mcf.add_edge(s, i, 1, 0);
    rep(i, 0, N) rep(j, 0, M) {
        int d = abs(A[i].first - B[j].first) + abs(A[i].second - B[j].second);
        if (D[j] == 0) d = 0;
        mcf.add_edge(i, j + N, 1, d);
    }

    cout << mcf.mincost(s, t, K) << endl;
}

Submission Info

Submission Time
Task F - 3人の騎士と1匹の犬
User hamayanhamayan
Language C++14 (GCC 5.4.1)
Score 100
Code Size 3918 Byte
Status AC
Exec Time 3 ms
Memory 384 KB

Judge Result

Set Name All
Score / Max Score 100 / 100
Status
AC × 23
Set Name Test Cases
All 00-sample, 01-minimum, 02-maximum, 50-random-00, 50-random-01, 50-random-02, 50-random-03, 50-random-04, 50-random-05, 50-random-06, 50-random-07, 50-random-08, 50-random-09, 50-random-10, 50-random-11, 50-random-12, 50-random-13, 50-random-14, 50-random-15, 50-random-16, 50-random-17, 50-random-18, 50-random-19
Case Name Status Exec Time Memory
00-sample AC 1 ms 256 KB
01-minimum AC 1 ms 256 KB
02-maximum AC 3 ms 384 KB
50-random-00 AC 2 ms 384 KB
50-random-01 AC 2 ms 256 KB
50-random-02 AC 2 ms 384 KB
50-random-03 AC 2 ms 256 KB
50-random-04 AC 2 ms 384 KB
50-random-05 AC 1 ms 256 KB
50-random-06 AC 2 ms 256 KB
50-random-07 AC 2 ms 256 KB
50-random-08 AC 2 ms 256 KB
50-random-09 AC 2 ms 256 KB
50-random-10 AC 1 ms 256 KB
50-random-11 AC 2 ms 384 KB
50-random-12 AC 2 ms 256 KB
50-random-13 AC 1 ms 256 KB
50-random-14 AC 1 ms 256 KB
50-random-15 AC 1 ms 256 KB
50-random-16 AC 1 ms 256 KB
50-random-17 AC 1 ms 256 KB
50-random-18 AC 2 ms 256 KB
50-random-19 AC 3 ms 384 KB