Submission #63828885
Source Code Expand
// Problem: G - Maximize Distance
// Contest: AtCoder - OMRON Corporation Programming Contest 2025 (AtCoder Beginner Contest 397)
// URL: https://atcoder.jp/contests/abc397/tasks/abc397_g
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define uint unsigned
#define mems(a, x) memset((a), (x), sizeof(a))
using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<int, int> pii;
const int maxn = 10050;
const int inf = 0x3f3f3f3f;
int n, m, K, hd[maxn], len, id[maxn];
pii a[maxn];
bool vis[maxn], mk[maxn];
struct edge {
int to, cap, flow, next;
} edges[maxn];
inline void add_edge(int u, int v, int c, int f) {
edges[++len].to = v;
edges[len].next = hd[u];
edges[len].cap = c;
edges[len].flow = f;
hd[u] = len;
}
struct Dinic {
int d[maxn], cur[maxn];
bool vis[maxn];
inline void add(int u, int v, int c) {
add_edge(u, v, c, 0);
add_edge(v, u, 0, 0);
}
inline bool bfs() {
queue<int> q;
for (int i = 1; i <= n; ++i) {
vis[i] = 0;
d[i] = -1;
}
d[1] = 0;
vis[1] = 1;
q.push(1);
while (q.size()) {
int u = q.front();
q.pop();
for (int i = hd[u]; i; i = edges[i].next) {
edge &e = edges[i];
if (!vis[e.to] && e.cap > e.flow) {
vis[e.to] = 1;
d[e.to] = d[u] + 1;
q.push(e.to);
}
}
}
return vis[n];
}
int dfs(int u, int a) {
if (u == n || !a) {
return a;
}
int flow = 0, f;
for (int &i = cur[u]; i; i = edges[i].next) {
edge &e = edges[i];
if (d[e.to] == d[u] + 1 && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0) {
e.flow += f;
edges[i ^ 1].flow -= f;
flow += f;
a -= f;
if (!a) {
break;
}
}
}
return flow;
}
inline int solve() {
int flow = 0;
while (bfs()) {
for (int i = 1; i <= n; ++i) {
cur[i] = hd[i];
}
flow += dfs(1, inf);
}
return flow;
}
} solver;
void dfs(int u) {
mk[u] = 1;
for (int i = hd[u]; i; i = edges[i].next) {
edge &e = edges[i];
if (!mk[e.to] && e.cap > e.flow) {
dfs(e.to);
}
}
}
void solve() {
scanf("%d%d%d", &n, &m, &K);
for (int i = 1; i <= m; ++i) {
scanf("%d%d", &a[i].fst, &a[i].scd);
}
int ans = 0;
while (1) {
mems(hd, 0);
len = 1;
for (int i = 1; i <= m; ++i) {
solver.add(a[i].fst, a[i].scd, vis[i] ? 1e4 : 1);
id[i] = len - 1;
}
int t = solver.solve();
if (t > K) {
break;
}
K -= t;
++ans;
mems(mk, 0);
dfs(1);
for (int i = 1; i <= m; ++i) {
if (mk[a[i].fst] && !mk[a[i].scd] && edges[id[i]].cap == edges[id[i]].flow) {
vis[i] = 1;
}
}
}
printf("%d\n", ans);
}
int main() {
int T = 1;
// scanf("%d", &T);
while (T--) {
solve();
}
return 0;
}
Submission Info
| Submission Time |
|
| Task |
G - Maximize Distance |
| User |
zlt |
| Language |
C++ 20 (gcc 12.2) |
| Score |
0 |
| Code Size |
3006 Byte |
| Status |
WA |
| Exec Time |
2 ms |
| Memory |
4004 KiB |
Compile Error
Main.cpp: In function ‘void solve()’:
Main.cpp:119:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
119 | scanf("%d%d%d", &n, &m, &K);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:121:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
121 | scanf("%d%d", &a[i].fst, &a[i].scd);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Judge Result
| Set Name |
Sample |
All |
| Score / Max Score |
0 / 0 |
0 / 625 |
| Status |
|
|
| Set Name |
Test Cases |
| Sample |
00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt |
| All |
00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 02_handmade_01.txt, 02_handmade_02.txt, 02_handmade_03.txt, 02_handmade_04.txt |
| Case Name |
Status |
Exec Time |
Memory |
| 00_sample_01.txt |
AC |
1 ms |
3876 KiB |
| 00_sample_02.txt |
AC |
1 ms |
3772 KiB |
| 00_sample_03.txt |
AC |
1 ms |
3724 KiB |
| 01_random_01.txt |
AC |
1 ms |
3876 KiB |
| 01_random_02.txt |
AC |
1 ms |
3872 KiB |
| 01_random_03.txt |
AC |
1 ms |
3792 KiB |
| 01_random_04.txt |
AC |
1 ms |
3884 KiB |
| 01_random_05.txt |
AC |
1 ms |
3816 KiB |
| 01_random_06.txt |
AC |
1 ms |
3984 KiB |
| 01_random_07.txt |
AC |
1 ms |
3940 KiB |
| 01_random_08.txt |
AC |
1 ms |
3772 KiB |
| 01_random_09.txt |
AC |
1 ms |
3736 KiB |
| 01_random_10.txt |
AC |
1 ms |
3856 KiB |
| 01_random_11.txt |
AC |
1 ms |
4004 KiB |
| 01_random_12.txt |
AC |
1 ms |
3748 KiB |
| 01_random_13.txt |
AC |
1 ms |
3872 KiB |
| 01_random_14.txt |
AC |
1 ms |
3792 KiB |
| 01_random_15.txt |
AC |
1 ms |
3796 KiB |
| 01_random_16.txt |
AC |
1 ms |
3928 KiB |
| 01_random_17.txt |
AC |
1 ms |
3872 KiB |
| 01_random_18.txt |
AC |
1 ms |
3788 KiB |
| 01_random_19.txt |
AC |
1 ms |
3944 KiB |
| 01_random_20.txt |
AC |
1 ms |
3748 KiB |
| 01_random_21.txt |
AC |
1 ms |
3860 KiB |
| 01_random_22.txt |
AC |
1 ms |
3880 KiB |
| 01_random_23.txt |
AC |
1 ms |
3860 KiB |
| 01_random_24.txt |
AC |
1 ms |
3868 KiB |
| 01_random_25.txt |
AC |
1 ms |
3868 KiB |
| 01_random_26.txt |
AC |
1 ms |
3772 KiB |
| 01_random_27.txt |
AC |
1 ms |
3796 KiB |
| 01_random_28.txt |
AC |
1 ms |
3732 KiB |
| 01_random_29.txt |
WA |
2 ms |
3740 KiB |
| 01_random_30.txt |
AC |
1 ms |
3788 KiB |
| 01_random_31.txt |
AC |
1 ms |
3768 KiB |
| 01_random_32.txt |
AC |
1 ms |
3792 KiB |
| 01_random_33.txt |
AC |
1 ms |
3928 KiB |
| 01_random_34.txt |
AC |
1 ms |
3656 KiB |
| 01_random_35.txt |
AC |
1 ms |
3740 KiB |
| 01_random_36.txt |
AC |
1 ms |
3864 KiB |
| 01_random_37.txt |
AC |
1 ms |
3748 KiB |
| 01_random_38.txt |
AC |
1 ms |
3740 KiB |
| 01_random_39.txt |
AC |
1 ms |
3872 KiB |
| 01_random_40.txt |
AC |
1 ms |
3792 KiB |
| 02_handmade_01.txt |
AC |
1 ms |
3740 KiB |
| 02_handmade_02.txt |
AC |
1 ms |
3940 KiB |
| 02_handmade_03.txt |
AC |
1 ms |
3848 KiB |
| 02_handmade_04.txt |
AC |
1 ms |
3656 KiB |