結果

問題 No.2604 Initial Motion
ユーザー fastmathfastmath
提出日時 2024-01-12 23:16:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,415 ms / 3,000 ms
コード長 6,370 bytes
コンパイル時間 2,022 ms
コンパイル使用メモリ 160,264 KB
実行使用メモリ 6,548 KB
最終ジャッジ日時 2024-01-12 23:17:20
合計ジャッジ時間 28,810 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 3 ms
6,548 KB
testcase_03 AC 39 ms
6,548 KB
testcase_04 AC 38 ms
6,548 KB
testcase_05 AC 39 ms
6,548 KB
testcase_06 AC 40 ms
6,548 KB
testcase_07 AC 40 ms
6,548 KB
testcase_08 AC 39 ms
6,548 KB
testcase_09 AC 40 ms
6,548 KB
testcase_10 AC 39 ms
6,548 KB
testcase_11 AC 39 ms
6,548 KB
testcase_12 AC 38 ms
6,548 KB
testcase_13 AC 1,245 ms
6,548 KB
testcase_14 AC 921 ms
6,548 KB
testcase_15 AC 467 ms
6,548 KB
testcase_16 AC 1,191 ms
6,548 KB
testcase_17 AC 1,415 ms
6,548 KB
testcase_18 AC 1,352 ms
6,548 KB
testcase_19 AC 1,337 ms
6,548 KB
testcase_20 AC 1,139 ms
6,548 KB
testcase_21 AC 953 ms
6,548 KB
testcase_22 AC 1,323 ms
6,548 KB
testcase_23 AC 1,016 ms
6,548 KB
testcase_24 AC 1,219 ms
6,548 KB
testcase_25 AC 1,301 ms
6,548 KB
testcase_26 AC 1,119 ms
6,548 KB
testcase_27 AC 817 ms
6,548 KB
testcase_28 AC 1,055 ms
6,548 KB
testcase_29 AC 1,235 ms
6,548 KB
testcase_30 AC 884 ms
6,548 KB
testcase_31 AC 1,105 ms
6,548 KB
testcase_32 AC 946 ms
6,548 KB
testcase_33 AC 314 ms
6,548 KB
testcase_34 AC 537 ms
6,548 KB
testcase_35 AC 550 ms
6,548 KB
testcase_36 AC 532 ms
6,548 KB
testcase_37 AC 242 ms
6,548 KB
testcase_38 AC 2 ms
6,548 KB
testcase_39 AC 3 ms
6,548 KB
testcase_40 AC 412 ms
6,548 KB
testcase_41 AC 399 ms
6,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <fstream>
#include <array>
#include <functional>
#include <stack>
#include <memory>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define x first
#define y second
#define Time (double)clock()/CLOCKS_PER_SEC
#define munq(a) sort(all(a)); a.resize(unique(all(a))-a.begin())
#define sz(a) ((int)a.size())

#ifdef LOCAL
#define debug(x) do { cout << #x << ": " << x << endl; } while(0)
#define debug2(x, y) do { std::cerr << #x << ", " << #y << ": " << x << ", " << y << endl; } while (0)
#define debug3(x, y, z) do {std::cerr << #x << ", " << #y << ", " << #z << ": " << x << ", " << y << ", " << z << endl; } while(0)
#else
#define debug(x)
#define debug2(x, y) 
#define debug3(x, y, z) 
#endif

#define FORI(i,a,b) for (int i = (a); i < (b); ++i)
#define FOR(i,a) FORI(i,0,a)
#define ROFI(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define ROF(i,a) ROFI(i,0,a)
#define rep(a) FOR(_,a)
#define each(a,x) for (auto& a: x)
#define FORN(i, n) FORI(i, 1, n + 1)

