QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#784237 | #3564. Admiral | SGColin# | 100 ✓ | 5ms | 4128kb | C++20 | 2.1kb | 2024-11-26 14:14:07 | 2024-11-26 14:14:13 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define pb push_back
#define eb emplace_back
#define all(s) (s).begin(), (s).end()
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
constexpr int V = 2007;
constexpr int E = 100007;
inline bool getmin(int &a, int b) {return (a > b ? (a = b, true) : false);}
int tot = 1, S, T, hd[V];
struct edge{int nxt, to, cap, cst;} e[E << 1];
void clear() {tot = 1; memset(hd, 0, sizeof(hd));}
void add(int u, int v, int cap, int cst) {
e[++tot] = {hd[u], v, cap, cst}; hd[u] = tot;
e[++tot] = {hd[v], u, 0, -cst}; hd[v] = tot;
}
int inid[V], fl[V], dis[V];
inline bool spfa() {
static queue<int> q;
static bool inq[V];
memset(inq, 0, sizeof(inq));
memset(dis, 0x3f, sizeof(dis));
int cstInf = dis[0];
q.push(S); dis[S] = 0; fl[S] = INT_MAX;
while (!q.empty()) {
int u = q.front();
q.pop(); inq[u] = false;
for (int i = hd[u], v; i; i = e[i].nxt)
if (e[i].cap != 0 && getmin(dis[v = e[i].to], dis[u] + e[i].cst)) {
inid[v] = i;
fl[v] = min(e[i].cap, fl[u]);
if (!inq[v]) {inq[v] = true; q.push(v);}
}
}
return dis[T] < cstInf;
}
pair<int, int> EK() {
int flow = 0;
int cost = 0;
while (spfa()) {
flow += fl[T];
cost += dis[T] * fl[T];
for (int u = T; u != S; u = e[inid[u] ^ 1].to)
e[inid[u]].cap -= fl[T], e[inid[u] ^ 1].cap += fl[T];
}
return make_pair(flow, cost);
}
int n, m;
inline void work() {
clear();
S = 0; T = 2 * n - 1;
add(S, 2, 2, 0);
rep(i, 1, n) add(i * 2 - 1, i * 2, 1, 0);
rep(i, 1, m) {
int u, v, w;
cin >> u >> v >> w;
add(u * 2, v * 2 - 1, 1, w);
}
auto [flow, cost] = EK();
printf("%d\n", cost);
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
while (cin >> n >> m) work();
return 0;
}
詳細信息
Pretests
Final Tests
Test #1:
score: 100
Accepted
time: 5ms
memory: 4128kb
input:
6 11 1 2 23 1 3 12 1 4 99 2 5 17 2 6 73 3 5 3 3 6 21 4 6 8 5 2 33 5 4 5 6 5 20 6 14 1 2 1 2 1 1 1 3 2 3 1 2 3 4 2 4 3 2 2 4 1 4 2 1 2 5 2 5 2 2 4 6 1 6 4 1 5 6 2 6 5 2 3 3 1 3 1 1 2 5 2 3 5 4 4 1 2 10 1 3 15 2 4 16 3 4 17 6 8 1 2 10 1 3 15 2 4 13 2 5 30 3 4 32 3 5 16 4 6 16 5 6 17 8 10 1 2 10 1 3 15...
output:
86 10 11 58 87 247 352 298 392 201 323 283 271 265 275 234 210 985 1484 8058 14510 39235
result:
ok 22 lines