QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#423531 | #2208. Flow | KinNa | RE | 0ms | 0kb | C++14 | 1.7kb | 2024-05-28 08:21:22 | 2024-05-28 08:21:22 |
answer
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
typedef long long LL;
const int N = 2010;
struct Edge {int y, nxt; LL w, c;} e[N << 2]; int ly[N], ltp = 1;
void add(int x, int y, LL w, LL c) {e[++ltp] = {y, ly[x], w, c}; ly[x] = ltp;}
LL dis[N]; int cur[N]; bool inq[N];
bool spfa(int s, int t) {
memset(dis, 0x3f, sizeof dis);
memset(inq, 0, sizeof inq);
queue <int> q; q.push(s); dis[s] = 0;
while (!q.empty()) {
int x = q.front(); q.pop(); inq[x] = 0; cur[x] = ly[x];
for (int i = ly[x], y; y = e[i].y, i; i = e[i].nxt) {
if (e[i].w && dis[y] > dis[x] + e[i].c) {
dis[y] = dis[x] + e[i].c;
if (!inq[y]) q.push(y), inq[y] = 1;
}
}
}
return dis[t] != 0x3f3f3f3f3f3f3f3f;
}
LL sum, mxf, mnc;
LL dfs(int x, int t, LL In = 0x3f3f3f3f3f3f3f3f) {
if (x == t) return In; LL Out = 0;
inq[x] = 1;
for (int i = cur[x], y; y = e[i].y, i && In; i = e[i].nxt) {
cur[x] = i;
if (!inq[y] && e[i].w && dis[y] == dis[x] + e[i].c) {
LL res = dfs(y, t, min(In, e[i].w));
e[i].w -= res; e[i ^ 1].w += res;
In -= res; Out += res;
mnc += res * e[i].c;
}
}
inq[x] = 0;
if (!Out) dis[x] = 0x3f3f3f3f3f3f3f3f;
return Out;
}
int n, m, ss, tt;
LL delta[N];
int main() {
cin >> n >> m; ss = n + 1; tt = n + 2;
for (int i = 1, x, y, w; i <= m; ++i) {
cin >> x >> y >> w;
delta[x] -= w; delta[y] += w; mnc -= w;
add(x, y, 0, -1); add(y, x, w, 1);
}
for (int i = 1; i <= n; ++i) {
if (delta[i] > 0) add(ss, i, delta[i], 0), add(i, ss, 0, 0), sum += delta[i];
else if (delta[i] < 0) add(i, tt, -delta[i], 0), add(tt, i, 0, 0);
}
while (spfa(ss, tt))
mxf += dfs(ss, tt);
cout << -mnc - (sum - mxf) << endl;
return 0;
}
詳細信息
Test #1:
score: 0
Runtime Error