結果
問題 | No.2604 Initial Motion |
ユーザー |
![]() |
提出日時 | 2024-01-12 23:16:50 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,507 ms / 3,000 ms |
コード長 | 6,370 bytes |
コンパイル時間 | 1,924 ms |
コンパイル使用メモリ | 154,252 KB |
最終ジャッジ日時 | 2025-02-18 19:06:07 |
ジャッジサーバーID (参考情報) |
judge2 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 39 |
ソースコード
#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 upll div_down(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded downll math_mod(ll a, ll b) { return a - b * div_down(a, b); }#define tcT template<class T#define tcTU tcT, class UtcT> 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);#endifint 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;}