結果

問題 No.2604 Initial Motion
ユーザー Astral__
提出日時 2024-01-12 23:02:14
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,048 ms / 3,000 ms
コード長 14,420 bytes
コンパイル時間 2,947 ms
コンパイル使用メモリ 230,000 KB
最終ジャッジ日時 2025-02-18 18:57:18
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 39
権限があれば一括ダウンロードができます

ソースコード

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

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
//using uLL = unsigned __int128;
using PT = priority_queue<tuple<ll, ll, ll>, vector<tuple<ll, ll, ll>>, greater<tuple<ll, ll, ll>>>;
using PPQ = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;
using PQ = priority_queue<ll, vector<ll>, greater<ll>>;
using P = pair<ll, ll>;
using vvvvl = vector<vector<vector<vector<ll>>>>;
using vvvi = vector<vector<vector<int>>>;
using vvvl = vector<vector<vector<ll>>>;
using vvvc = vector<vector<vector<char>>>;
using vvvd = vector<vector<vector<double>>>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvs = vector<vector<string>>;
using vvc = vector<vector<char>>;
using vvp = vector<vector<pair<ll, ll>>>;
using vvb = vector<vector<bool>>;
using vvd = vector<vector<double>>;
using vp = vector<pair<ll, ll>>;
using vi = vector<int>;
using vl = vector<ll>;
using vu = vector<unsigned long long>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
#define vvvm vector<vector<vector<mint>>>
#define vvm vector<vector<mint>>
#define vm vector<mint>
#define umap unordered_map
#define uset unordered_set
#define rrr(l, r) mt()%(r-l+1)+l
#define rep(i, s, f) for(long long i = s; i <= f; i++)
#define rep1(i, s, f) for(long long i = s; i <= f; i++)
#define per(i, s, f) for(long long i = s; i >= f; i--)
#define per1(i, s, f) for(long long i = s; i >= f; i--)
#define all0(x) (x).begin() ,(x).end()
#define all(x) (x).begin() + 1, (x).end()
#define ENDL '\n'
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
template <typename T>
T or_less(vector<T> &A, T x) { //x : sort : -1
return distance(A.begin(), upper_bound(A.begin(), A.end(), x)-1);
}
template <typename T>
T under(vector<T> &A, T x) { //x : sort : -1
return distance(A.begin(), lower_bound(A.begin(), A.end(), x)-1);
}
template <typename T>
T or_more(vector<T> &A, T x) { //x  : sort : . //distanceA.beginA
    .begin() NG: A.begin() + 1
return distance(A.begin(), lower_bound(A.begin(), A.end(), x));
}
template <typename T>
T over(vector<T> &A, T x) { //x: sort :
return distance(A.begin(), upper_bound(A.begin(), A.end(), x));
}
template <typename T>
vector<T> vec_shift(vector<T> A, ll step, ll dir, ll indexed) {//dir = 1 : dir = -1 :
ll N = A.size() - indexed;
vector<T> res(N+1);
rep(i, indexed, N) {
ll idx = i - step * dir;
if(idx < indexed) idx += N;
if(idx > N) idx -= N;
res.at(i) = A.at(idx);
}
return res;
}
template <typename T>
void UNIQUE(vector<T> &A, int indexed) {
sort(A.begin() + indexed, A.end());
A.erase(unique(A.begin() + indexed, A.end()), A.end());
}
template <typename T>
vector<T> RLE(vector<T> &A, int indexed) {
vector<T> res(indexed, -INT_MAX);
for(int i = indexed; i < int(A.size()); i++) {
if(i == indexed) res.push_back(A[i]);
else if(A[i-1] != A[i]) res.push_back(A[i]);
}
return res;
}
template <typename T>
void rev90(vector<vector<T>> &A, int indexed) {
reverse(A.begin() + indexed, A.end());
int n = A.size();
rep(i, indexed, n-1) {
rep(j, i+1, n-1) {
swap(A.at(i).at(j), A.at(j).at(i));
}
}
}
int msb(long long a) {
if(a == 0) return -1;
return 64 - int(__builtin_clzll(a));
}
template<typename T = long long>
void chmin(T &a, T b) {
a = min(a, b);
}
template<typename T = long long>
void chmax(T &a, T b) {
a = max(a, b);
}
//////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////
ll round(ll x, ll i) {return ll(x + 5 * powl(10, i-1))/ll(powl(10, i)) * ll(powl(10, i));}
vp insu_bunkai(ll N) {
vp res;
for (ll i = 2; i * i <= N; i++) {
ll cnt = 0;
while(N % i == 0) {
cnt++;
N /= i;
}
if(cnt != 0) res.push_back(P(i, cnt));
}
if(N != 1) res.push_back(P(N, 1));
return res;
}
ll extgcd (ll a, ll b, ll &x, ll &y) {
if(b == 0) { x = 1;y = 0;return a;}
ll d = extgcd(b, a%b, y, x);
y -= a/b * x;
return d;
}
template <typename T>
T ceil(T a, T b) {
assert(b != 0);
if(a % b == 0) return a / b;
if((a <= 0 && b < 0) || (a >= 0 && b > 0)) return a/b + 1;
else return a / b;
}
template <typename T>
T floor(T a, T b) {
assert(b != 0);
if(a % b == 0) return a / b;
if((a <= 0 && b < 0) || (a >= 0 && b > 0)) return a/b;
else return a/b - 1;
}
long long Calnum(long long l, long long r, long long M, long long m) {//[l, r]modMm
l -= m; r -= m;//
l = ceil(l, M) * M;
r = floor(r, M) * M;
if(l > r) return 0LL;
return (r - l)/M+1;
}
ll modpow(ll x, ll y, ll mod) {
if(x > mod) x %= mod;
if(y == 0) return 1;
ll res = modpow(x, y >> 1, mod);
res = res * res % mod;
if(y & 1) res *= x, res %= mod;
return res;
}
ll sqrt_(ll a) {
ll l = 0;
ll r = 3037000499LL;
while(l < r) {
ll mid = (l + r + 1) / 2;
if(mid * mid <= a) l = mid;
else r = mid - 1;
}
return l;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
const ll int_max = 1001001001;
const ll ll_max = 1001001001001001001LL;
const double pi = 3.141592653589793;
vl dx{0, 1, 0, -1, 0, 1, 1, -1, -1}; // () → ↓ ← ↑ ※ : or
vl dy{0, 0, -1, 0, 1, 1, -1, -1, 1};
//const ll mod = 1000000007;
//const ll mod = 998244353;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
struct Edge {
int to;
ll cap;
int rev;
ll cost;
};
struct MinCostFlow {
vector<vector<Edge>> G;
int N;
vl h;
vl dist;
vi prevV;// : 使
vi prevE;//
ll now_ryuuryou = 0;
ll now_cost = 0;
vector<pair<int, Edge>> es;// [from, EDGE]
vector<unordered_map<int, int>> getid;
MinCostFlow(int _N) {
N = _N;
G.resize(N+1);
h.assign(N+1, 0);
dist.assign(N+1, ll_max);
prevV.resize(N+1);
prevE.resize(N+1);
es.resize(1);
getid.resize(N+1);
}
private:
ll Daiku(int start, int goal, ll &ryuuryou) {
ll res = 0;
while(ryuuryou > 0) {
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> que;
dist.assign(N+1, ll_max);
dist.at(start) = 0;
que.push(P(0, start));
while(!que.empty()) {
ll cd = que.top().first;
int cp = que.top().second;
que.pop();
if(dist[cp] < cd) {
continue;
}
for (int i = 0; i < G.at(cp).size(); i++) {
Edge e = G.at(cp).at(i);
if(e.cap > 0 && dist[e.to] > dist[cp] + e.cost - (h[e.to] - h[cp])) {
//1h0
dist[e.to] = dist[cp] + e.cost - (h[e.to] - h[cp]);
prevV.at(e.to) = cp;
prevE.at(e.to) = i;
que.push(P(dist.at(e.to), e.to));
}
}
}
if(dist[goal] == ll_max) {
return -1;//辿
}
for (int i = 1; i <= N; i++) {
h[i] += dist[i];//
}
ll flow = ryuuryou;
for (int v = goal; v != start; v = prevV.at(v)) {
flow = min(flow, G[prevV[v]][prevE[v]].cap);//
}
ryuuryou -= flow;
now_ryuuryou += flow;
res += h[goal] * flow;//: dist h
now_cost += h[goal] * flow;
for (int v = goal; v != start; v = prevV[v]) {
Edge &e = G[prevV[v]][prevE[v]];
e.cap -= flow;
G[v][e.rev].cap += flow;
}
}
return res;
}
public:
void add_edge(int from, int to, ll cap, ll cost) {
int Current_Ga_idx = G.at(from).size();
int Current_Gb_idx = G.at(to).size();
G.at(from).push_back(Edge{to, cap, Current_Gb_idx, cost});
G.at(to).push_back(Edge{from, 0, Current_Ga_idx, -1 * cost});//rev 
es.emplace_back(from, Edge{to, cap, Current_Gb_idx, cost});
getid[from][to] = es.size() - 1;
}
void init(int s) {// 辿cost
vl d(N+1, ll_max);
d[s] = 0;
rep(i,1,N) {//|| - 1
rep(j, 0, N) {
for(auto e : G[j]) {
if(e.cap != 0) d[e.to] = min(d[e.to], d[j] + e.cost);//cap = 0 : 
}
}
}
rep(i,1,N)h[i] = d[i];
}// O(EV)
int get_edgecount() {return es.size() - 1;}//O(1)
int get_id(int s, int t) {return getid[s][t];}//s→tidO(1)
ll get_flow(int id) {//id
auto [_, e] = es[id];
return G[e.to][e.rev].cap;
}//O(1)
pair<int, int> get_st(int id) {//ids→t
auto [s, e] = es[id];
return make_pair(s, e.to);
}//O(1)
void change_edge(int id, ll new_cap, ll new_flow) {//new_flow = get_flow(id)
assert(new_cap >= new_flow);
auto[s, e] = es[id];
G[s][G[e.to][e.rev].rev] = Edge{e.to, new_cap-new_flow, e.rev, e.cost};
G[e.to][e.rev] = Edge{s, new_flow, G[e.to][e.rev].rev, -e.cost};
}//O(1)
P min_cost(int s, int t, ll Totalryuuryou) {//{}
ll ryuuryou = Totalryuuryou - now_ryuuryou;
Daiku(s, t, ryuuryou);
return P(now_cost, now_ryuuryou);
} //O(FElog(V)) or O(FV^2)
};
void solve() {
ll K, N, M;
cin >> K >> N >> M;
vvl dist(N+1, vl(N+1, ll_max));
rep(i,1,N) dist[i][i] = 0;
vl A(K+1), B(N+1);
rep(i,1,K)cin >> A[i];
rep(i,1,N)cin >> B[i];
vvp G(N+1);
rep(i,1,M) {
ll u, v, d;
cin >> u >> v >> d;
G[u].emplace_back(d, v);
G[v].emplace_back(d, u);
}
/*
rep(i,1,N) {
PPQ que;
que.push(P(0, i));
while(!que.empty()) {
auto [c, now] = que.top();
que.pop();
if(dist[i][now] < c) continue;
for(auto [cos, to] : G[now]) {
if(dist[i][to] < cos + c) continue;
dist[i][to] = cos + c;
que.push(P(cos + c, to));
}
}
}
MinCostFlow flow(K + N + 1);
ll s = 0;
ll t = K + N + 1;
rep(i,1,K) {
flow.add_edge(0,i,1,0);
rep(j, 1, N) {
flow.add_edge(i, K + j, 1, dist[A[i]][j]);
}
}
rep(i,1,N) {
flow.add_edge(K+i,t,B[i],0);
}
ll ans = flow.min_cost(s, t, K).first;
cout << ans << ENDL;
*/
MinCostFlow flow(N+1);
ll s = 0;
ll t = N + 1;
rep(i,1,K) {
flow.add_edge(0, A[i], 1, 0);
}
rep(i,1,N) {
flow.add_edge(i, t, B[i], 0);
}
rep(i,1,N) {
for(auto [c, to] : G[i]) {
flow.add_edge(i, to, K+1, c);
flow.add_edge(to, i, K+1, c);
}
}
cout << flow.min_cost(s, t, K).first << ENDL;
}
//umap使)
//
//0-indexed
//return
//LIS
//mint0(: assert
///2floor
//DP!""
//r"lN"139)
//0
//”
//(
//2( :(x / 2) :floor<ll>(x, 2);
//string1 :S.at(N-1) :S.at(0)
//if(S.at(i) == 1) ← char1...?
// mod...?(´ω)
//sort a == b  falseRE(
int main() {
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
cout << fixed << setprecision(15);
ll T = 1;
//cin >> T;
rep(i, 1, T) {
solve();
}
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0