QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#423520 | #2208. Flow | jijidawang | AC ✓ | 343ms | 5820kb | C++14 | 3.1kb | 2024-05-28 07:37:03 | 2024-05-28 07:37:03 |
Judging History
answer
#include <bits/stdc++.h>
namespace // my guiding star
{
#define filein(x) freopen(x".in", "r", stdin);
#define fileout(x) freopen(x, "w", stdout);
#define fileerr(x) freopen(x, "w", stderr);
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout);
#define files(x) freopen(x".in", "r", stdin), freopen(x".ans", "w", stdout);
using namespace std;
#define cT const T&
template<typename T>
inline T chkmin(T& x, cT y){if (x > y) x = y; return x;}
template<typename T>
inline T chkmax(T& x, cT y){if (x < y) x = y; return x;}
template <typename T>
inline bool inrange(cT x, cT l, cT r){return (l <= x) && (x <= r);}
template <typename T>
inline bool inrange(cT l, cT r, cT L, cT R){return (L <= l) && (r <= R);}
#undef cT
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef unsigned u32;
template <typename T>
using pr = pair<T, T>;
typedef pr<int> pii;
typedef pr<ll> pll;
typedef pr<db> pdd;
typedef complex<double> cp;
typedef vector<int> vi;
inline void YN(bool x){puts(x ? "Yes" : "No");}
}
const int N = 55555, INF = 0x3f3f3f3f;
struct dinic
{
struct Node
{
int u, v, w, c;
Node() = default;
Node(int u, int v, int w, int c) : u(u), v(v), w(w), c(c){}
};
vector<Node> e;
vector<int> g[N];
inline void clear(int n){e.clear(); do g[n].clear(); while (n--);}
inline void addedge(int u, int v, int w, int c)
{
int s = e.size();
e.emplace_back(Node(u, v, w, c)); g[u].emplace_back(s++);
e.emplace_back(Node(v, u, 0, -c)); g[v].emplace_back(s++);
}
bool vis[N];
int dist[N];
inline bool spfa(int s, int t)
{
memset(dist, 0x3f, sizeof dist);
queue<int> q; dist[s] = 0; vis[s] = true; q.push(s);
while (!q.empty())
{
int u = q.front(); q.pop(); vis[u] = false;
for (int f : g[u])
{
int v = e[f].v, w = e[f].w, c = e[f].c;
if (w && (dist[u] + c < dist[v]))
{
dist[v] = dist[u] + c;
if (!vis[v]){vis[v] = true; q.push(v);}
}
}
}
return dist[t] != INF;
}
int dfs(int u, int t, int flow, int& cost)
{
if (u == t) return flow;
vis[u] = true;
int rest = flow;
for (int f : g[u])
{
int v = e[f].v, w = e[f].w, c = e[f].c;
if (!vis[v] && w && (dist[v] == dist[u] + c))
{
int k = dfs(v, t, min(rest, w), cost);
if (!k) dist[v] = -1;
e[f].w -= k; e[f^1].w += k; rest -= k; cost += c * k;
}
if (!rest) break;
}
vis[u] = false;
return flow - rest;
}
inline pii flow(int s, int t)
{
int flow, maxflow = 0, mincost = 0;
while (spfa(s, t))
while (flow = dfs(s, t, INF, mincost)) maxflow += flow;
return make_pair(maxflow, mincost);
}
}F;
int n, m, sum[N];
int main()
{
scanf("%d%d", &n, &m);
int ans = 0, s = 0, t = n + 1;
for (int i=0, u, v, w; i<m; i++)
{
scanf("%d%d%d", &u, &v, &w);
F.addedge(v, u, w, 1); sum[v] += w; sum[u] -= w; ans += w;
}
for (int i=1; i<=n; i++)
{
if (sum[i] > 0) F.addedge(s, i, sum[i], 0);
else F.addedge(i, t, -sum[i], 0);
}
printf("%d\n", ans - F.flow(s, t).second);
return 0;
}
Details
Test #1:
score: 100
Accepted
time: 238ms
memory: 5772kb
Test #2:
score: 0
Accepted
time: 136ms
memory: 5500kb
Test #3:
score: 0
Accepted
time: 153ms
memory: 5584kb
Test #4:
score: 0
Accepted
time: 167ms
memory: 5816kb
Test #5:
score: 0
Accepted
time: 214ms
memory: 5528kb
Test #6:
score: 0
Accepted
time: 135ms
memory: 5524kb
Test #7:
score: 0
Accepted
time: 67ms
memory: 5524kb
Test #8:
score: 0
Accepted
time: 175ms
memory: 5512kb
Test #9:
score: 0
Accepted
time: 163ms
memory: 5488kb
Test #10:
score: 0
Accepted
time: 123ms
memory: 5468kb
Test #11:
score: 0
Accepted
time: 1ms
memory: 5732kb
Test #12:
score: 0
Accepted
time: 155ms
memory: 5492kb
Test #13:
score: 0
Accepted
time: 93ms
memory: 5504kb
Test #14:
score: 0
Accepted
time: 103ms
memory: 5504kb
Test #15:
score: 0
Accepted
time: 93ms
memory: 5544kb
Test #16:
score: 0
Accepted
time: 180ms
memory: 5508kb
Test #17:
score: 0
Accepted
time: 298ms
memory: 5808kb
Test #18:
score: 0
Accepted
time: 72ms
memory: 5488kb
Test #19:
score: 0
Accepted
time: 68ms
memory: 5524kb
Test #20:
score: 0
Accepted
time: 4ms
memory: 5464kb
Test #21:
score: 0
Accepted
time: 144ms
memory: 5804kb
Test #22:
score: 0
Accepted
time: 341ms
memory: 5524kb
Test #23:
score: 0
Accepted
time: 150ms
memory: 5532kb
Test #24:
score: 0
Accepted
time: 137ms
memory: 5524kb
Test #25:
score: 0
Accepted
time: 77ms
memory: 5508kb
Test #26:
score: 0
Accepted
time: 152ms
memory: 5484kb
Test #27:
score: 0
Accepted
time: 110ms
memory: 5464kb
Test #28:
score: 0
Accepted
time: 197ms
memory: 5496kb
Test #29:
score: 0
Accepted
time: 142ms
memory: 5508kb
Test #30:
score: 0
Accepted
time: 86ms
memory: 5520kb
Test #31:
score: 0
Accepted
time: 213ms
memory: 5516kb
Test #32:
score: 0
Accepted
time: 133ms
memory: 5440kb
Test #33:
score: 0
Accepted
time: 144ms
memory: 5816kb
Test #34:
score: 0
Accepted
time: 61ms
memory: 5504kb
Test #35:
score: 0
Accepted
time: 175ms
memory: 5500kb
Test #36:
score: 0
Accepted
time: 67ms
memory: 5520kb
Test #37:
score: 0
Accepted
time: 56ms
memory: 5516kb
Test #38:
score: 0
Accepted
time: 109ms
memory: 5524kb
Test #39:
score: 0
Accepted
time: 143ms
memory: 5516kb
Test #40:
score: 0
Accepted
time: 110ms
memory: 5476kb
Test #41:
score: 0
Accepted
time: 158ms
memory: 5484kb
Test #42:
score: 0
Accepted
time: 246ms
memory: 5504kb
Test #43:
score: 0
Accepted
time: 192ms
memory: 5552kb
Test #44:
score: 0
Accepted
time: 114ms
memory: 5512kb
Test #45:
score: 0
Accepted
time: 87ms
memory: 5512kb
Test #46:
score: 0
Accepted
time: 133ms
memory: 5776kb
Test #47:
score: 0
Accepted
time: 229ms
memory: 5588kb
Test #48:
score: 0
Accepted
time: 103ms
memory: 5528kb
Test #49:
score: 0
Accepted
time: 163ms
memory: 5480kb
Test #50:
score: 0
Accepted
time: 163ms
memory: 5816kb
Test #51:
score: 0
Accepted
time: 181ms
memory: 5804kb
Test #52:
score: 0
Accepted
time: 125ms
memory: 5452kb
Test #53:
score: 0
Accepted
time: 60ms
memory: 5776kb
Test #54:
score: 0
Accepted
time: 85ms
memory: 5484kb
Test #55:
score: 0
Accepted
time: 14ms
memory: 5776kb
Test #56:
score: 0
Accepted
time: 247ms
memory: 5468kb
Test #57:
score: 0
Accepted
time: 175ms
memory: 5496kb
Test #58:
score: 0
Accepted
time: 108ms
memory: 5580kb
Test #59:
score: 0
Accepted
time: 117ms
memory: 5600kb
Test #60:
score: 0
Accepted
time: 61ms
memory: 5528kb
Test #61:
score: 0
Accepted
time: 85ms
memory: 5484kb
Test #62:
score: 0
Accepted
time: 148ms
memory: 5512kb
Test #63:
score: 0
Accepted
time: 186ms
memory: 5576kb
Test #64:
score: 0
Accepted
time: 74ms
memory: 5784kb
Test #65:
score: 0
Accepted
time: 168ms
memory: 5812kb
Test #66:
score: 0
Accepted
time: 95ms
memory: 5552kb
Test #67:
score: 0
Accepted
time: 111ms
memory: 5500kb
Test #68:
score: 0
Accepted
time: 82ms
memory: 5788kb
Test #69:
score: 0
Accepted
time: 156ms
memory: 5572kb
Test #70:
score: 0
Accepted
time: 204ms
memory: 5564kb
Test #71:
score: 0
Accepted
time: 8ms
memory: 5512kb
Test #72:
score: 0
Accepted
time: 224ms
memory: 5796kb
Test #73:
score: 0
Accepted
time: 151ms
memory: 5516kb
Test #74:
score: 0
Accepted
time: 139ms
memory: 5816kb
Test #75:
score: 0
Accepted
time: 233ms
memory: 5576kb
Test #76:
score: 0
Accepted
time: 198ms
memory: 5508kb
Test #77:
score: 0
Accepted
time: 131ms
memory: 5820kb
Test #78:
score: 0
Accepted
time: 65ms
memory: 5592kb
Test #79:
score: 0
Accepted
time: 100ms
memory: 5596kb
Test #80:
score: 0
Accepted
time: 149ms
memory: 5560kb
Test #81:
score: 0
Accepted
time: 186ms
memory: 5800kb
Test #82:
score: 0
Accepted
time: 81ms
memory: 5524kb
Test #83:
score: 0
Accepted
time: 94ms
memory: 5524kb
Test #84:
score: 0
Accepted
time: 205ms
memory: 5520kb
Test #85:
score: 0
Accepted
time: 93ms
memory: 5508kb
Test #86:
score: 0
Accepted
time: 87ms
memory: 5524kb
Test #87:
score: 0
Accepted
time: 5ms
memory: 5548kb
Test #88:
score: 0
Accepted
time: 190ms
memory: 5496kb
Test #89:
score: 0
Accepted
time: 194ms
memory: 5448kb
Test #90:
score: 0
Accepted
time: 97ms
memory: 5780kb
Test #91:
score: 0
Accepted
time: 163ms
memory: 5808kb
Test #92:
score: 0
Accepted
time: 11ms
memory: 5540kb
Test #93:
score: 0
Accepted
time: 156ms
memory: 5584kb
Test #94:
score: 0
Accepted
time: 106ms
memory: 5600kb
Test #95:
score: 0
Accepted
time: 190ms
memory: 5508kb
Test #96:
score: 0
Accepted
time: 74ms
memory: 5776kb
Test #97:
score: 0
Accepted
time: 167ms
memory: 5516kb
Test #98:
score: 0
Accepted
time: 90ms
memory: 5756kb
Test #99:
score: 0
Accepted
time: 23ms
memory: 5468kb
Test #100:
score: 0
Accepted
time: 84ms
memory: 5768kb
Test #101:
score: 0
Accepted
time: 76ms
memory: 5456kb
Test #102:
score: 0
Accepted
time: 76ms
memory: 5772kb
Test #103:
score: 0
Accepted
time: 121ms
memory: 5556kb
Test #104:
score: 0
Accepted
time: 75ms
memory: 5516kb
Test #105:
score: 0
Accepted
time: 146ms
memory: 5804kb
Test #106:
score: 0
Accepted
time: 135ms
memory: 5520kb
Test #107:
score: 0
Accepted
time: 93ms
memory: 5584kb
Test #108:
score: 0
Accepted
time: 192ms
memory: 5524kb
Test #109:
score: 0
Accepted
time: 186ms
memory: 5580kb
Test #110:
score: 0
Accepted
time: 161ms
memory: 5460kb
Test #111:
score: 0
Accepted
time: 207ms
memory: 5476kb
Test #112:
score: 0
Accepted
time: 193ms
memory: 5804kb
Test #113:
score: 0
Accepted
time: 153ms
memory: 5524kb
Test #114:
score: 0
Accepted
time: 124ms
memory: 5512kb
Test #115:
score: 0
Accepted
time: 177ms
memory: 5516kb
Test #116:
score: 0
Accepted
time: 79ms
memory: 5496kb
Test #117:
score: 0
Accepted
time: 175ms
memory: 5804kb
Test #118:
score: 0
Accepted
time: 112ms
memory: 5808kb
Test #119:
score: 0
Accepted
time: 128ms
memory: 5536kb
Test #120:
score: 0
Accepted
time: 111ms
memory: 5584kb
Test #121:
score: 0
Accepted
time: 73ms
memory: 5520kb
Test #122:
score: 0
Accepted
time: 112ms
memory: 5812kb
Test #123:
score: 0
Accepted
time: 249ms
memory: 5476kb
Test #124:
score: 0
Accepted
time: 68ms
memory: 5772kb
Test #125:
score: 0
Accepted
time: 60ms
memory: 5508kb
Test #126:
score: 0
Accepted
time: 256ms
memory: 5540kb
Test #127:
score: 0
Accepted
time: 5ms
memory: 5548kb
Test #128:
score: 0
Accepted
time: 78ms
memory: 5516kb
Test #129:
score: 0
Accepted
time: 209ms
memory: 5820kb
Test #130:
score: 0
Accepted
time: 63ms
memory: 5772kb
Test #131:
score: 0
Accepted
time: 326ms
memory: 5512kb
Test #132:
score: 0
Accepted
time: 163ms
memory: 5504kb
Test #133:
score: 0
Accepted
time: 62ms
memory: 5528kb
Test #134:
score: 0
Accepted
time: 87ms
memory: 5504kb
Test #135:
score: 0
Accepted
time: 85ms
memory: 5576kb
Test #136:
score: 0
Accepted
time: 195ms
memory: 5808kb
Test #137:
score: 0
Accepted
time: 172ms
memory: 5544kb
Test #138:
score: 0
Accepted
time: 202ms
memory: 5776kb
Test #139:
score: 0
Accepted
time: 146ms
memory: 5456kb
Test #140:
score: 0
Accepted
time: 86ms
memory: 5508kb
Test #141:
score: 0
Accepted
time: 71ms
memory: 5764kb
Test #142:
score: 0
Accepted
time: 124ms
memory: 5596kb
Test #143:
score: 0
Accepted
time: 83ms
memory: 5508kb
Test #144:
score: 0
Accepted
time: 87ms
memory: 5496kb
Test #145:
score: 0
Accepted
time: 258ms
memory: 5500kb
Test #146:
score: 0
Accepted
time: 145ms
memory: 5772kb
Test #147:
score: 0
Accepted
time: 178ms
memory: 5528kb
Test #148:
score: 0
Accepted
time: 139ms
memory: 5780kb
Test #149:
score: 0
Accepted
time: 95ms
memory: 5524kb
Test #150:
score: 0
Accepted
time: 148ms
memory: 5496kb
Test #151:
score: 0
Accepted
time: 123ms
memory: 5512kb
Test #152:
score: 0
Accepted
time: 343ms
memory: 5796kb
Test #153:
score: 0
Accepted
time: 86ms
memory: 5504kb
Test #154:
score: 0
Accepted
time: 128ms
memory: 5456kb
Test #155:
score: 0
Accepted
time: 170ms
memory: 5600kb
Test #156:
score: 0
Accepted
time: 70ms
memory: 5520kb
Test #157:
score: 0
Accepted
time: 125ms
memory: 5492kb
Test #158:
score: 0
Accepted
time: 140ms
memory: 5516kb
Test #159:
score: 0
Accepted
time: 57ms
memory: 5772kb
Test #160:
score: 0
Accepted
time: 220ms
memory: 5816kb
Test #161:
score: 0
Accepted
time: 236ms
memory: 5500kb
Test #162:
score: 0
Accepted
time: 145ms
memory: 5788kb
Test #163:
score: 0
Accepted
time: 99ms
memory: 5496kb
Test #164:
score: 0
Accepted
time: 210ms
memory: 5512kb
Test #165:
score: 0
Accepted
time: 162ms
memory: 5512kb
Test #166:
score: 0
Accepted
time: 136ms
memory: 5520kb
Test #167:
score: 0
Accepted
time: 84ms
memory: 5528kb
Test #168:
score: 0
Accepted
time: 84ms
memory: 5528kb
Test #169:
score: 0
Accepted
time: 79ms
memory: 5508kb
Test #170:
score: 0
Accepted
time: 227ms
memory: 5512kb
Test #171:
score: 0
Accepted
time: 104ms
memory: 5776kb
Test #172:
score: 0
Accepted
time: 146ms
memory: 5576kb
Test #173:
score: 0
Accepted
time: 83ms
memory: 5780kb
Test #174:
score: 0
Accepted
time: 233ms
memory: 5472kb
Test #175:
score: 0
Accepted
time: 259ms
memory: 5576kb
Test #176:
score: 0
Accepted
time: 108ms
memory: 5512kb
Test #177:
score: 0
Accepted
time: 126ms
memory: 5796kb
Test #178:
score: 0
Accepted
time: 213ms
memory: 5800kb
Test #179:
score: 0
Accepted
time: 167ms
memory: 5520kb
Test #180:
score: 0
Accepted
time: 76ms
memory: 5476kb
Test #181:
score: 0
Accepted
time: 174ms
memory: 5528kb
Test #182:
score: 0
Accepted
time: 241ms
memory: 5772kb
Test #183:
score: 0
Accepted
time: 207ms
memory: 5504kb
Test #184:
score: 0
Accepted
time: 82ms
memory: 5572kb
Test #185:
score: 0
Accepted
time: 74ms
memory: 5808kb
Test #186:
score: 0
Accepted
time: 73ms
memory: 5812kb