QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#468608 | #4165. 星际竞速 | little_sun | 100 ✓ | 1047ms | 21952kb | C++14 | 2.5kb | 2024-07-08 21:49:29 | 2024-07-08 21:49:30 |
Judging History
answer
#include <bits/stdc++.h>
#define R register
#define ll long long
#define sum(a, b, mod) (((a) + (b)) % mod)
#define meow(cat...) fprintf(stderr, cat)
const ll MaxN = 5e5 + 10;
const ll MaxM = 5e5 + 10;
const ll inf = 0x3f3f3f3f3f3f3f3f;
struct edge
{
ll next, to, flow, cost;
} e[MaxM << 1];
ll head[MaxN], dis[MaxN], cur[MaxN], vis[MaxN];
ll n, m, s, t, cnt = 1, mincost, maxflow, a[MaxN];
void add(ll u, ll v, ll f, ll c)
{
++cnt;
e[cnt].to = v;
e[cnt].flow = f;
e[cnt].cost = c;
e[cnt].next = head[u];
head[u] = cnt;
}
void add_edge(ll u, ll v, ll f, ll c) { add(u, v, f, c), add(v, u, 0, -c); }
inline ll read()
{
ll x = 0;
char ch = getchar();
while(ch > '9' || ch < '0')
ch = getchar();
while(ch <= '9' && ch >= '0')
x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
return x;
}
ll bfs(ll s)
{
std::queue<ll> q;
memset(dis, 0x3f, sizeof(dis));
memcpy(cur, head, sizeof(head));
dis[s] = 0, q.push(s), vis[s] = 1;
while(!q.empty())
{
ll u = q.front(); q.pop(), vis[u] = 0;
for(ll i = head[u]; i; i = e[i].next)
{
ll v = e[i].to, c = e[i].flow;
if(dis[v] > dis[u] + e[i].cost && c)
{
dis[v] = dis[u] + e[i].cost;
if(!vis[v]) vis[v] = 1, q.push(v);
}
}
}
return dis[t] != inf;
}
ll dinic(ll u, ll flow)
{
if(u == t)
{
maxflow += flow;
return flow;
}
ll rest = flow; vis[u] = 1;
for(ll i = cur[u]; i && rest; i = e[i].next)
{
ll v = e[i].to, c = e[i].flow, k;
if(!vis[v] && (dis[v] == dis[u] + e[i].cost) && c)
{
cur[u] = i, k = dinic(v, std::min(c, rest));
rest -= k, e[i].flow -= k, e[i ^ 1].flow += k;
mincost += k * e[i].cost;
}
}
vis[u] = 0;
return flow - rest;
}
void solve()
{
while(bfs(s))
{
memset(vis, 0, sizeof(vis));
dinic(s, inf);
}
}
signed main()
{
n = read(), m = read(), s = 2 * n + 1, t = 2 * n + 2;
for(int i = 1; i <= n; i++)
a[i] = read(), add_edge(s, i, 1, 0), add_edge(i + n, t, 1, 0);
for(int i = 1; i <= m; i++)
{
int u = read(), v = read(), d = read();
if(u > v) std::swap(u, v);
add_edge(u, v + n, inf, d);
}
for(int j = 1; j <= n; j++)
add_edge(s, j + n, inf, a[j]);
solve(), printf("%lld\n", mincost);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 10
Accepted
time: 2ms
memory: 19536kb
input:
4 5 539057 13724 518925 2598 4 1 10423 3 1 31021 3 4 7871 1 2 17315 2 4 3585
output:
586400
result:
ok single line: '586400'
Test #2:
score: 10
Accepted
time: 7ms
memory: 17440kb
input:
15 30 522387 29355 541794 521018 543985 542230 26389 523220 525853 535653 543915 18572 29378 525461 530719 12 9 3190 9 5 26126 4 8 1236 9 13 18451 15 10 25338 14 5 1575 1 5 31237 13 4 24538 15 1 765 11 4 9138 9 15 9881 1 13 11918 14 6 25910 6 2 22440 9 2 20730 6 1 7901 4 3 13426 10 5 5935 1 12 21191...
output:
1790859
result:
ok single line: '1790859'
Test #3:
score: 10
Accepted
time: 12ms
memory: 19028kb
input:
20 50 20426 12681 29314 4878 11984 14987 536269 532472 535906 525476 541019 526047 537789 521207 527194 14951 17826 20554 547772 537878 5 3 18553 19 5 19898 16 13 25686 17 20 19258 7 19 25324 15 14 11599 16 17 2116 8 13 18747 6 1 3787 18 11 22506 1 9 20431 8 20 703 15 10 13914 6 10 13672 8 1 30228 2...
output:
812779
result:
ok single line: '812779'
Test #4:
score: 10
Accepted
time: 108ms
memory: 18888kb
input:
200 1000 20813 546294 5674 525541 7701 1486 521494 5135 25492 546425 535568 534438 518163 15501 526948 21843 545151 519269 13123 16067 529899 534943 30015 27942 20747 524446 1745 19633 545979 7944 7379 545564 538998 542793 11270 20773 12768 543247 7054 517131 525629 529613 1905 7443 536823 537912 48...
output:
6277991
result:
ok single line: '6277991'
Test #5:
score: 10
Accepted
time: 117ms
memory: 18052kb
input:
200 2000 8659 520000 18451 13031 538496 539577 14299 542624 30074 21710 547648 15184 460 20710 2033 30738 19558 111 16063 521028 542620 30809 7201 531721 525734 534681 518319 712 12318 523943 11687 548523 10885 516609 543339 519457 24510 521894 16385 525661 2193 545948 543634 529109 523817 519786 54...
output:
4468321
result:
ok single line: '4468321'
Test #6:
score: 10
Accepted
time: 139ms
memory: 20060kb
input:
200 4000 520133 785 519519 16396 3400 8745 11920 523293 531896 4479 531231 15063 10685 539897 29203 541841 27746 521214 546076 526948 2712 534012 25353 544687 27313 996 524882 545686 21990 548009 523407 28542 519121 522693 8959 18834 540915 16051 538882 19778 546608 3702 525727 32190 31566 15633 181...
output:
2063842
result:
ok single line: '2063842'
Test #7:
score: 10
Accepted
time: 127ms
memory: 18560kb
input:
200 4000 30559 528162 31544 544939 31815 19278 17590 31115 22313 18789 543711 523798 544505 524289 537 533480 10031 26463 2192 11136 524617 19681 525746 8643 546118 908 545969 532088 4651 21387 26629 13770 525441 13226 530663 533705 725 29660 23511 521704 533303 546998 7447 19841 16828 241 13357 698...
output:
1524757
result:
ok single line: '1524757'
Test #8:
score: 10
Accepted
time: 681ms
memory: 19712kb
input:
600 12000 543932 12351 18423 541357 528639 4985 29393 6570 31434 539050 12221 22519 16196 544282 16211 4829 540691 532921 6637 538510 8420 545175 544553 530588 14057 361 12138 538240 538198 527115 13399 544276 22755 528120 25946 547865 32078 18388 31521 544691 9174 525539 536097 543976 10688 27279 1...
output:
5177446
result:
ok single line: '5177446'
Test #9:
score: 10
Accepted
time: 1000ms
memory: 18992kb
input:
800 14000 18996 24768 542822 11592 28975 13142 531386 4506 523343 13742 527235 6638 3244 5514 541848 545754 14207 532721 544053 3444 5796 520365 534394 21979 6175 519372 8938 547502 522388 530211 525293 16019 26829 543430 11086 543154 29240 535185 18109 12599 538727 532387 522574 19668 529550 518110...
output:
10707551
result:
ok single line: '10707551'
Test #10:
score: 10
Accepted
time: 1047ms
memory: 21952kb
input:
800 15000 549001 9281 27830 1808 6346 535277 12071 12667 18596 534113 521277 538268 8713 27161 547436 20611 527501 531802 522762 11589 527083 4009 521173 535674 547712 13649 4971 5567 548559 15074 1489 18485 4603 530860 538349 18177 523856 528712 17164 1226 526779 547856 533341 31038 518989 18595 54...
output:
10340820
result:
ok single line: '10340820'