結果

問題 No.2604 Initial Motion
ユーザー Misuki
提出日時 2024-01-12 22:01:12
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 863 ms / 3,000 ms
コード長 4,909 bytes
コンパイル時間 2,397 ms
コンパイル使用メモリ 213,080 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-27 22:13:06
合計ジャッジ時間 17,988 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 39
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#pragma GCC optimize("O2")
#include <algorithm>
#include <array>
#include <bit>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <cfloat>
#include <chrono>
#include <cinttypes>
#include <climits>
#include <cmath>
#include <compare>
#include <complex>
#include <concepts>
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <new>
#include <numbers>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <ranges>
#include <set>
#include <span>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <tuple>
#include <type_traits>
#include <variant>
#define int ll
#define INT128_MAX (__int128)(((unsigned __int128) 1 << ((sizeof(__int128) * __CHAR_BIT__) - 1)) - 1)
#define INT128_MIN (-INT128_MAX - 1)
#ifdef DEBUG
#define dbg(x) cout << (#x) << " = " << x << '\n'
#else
#define dbg(x)
#endif
namespace R = std::ranges;
namespace V = std::views;
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ldb = long double;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
//#define double ldb
template<class T, size_t N>
ostream& operator<<(ostream& os, const array<T, N> &arr) {
for(const T &X : arr)
os << X << ' ';
return os;
}
template<class T>
ostream& operator<<(ostream& os, const vector<T> &vec) {
for(const T &X : vec)
os << X << ' ';
return os;
}
template<class T>
ostream& operator<<(ostream& os, const set<T> &s) {
for(const T &x : s)
os << x << ' ';
return os;
}
/**
* template name: MCMF
* author: Misuki
* last update: 2021/11/11
*/
struct MCMF {
struct Edge {
int to, rev;
long long cap, cos;
Edge(int _to, long long _cap, long long _cos, int _rev) :
to(_to), cap(_cap), cos(_cos), rev(_rev) {}
};
static const int SIZE = 2002;
int n, s, t;
vector<Edge> G[SIZE];
array<int, SIZE> par, idx;
array<long long, SIZE> pot, dis, f;
void init(int _n, int _s, int _t) {
n = _n, s = _s, t = _t;
for(int i = 0; i < n; i++)
G[i].clear();
}
void addEdge(int from, int to, long long cap, long long cos) {
G[from].emplace_back(Edge(to, cap, cos, G[to].size()));
G[to].emplace_back(Edge(from, 0, -cos, (int)G[from].size() - 1));
}
void initPotential() {
fill(dis.begin(), dis.end(), LLONG_MAX);
dis[s] = 0;
for(int i = 1; i < n; i++) {
for(int j = 0; j < n; j++) {
if (dis[j] == LLONG_MAX)
continue;
for(Edge E : G[j]) {
if (E.cap == 0)
continue;
if (dis[j] + E.cos < dis[E.to])
dis[E.to] = dis[j] + E.cos;
}
}
}
pot.swap(dis);
}
pll flow() {
long long Cost = 0, Flow = 0;
while(true) {
priority_queue<pii, vector<pii>, greater<pii> > pq;
fill(dis.begin(), dis.end(), LLONG_MAX);
dis[s] = 0, f[s] = LLONG_MAX;
pq.push(make_pair(0, s));
while(!pq.empty()) {
pii now = pq.top(); pq.pop();
if (dis[now.second] != now.first)
continue;
int V = now.second;
for(Edge E : G[V]) {
if (E.cap == 0)
continue;
if (dis[V] + E.cos + pot[V] - pot[E.to] < dis[E.to]) {
dis[E.to] = dis[V] + E.cos + pot[V] - pot[E.to];
f[E.to] = min(f[V], E.cap);
par[E.to] = V;
idx[E.to] = G[E.to][E.rev].rev;
pq.push(make_pair(dis[E.to], E.to));
}
}
}
if (dis[t] == LLONG_MAX)
break;
long long bot = f[t];
int now = t;
while(now != s) {
Edge &E = G[par[now]][idx[now]];
E.cap -= bot;
G[now][E.rev].cap += bot;
now = par[now];
}
Flow += bot, Cost += bot * (dis[t] - pot[s] + pot[t]);
for(int i = 0; i < n; i++) {
dis[i] += pot[i] - pot[s];
}
pot.swap(dis);
}
return make_pair(Flow, Cost);
}
};
signed main() {
ios::sync_with_stdio(false), cin.tie(NULL);
int k, n, m; cin >> k >> n >> m;
vector<int> a(k), b(n);
for(int &x : a) {
cin >> x;
x--;
}
for(int &x : b)
cin >> x;
vector<array<int, 3>> e(m);
for(auto &[u, v, w] : e) {
cin >> u >> v >> w;
u--, v--;
}
const int S = n, T = n + 1;
MCMF flow;
flow.init(n + 2, S, T);
for(auto [u, v, w] : e) {
flow.addEdge(u, v, INT_MAX, w);
flow.addEdge(v, u, INT_MAX, w);
}
for(int x : a)
flow.addEdge(S, x, 1, 0);
for(int i = 0; i < n; i++)
flow.addEdge(i, T, b[i], 0);
flow.initPotential();
cout << flow.flow().second << '\n';
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0