結果
| 問題 |
No.2604 Initial Motion
|
| コンテスト | |
| ユーザー |
tnakao0123
|
| 提出日時 | 2024-06-18 10:08:42 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 3,331 bytes |
| コンパイル時間 | 1,455 ms |
| コンパイル使用メモリ | 73,952 KB |
| 最終ジャッジ日時 | 2025-02-21 23:14:20 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 36 TLE * 1 -- * 2 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:133:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
133 | scanf("%d%d%d", &k, &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
main.cpp:135:36: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
135 | for (int i = 0; i < k; i++) scanf("%d", as + i), as[i]--;
| ~~~~~^~~~~~~~~~~~~~
main.cpp:136:36: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
136 | for (int i = 0; i < n; i++) scanf("%d", bs + i);
| ~~~~~^~~~~~~~~~~~~~
main.cpp:143:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
143 | scanf("%d%d%lld", &u, &v, &d);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
ソースコード
/* -*- coding: utf-8 -*-
*
* 2604.cc: No.2604 Initial Motion - yukicoder
*/
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
#include<utility>
using namespace std;
/* constant */
const int MAX_K = 2000;
const int MAX_N = 2000;
const int INF = 1 << 30;
const long long LINF = 1LL << 62;
/* typedef */
using ll = long long;
template <typename T>
struct MinCostFlow {
typedef pair<int,int> pii;
typedef pair<T,int> pti;
typedef pair<T,T> ptt;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
int n, m;
vvpii nbrs;
T inf;
T max_flow, min_cost;
vpii prvs;
vector<T> minfs, caps, costs, flows, ds;
MinCostFlow(int _n, T _inf): n(_n), m(0), inf(_inf) {
nbrs.assign(n, vpii());
minfs.assign(n, 0);
prvs.assign(n, pii(-1, -1));
caps.clear(), costs.clear(), flows.clear();
ds.assign(n, 0);
max_flow = min_cost = 0;
}
int addedges(int u, int v, T cap, T cost) {
// edge[m]: u -> v, cap=c
nbrs[u].push_back(pii(v, m));
caps.push_back(cap); costs.push_back(cost); flows.push_back(0);
// edge[m + 1]: v -> u, cap=0
nbrs[v].push_back(pii(u, m + 1));
caps.push_back(0); costs.push_back(-cost); flows.push_back(0);
m += 2;
return m;
}
ptt flow(int st, int gl) { // return ptt(max_flow, min_cost)
return flow(st, gl, inf, false);
}
ptt flow(int st, int gl, T limit, bool cont) {
// return ptt(max_flow, min_cost)
if (! cont) {
fill(flows.begin(), flows.end(), 0);
max_flow = min_cost = 0;
}
while (max_flow < limit) {
//printf("max_flow = %d, limit = %d\n", max_flow, limit);
fill(prvs.begin(), prvs.end(), pii(-1, -1));
fill(ds.begin(), ds.end(), inf);
prvs[st] = pii(st, -1);
minfs[st] = inf;
ds[st] = 0;
priority_queue<pti> q;
q.push(pti(0, st));
while (! q.empty()) {
pti u = q.top(); q.pop();
T ud = -u.first;
int ui = u.second;
if (ud != ds[ui]) continue;
if (ui == gl) break;
for (auto ve: nbrs[ui]) {
int vi = ve.first, ei = ve.second;
T vc = caps[ei] - flows[ei];
T vd = ud + costs[ei];
if (vc > 0 && ds[vi] > vd) {
prvs[vi] = pii(ui, ei);
minfs[vi] = (minfs[ui] < vc) ? minfs[ui] : vc;
ds[vi] = vd;
q.push(pti(-vd, vi));
}
}
}
if (prvs[gl].first < 0) break;
T min_flow = min(minfs[gl], limit - max_flow);
for (int j = gl; j != st;) {
int i = prvs[j].first, ei = prvs[j].second;
flows[ei] += min_flow;
flows[ei ^ 1] -= min_flow;
j = i;
}
max_flow += min_flow;
min_cost += ds[gl] * min_flow;
}
return ptt(max_flow, min_cost);
}
};
/* global variables */
int as[MAX_K], bs[MAX_N];
/* subroutines */
/* main */
int main() {
int k, n, m;
scanf("%d%d%d", &k, &n, &m);
for (int i = 0; i < k; i++) scanf("%d", as + i), as[i]--;
for (int i = 0; i < n; i++) scanf("%d", bs + i);
MinCostFlow<ll> mcf(n + 2, LINF);
for (int i = 0; i < m; i++) {
int u, v;
ll d;
scanf("%d%d%lld", &u, &v, &d);
u--, v--;
mcf.addedges(u, v, INF, d);
mcf.addedges(v, u, INF, d);
}
for (int i = 0; i < k; i++)
mcf.addedges(n, as[i], 1, 0);
for (int i = 0; i < n; i++)
mcf.addedges(i, n + 1, bs[i], 0);
auto f = mcf.flow(n, n + 1);
printf("%lld\n", f.second);
return 0;
}
tnakao0123