Submission #26084176
Source Code Expand
#include <bits/stdc++.h>
const int N = 605, M = N * 10;
int n, m, a[N], b[N], h[N], cur[N], lev[N], ans;
struct edge {
int v, nxt, w;
} e[M];
int cnt = -1, S, T;
void add(int u, int v, int w) {
e[++cnt] = (edge){v, h[u], w}; h[u] = cnt;
e[++cnt] = (edge){u, h[v], 0}; h[v] = cnt;
}
int que[N], hd, tl;
bool bfs() {
memset(lev, 0, sizeof(lev));
hd = tl = lev[S] = 1; que[1] = S;
while(hd <= tl) {
int u = que[hd++];
for(int i = h[u]; ~i; i = e[i].nxt) {
int v = e[i].v;
if(!lev[v] && e[i].w > 0) {
que[++tl] = v;
lev[v] = lev[u] + 1;
}
}
}
return lev[T];
}
int dfs(int u, int can_flow) {
if(u == T) return can_flow;
int res_flow = 0;
for(int &i = cur[u]; ~i; i = e[i].nxt) {
int v = e[i].v;
if(lev[v] == lev[u] + 1 && e[i].w > 0) {
int will_flow = dfs(v, std::min(can_flow, e[i].w));
res_flow += will_flow;
can_flow -= will_flow;
e[i ^ 1].w += will_flow;
e[i].w -= will_flow;
if(!can_flow) break;
}
}
if(!res_flow) lev[u] = 0;
return res_flow;
}
int dinic() {
int res = 0;
while(bfs()) {
memcpy(cur, h, sizeof(h));
res += dfs(S, INT_MAX);
}
return res;
}
int main() {
memset(h, -1, sizeof(h));
scanf("%d %d", &n, &m);
S = n * 2 + 1; T = n * 2 + 2;
for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
for(int i = 1; i <= n; i++) scanf("%d", &b[i]);
for(int i = 1; i <= m; i++) {
int u, v; scanf("%d %d", &u, &v);
add(u + n, v, INT_MAX); add(v + n, u, INT_MAX);
}
for(int i = 1; i <= n; i++) add(i, i + n, a[i] + abs(b[i]));
for(int i = 1; i <= n; i++) {
if(b[i] >= 0) {
add(i + n, T, 2 * b[i]);
ans += b[i];
} else {
add(S, i, -2 * b[i]);
ans -= b[i];
}
}
printf("%d\n", ans - dinic());
return 0;
}
Submission Info
Submission Time |
|
Task |
F - Sum of Abs |
User |
syksykCCC |
Language |
C++ (GCC 9.2.1) |
Score |
900 |
Code Size |
1786 Byte |
Status |
AC |
Exec Time |
7 ms |
Memory |
3836 KiB |
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:62:7: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
62 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
./Main.cpp:64:35: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
64 | for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
| ~~~~~^~~~~~~~~~~~~
./Main.cpp:65:35: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
65 | for(int i = 1; i <= n; i++) scanf("%d", &b[i]);
| ~~~~~^~~~~~~~~~~~~
./Main.cpp:67:18: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
67 | int u, v; scanf("%d %d", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~~
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
900 / 900 |
Status |
|
|
Set Name |
Test Cases |
Sample |
00-sample-001.txt, 00-sample-002.txt, 00-sample-003.txt |
All |
00-sample-001.txt, 00-sample-002.txt, 00-sample-003.txt, 01-001.txt, 01-002.txt, 01-003.txt, 01-004.txt, 01-005.txt, 01-006.txt, 01-007.txt, 01-008.txt, 01-009.txt, 01-010.txt, 01-011.txt, 01-012.txt, 01-013.txt, 01-014.txt, 01-015.txt, 01-016.txt, 01-017.txt, 01-018.txt, 01-019.txt, 01-020.txt, 01-021.txt, 01-022.txt, 01-023.txt, 01-024.txt, 01-025.txt, 01-026.txt, 01-027.txt, 01-028.txt, 01-029.txt, 01-030.txt, 01-031.txt, 01-032.txt, 01-033.txt, 01-034.txt, 01-035.txt, 01-036.txt, 01-037.txt, 01-038.txt, 01-039.txt, 01-040.txt, 01-041.txt, 01-042.txt, 01-043.txt, 01-044.txt, 01-045.txt, 01-046.txt, 01-047.txt, 01-048.txt, 01-049.txt, 01-050.txt, 01-051.txt |
Case Name |
Status |
Exec Time |
Memory |
00-sample-001.txt |
AC |
6 ms |
3744 KiB |
00-sample-002.txt |
AC |
2 ms |
3584 KiB |
00-sample-003.txt |
AC |
2 ms |
3748 KiB |
01-001.txt |
AC |
2 ms |
3736 KiB |
01-002.txt |
AC |
2 ms |
3584 KiB |
01-003.txt |
AC |
2 ms |
3700 KiB |
01-004.txt |
AC |
2 ms |
3716 KiB |
01-005.txt |
AC |
1 ms |
3716 KiB |
01-006.txt |
AC |
1 ms |
3560 KiB |
01-007.txt |
AC |
2 ms |
3708 KiB |
01-008.txt |
AC |
2 ms |
3760 KiB |
01-009.txt |
AC |
2 ms |
3708 KiB |
01-010.txt |
AC |
2 ms |
3596 KiB |
01-011.txt |
AC |
1 ms |
3708 KiB |
01-012.txt |
AC |
2 ms |
3572 KiB |
01-013.txt |
AC |
2 ms |
3580 KiB |
01-014.txt |
AC |
2 ms |
3576 KiB |
01-015.txt |
AC |
2 ms |
3744 KiB |
01-016.txt |
AC |
2 ms |
3728 KiB |
01-017.txt |
AC |
2 ms |
3724 KiB |
01-018.txt |
AC |
3 ms |
3604 KiB |
01-019.txt |
AC |
2 ms |
3764 KiB |
01-020.txt |
AC |
2 ms |
3576 KiB |
01-021.txt |
AC |
3 ms |
3600 KiB |
01-022.txt |
AC |
3 ms |
3756 KiB |
01-023.txt |
AC |
2 ms |
3604 KiB |
01-024.txt |
AC |
2 ms |
3612 KiB |
01-025.txt |
AC |
2 ms |
3832 KiB |
01-026.txt |
AC |
2 ms |
3576 KiB |
01-027.txt |
AC |
2 ms |
3768 KiB |
01-028.txt |
AC |
3 ms |
3720 KiB |
01-029.txt |
AC |
2 ms |
3600 KiB |
01-030.txt |
AC |
2 ms |
3576 KiB |
01-031.txt |
AC |
2 ms |
3604 KiB |
01-032.txt |
AC |
2 ms |
3656 KiB |
01-033.txt |
AC |
2 ms |
3836 KiB |
01-034.txt |
AC |
2 ms |
3748 KiB |
01-035.txt |
AC |
2 ms |
3712 KiB |
01-036.txt |
AC |
2 ms |
3608 KiB |
01-037.txt |
AC |
2 ms |
3724 KiB |
01-038.txt |
AC |
2 ms |
3716 KiB |
01-039.txt |
AC |
2 ms |
3576 KiB |
01-040.txt |
AC |
2 ms |
3580 KiB |
01-041.txt |
AC |
7 ms |
3728 KiB |
01-042.txt |
AC |
4 ms |
3756 KiB |
01-043.txt |
AC |
2 ms |
3580 KiB |
01-044.txt |
AC |
2 ms |
3776 KiB |
01-045.txt |
AC |
2 ms |
3612 KiB |
01-046.txt |
AC |
4 ms |
3736 KiB |
01-047.txt |
AC |
2 ms |
3616 KiB |
01-048.txt |
AC |
2 ms |
3664 KiB |
01-049.txt |
AC |
2 ms |
3724 KiB |
01-050.txt |
AC |
2 ms |
3612 KiB |
01-051.txt |
AC |
3 ms |
3728 KiB |