using vi = vector<int>;
template <typename T>
std::istream& operator >>(std::istream& input, std::pair <T, T> & data)
{
    input >> data.x >> data.y;
    return input;
}
template <typename T>
std::istream& operator >>(std::istream& input, std::vector<T>& data)
{
    for (T& x : data)
        input >> x;
    return input;
}
template <typename T>
std::ostream& operator <<(std::ostream& output, const pair <T, T> & data)
{
    output << "(" << data.x << "," << data.y << ")";
    return output;
}
template <typename T>
std::ostream& operator <<(std::ostream& output, const std::vector<T>& data)
{
    for (const T& x : data)
        output << x << " ";
    return output;
}
ll div_up(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up
ll div_down(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down 
ll math_mod(ll a, ll b) { return a - b * div_down(a, b); }
#define tcT template<class T
#define tcTU tcT, class U
tcT> using V = vector<T>; 
tcT> bool ckmin(T& a, const T& b) {
    return b < a ? a = b, 1 : 0; 
} // set a = min(a,b)
tcT> bool ckmax(T& a, const T& b) {
    return a < b ? a = b, 1 : 0; 
}
tcT> vector <T> presum(vector <T> &a) {
    vector <T> p(a.size() + 1);
    FOR (i, a.size()) {
        p[i + 1] = p[i] + a[i];
    }   
    return p;
}
tcT> vector <T> sufsum(vector <T> &a) {
    vector <T> p(a.size() + 1);
    for (int i = (int)a.size() - 1; i >= 0; --i) {
        p[i] = p[i + 1] + a[i];
    }
    return p;
}
ll gcd(ll a, ll b) {
    while (b) {
        tie(a, b) = mp(b, a % b);
    }
    return a;
}
int Bit(int mask, int bit) { return (mask >> bit) & 1; }

signed main() {
    #ifdef LOCAL
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif
    int k,n,m;
    cin >> k >> n >> m;
    vi a(k); cin >> a; 
    each(e, a) {
        e--;
    }
    vi b(n); cin >> b;

    n += 2;

    V <vi> g(n);

    int s = n - 2, t = n - 1;

struct edge {
  int next, capacity, cost, flow = 0;

  edge() = default;

  edge(int next, int capacity, int cost) : next(next), capacity(capacity), cost(cost) {}

  int rem() const { return capacity - flow; }

  int operator+=(int f) { return flow += f; }

  int operator-=(int f) { return flow -= f; }
};
vector <edge> e;
auto addEdge = [&](int from, int next, int capacity, int cost) {
    //debug2(from, next);debug2(capacity, cost);
  g[from].push_back(e.size());
  e.emplace_back(next, capacity, cost);
  g[next].push_back(e.size());
  e.emplace_back(from, 0, -cost);
};
/*
Если граф ориентированный, то addEdge вызываем один раз.
Если неориентированный, то два, вот так:
addEdge(u, v, capacity, cost);
addEdge(v, u, capacity, cost);
*/
vector<ll> phi(n, 0);
auto fordBellman = [&](int s, int t) {
  phi.assign(n, 0);
  for (int iter = 0; iter < n; ++iter) {
    bool changed = false;
    for (int u = 0; u < n; ++u) {
      for (auto index : g[u]) {
        auto edge = e[index];
        if (edge.rem() > 0 && phi[edge.next] > phi[u] + edge.cost) {
          phi[edge.next] = phi[u] + edge.cost;
          changed = true;
        }
      }
    }
    if (!changed) break;
  }
};
fordBellman(s, t);

vector<ll> dist;
vector<int> from;
vector<bool> cnt;
auto dijkstra = [&](int s, int t) {
  dist.assign(n, 1e18);
  from.assign(n, -1);
  cnt.assign(n, false);
  dist[s] = 0;
  set <ii> se;
  se.insert({0, s});
  for (int i = 1; i < n; ++i) {
    int cur = se.begin()->y;
    se.erase(se.begin());
    cnt[cur] = true;
    for (int index : g[cur]) {
      auto &edge = e[index];
      if (edge.rem() == 0) continue;
      ll weight = edge.cost + phi[cur] - phi[edge.next];
      if (dist[edge.next] > dist[cur] + weight) {
        se.erase({dist[edge.next], edge.next});
        dist[edge.next] = dist[cur] + weight;
        se.insert({dist[edge.next], edge.next});
        from[edge.next] = cur;
      }
    }
  }
  if (dist[t] == (ll) 1e18) return -1LL;
  ll cost = 0;
  for (int p = t; p != s; p = from[p]) {
    for (auto index : g[from[p]]) {
      auto &edge = e[index];
      ll weight = edge.cost + phi[from[p]] - phi[edge.next];
      if (edge.rem() > 0 && edge.next == p && dist[edge.next] == dist[from[p]] + weight) {
        edge += 1;
        e[index ^ 1] -= 1;
        cost += edge.cost;
        break;
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    phi[i] += dist[i];
  }
  return cost;
};

for (int i = 0; i < k; ++i) {
    addEdge(s, a[i], 1, 0);
}

for (int i = 0; i < n - 2; ++i) {
    addEdge(i, t, b[i], 0);
}
debug("ok");

for (int i = 0; i < m; ++i) {
    int u, v, cost; cin >> u >> v >> cost; u--; v--;
    addEdge(u, v, k, cost);
    addEdge(v, u, k, cost);
}
debug("ok");

ll cost = 0;
for (int flow = 0; flow < k; ++flow) {
  ll a = dijkstra(s, t);
  if (a == -1) {
    cout << "-1\n";
    return 0;
  }
  cost += a;
}

cout << cost << endl;

    
}
0