結果
| 問題 |
No.2604 Initial Motion
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-01-12 22:35:52 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 885 ms / 3,000 ms |
| コード長 | 6,299 bytes |
| コンパイル時間 | 2,117 ms |
| コンパイル使用メモリ | 186,616 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-09-27 23:16:19 |
| 合計ジャッジ時間 | 18,614 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 39 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
#define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i)
#define rep(i, n) drep(i, 0, n - 1)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define fi first
#define se second
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
const ll MOD1000000007 = 1000000007;
const ll MOD998244353 = 998244353;
const ll MOD[3] = {999727999, 1070777777, 1000000007};
const ll LINF = 1LL << 60LL;
const int IINF = (1 << 30) - 1;
template<typename T> struct Edge{
int to; T w;
Edge(int to_, T w_=1){
to = to_;
w=w_;
}
};
template<typename T> using Tree = vector<vector<Edge<T>>>;
template<typename T> using Graph = vector<vector<Edge<T>>>;
/* 容量&重み付きエッジ for Dinic */
template<typename T> struct REdge{
int to;
T cap;
T cost;
int rev;
REdge(int to_, T cap_, T cost_=1){
to = to_;
cap = cap_;
cost = cost_;
}
REdge(int to_, T cap_, T cost_, int rev_){
to = to_;
cap = cap_;
cost = cost_;
rev = rev_;
}
};
/* 残余グラフ for Dinic */
template<typename T> using RGraph = vector<vector<REdge<T>>>;
// ここから
template<typename T>
struct MinCostFlow{
vector<vector<REdge<T>>> graph;
int n;
MinCostFlow(RGraph<T> graph_){
n = (int)graph_.size();
graph.resize(n);
for(int from=0; from<n; from++){
for(REdge<T> e : graph_[from]){
graph[from].push_back(REdge<T>(e.to, e.cap, e.cost, (int)graph[e.to].size()));
graph[e.to].pb(REdge<T>(from, 0, -e.cost, (int)graph[from].size()-1));
}
}
}
//コストが非負実数の場合
T min_cost_flow1(int s, int t, T f){
RGraph<T> rgraph(n);
vector<T> h(n, 0); //ポテンシャル
vector<T> dist(n, 0); //最短距離
vector<int> prevv(n, 0); // 直前の頂点
vector<int> preve(n, 0); // 直前の辺
for(int from=0; from<n; from++) for(REdge<T> e : graph[from]) rgraph[from].push_back(e);
T res = 0;
while(f > 0){
//ダイクストラ法を用いてhを更新
priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> PQ;
for(int i=0; i<n; i++) dist[i] = LINF;
dist[s] = 0;
PQ.push({0, s});
while(!PQ.empty()){
pair<T, int> p = PQ.top();
PQ.pop();
int v = p.se;
if(dist[v] < p.fi) continue;
for(int i=0; i<(int)rgraph[v].size(); i++){
REdge<T> &e = rgraph[v][i];
if(e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]){
dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
prevv[e.to] = v;
preve[e.to] = i;
PQ.push({dist[e.to], e.to});
}
}
}
if(dist[t] == LINF){
//これ以上流せない
return -1;
}
for(int v=0; v<n; v++) h[v] += dist[v];
// s-t間最短経路に沿って目一杯流す
T d = f;
for(int v=t; v!=s; v=prevv[v]){
d = min(d, rgraph[prevv[v]][preve[v]].cap);
}
f -= d;
res += d*h[t];
for(int v=t; v!=s; v=prevv[v]){
REdge<T> &e = rgraph[prevv[v]][preve[v]];
e.cap -= d;
rgraph[v][e.rev].cap += d;
}
}
return res;
}
//負のコストが存在する場合
T min_cost_flow2(int s, int t, T f){
RGraph<T> rgraph(n);
vector<T> dist(n, 0); //最短距離
vector<int> prevv(n, 0); // 直前の頂点
vector<int> preve(n, 0); // 直前の辺
for(int from=0; from<n; from++) for(REdge<T> e : graph[from]) rgraph[from].push_back(e);
T res = 0;
while(f > 0){
//ベルマンフォード法により, s-t間最短経路を求める
for(int i=0; i<n; i++) dist[i] = LINF;
dist[s] = 0;
bool update = true;
while(update){
update = false;
for(int v=0; v<n; v++){
if(dist[v]==LINF) continue;
for(int i=0; i<(int)rgraph[v].size(); i++){
REdge<T> &e = rgraph[v][i];
if(e.cap > 0 && dist[e.to] > dist[v] + e.cost){
dist[e.to] = dist[v] + e.cost;
prevv[e.to] = v;
preve[e.to] = i;
update = true;
}
}
}
}
if(dist[t] == LINF){
//これ以上流せない
return -1;
}
//s-t間最短経路に沿って目一杯流す
T d = f;
for(int v=t; v!=s; v=prevv[v]){
d = min(d, rgraph[prevv[v]][preve[v]].cap);
}
f -= d;
res += d*dist[t];
for(int v=t; v!=s; v=prevv[v]){
REdge<T> &e = rgraph[prevv[v]][preve[v]];
e.cap -= d;
rgraph[v][e.rev].cap += d;
}
}
return res;
}
};
void solve(){
int K, N, M; cin >> K >> N >> M;
vector<int> A(K);
for(int i=0; i<K; i++){
cin >> A[i];
A[i]--;
}
vector<int> B(N);
for(int i=0; i<N; i++){
cin >> B[i];
}
RGraph<ll> G(N+2);
int s = N, t = N+1;
for(int i=0; i<K; i++) G[s].pb(REdge<ll>(A[i], 1, 0LL));
for(int i=0; i<M; i++){
int u, v;
ll d;
cin >> u >> v >> d;
u--; v--;
G[u].pb(REdge<ll>(v, LINF, d));
G[v].pb(REdge<ll>(u, LINF, d));
}
for(int i=0; i<N; i++) G[i].pb(REdge<ll>(t, B[i], 0LL));
MinCostFlow<ll> mcf(G);
cout << mcf.min_cost_flow1(s, t, K) << '\n';
}
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
int T=1;
//cin >> T;
while(T--) solve();
}