From b82a6c611dae916d1a3990e2bdc0cee90e865e04 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Tue, 22 Mar 2022 16:31:19 +0800 Subject: [PATCH] =?UTF-8?q?#102.=20=E6=9C=80=E5=B0=8F=E8=B4=B9=E7=94=A8?= =?UTF-8?q?=E6=B5=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://loj.ac/s/1419683 --- LibreOJ/102/102.cpp | 109 ++++++++++++++++++++++++++++++++++++++++ LibreOJ/102/data/1.in | 3 ++ LibreOJ/102/data/1.out | 3 ++ LibreOJ/102/data/10.in | 3 ++ LibreOJ/102/data/10.out | 3 ++ LibreOJ/102/data/2.in | 3 ++ LibreOJ/102/data/2.out | 3 ++ LibreOJ/102/data/3.in | 3 ++ LibreOJ/102/data/3.out | 3 ++ LibreOJ/102/data/4.in | 3 ++ LibreOJ/102/data/4.out | 3 ++ LibreOJ/102/data/5.in | 3 ++ LibreOJ/102/data/5.out | 3 ++ LibreOJ/102/data/6.in | 3 ++ LibreOJ/102/data/6.out | 3 ++ LibreOJ/102/data/7.in | 3 ++ LibreOJ/102/data/7.out | 3 ++ LibreOJ/102/data/8.in | 3 ++ LibreOJ/102/data/8.out | 3 ++ LibreOJ/102/data/9.in | 3 ++ LibreOJ/102/data/9.out | 3 ++ 21 files changed, 169 insertions(+) create mode 100644 LibreOJ/102/102.cpp create mode 100644 LibreOJ/102/data/1.in create mode 100644 LibreOJ/102/data/1.out create mode 100644 LibreOJ/102/data/10.in create mode 100644 LibreOJ/102/data/10.out create mode 100644 LibreOJ/102/data/2.in create mode 100644 LibreOJ/102/data/2.out create mode 100644 LibreOJ/102/data/3.in create mode 100644 LibreOJ/102/data/3.out create mode 100644 LibreOJ/102/data/4.in create mode 100644 LibreOJ/102/data/4.out create mode 100644 LibreOJ/102/data/5.in create mode 100644 LibreOJ/102/data/5.out create mode 100644 LibreOJ/102/data/6.in create mode 100644 LibreOJ/102/data/6.out create mode 100644 LibreOJ/102/data/7.in create mode 100644 LibreOJ/102/data/7.out create mode 100644 LibreOJ/102/data/8.in create mode 100644 LibreOJ/102/data/8.out create mode 100644 LibreOJ/102/data/9.in create mode 100644 LibreOJ/102/data/9.out diff --git a/LibreOJ/102/102.cpp b/LibreOJ/102/102.cpp new file mode 100644 index 00000000..200f0e47 --- /dev/null +++ b/LibreOJ/102/102.cpp @@ -0,0 +1,109 @@ +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 405, + M = 15005, + INF = 0x3f3f3f3f; + +int n, m, s, t, flow, cost; + +// Graph +int idx, head[N], ver[M << 1], next[M << 1]; +std::pair edge[M << 1]; +// + +void add(int u, int v, int c, int w) { + next[idx] = head[u]; + ver[idx] = v; + edge[idx] = std::make_pair(c, w); + head[u] = idx++; +} + +// Dinic +int dist[N]; +bool vis[N]; + +bool spfa() { + memset(vis, 0x00, sizeof(vis)); + memset(dist, 0x3f, sizeof(dist)); + + std::queue q; + q.push(s); + dist[s] = 0; + vis[s] = true; + + while (!q.empty()) { + int u = q.front(); + q.pop(); + vis[u] = false; + + for (int i = head[u]; ~i; i = next[i]) { + int v = ver[i], + c = edge[i].first, + w = edge[i].second; + + if (c > 0 && dist[v] > dist[u] + w) { + dist[v] = dist[u] + w; + if (!vis[v]) { + q.push(v); + vis[v] = true; + } + } + } + } + + return dist[t] != INF; +} + +int dinic(int u, int limit) { + if (u == t) return limit; + + int flow = 0; + vis[u] = true; + for (int i = head[u]; ~i && flow < limit; i = next[i]) { + int v = ver[i], + c = edge[i].first, + w = edge[i].second; + + if (dist[v] == dist[u] + w && c && !vis[v]) { + int k = dinic(v, std::min(c, limit - flow)); + if (!k) dist[v] = INF; + edge[i].first -= k; + edge[i ^ 1].first += k; + flow += k; + cost += k * w; + } + } + vis[u] = false; + + return flow; +} + +int main() { + std::ios::sync_with_stdio(false); + + memset(head, 0xff, sizeof(head)); + + cin >> n >> m; + + s = 1, t = n; + for (int i = 1; i <= m; i++) { + int u, v, c, w; + cin >> u >> v >> c >> w; + add(u, v, c, w); + add(v, u, 0, -w); + } + + while (spfa()) { + flow += dinic(s, INF); + } + cout << flow << ' ' << cost << endl; + + return 0; +} diff --git a/LibreOJ/102/data/1.in b/LibreOJ/102/data/1.in new file mode 100644 index 00000000..02e59aab --- /dev/null +++ b/LibreOJ/102/data/1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b80bdda93e796749684e4baa31e0490c611b5950f855bc452d1020c4a6a511f8 +size 352 diff --git a/LibreOJ/102/data/1.out b/LibreOJ/102/data/1.out new file mode 100644 index 00000000..29892e30 --- /dev/null +++ b/LibreOJ/102/data/1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cdcb06834457bfe2af860487d0a56ee5ae80ca7e3b6419b71ac88e1b0cc47585 +size 6 diff --git a/LibreOJ/102/data/10.in b/LibreOJ/102/data/10.in new file mode 100644 index 00000000..d0ce3083 --- /dev/null +++ b/LibreOJ/102/data/10.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5cef1956fc48b8ac74c4931dc152056cbeed2b63c10aeaa50a7f4fc5f7866e67 +size 175292 diff --git a/LibreOJ/102/data/10.out b/LibreOJ/102/data/10.out new file mode 100644 index 00000000..271ca675 --- /dev/null +++ b/LibreOJ/102/data/10.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b80c45bdb87e9d00103f578e634abef7d33ea35fe41e5c18267e531954b6b48a +size 13 diff --git a/LibreOJ/102/data/2.in b/LibreOJ/102/data/2.in new file mode 100644 index 00000000..d0cf920a --- /dev/null +++ b/LibreOJ/102/data/2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:808440830915911c57ad4da3097bc9bf8e585955922543ddd6edea3a46ed414a +size 663 diff --git a/LibreOJ/102/data/2.out b/LibreOJ/102/data/2.out new file mode 100644 index 00000000..d6a3ff6c --- /dev/null +++ b/LibreOJ/102/data/2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3b3c7a8c44d15f4606cbd117181d1486e7c4ab07adf9303eaede2fb6b92d60db +size 7 diff --git a/LibreOJ/102/data/3.in b/LibreOJ/102/data/3.in new file mode 100644 index 00000000..6f732b75 --- /dev/null +++ b/LibreOJ/102/data/3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d122a67d3e8c9d66aaa5a1f86ede4eb5c4c22c3ad2ca2d80cb183b8c9ea5b7c5 +size 2418 diff --git a/LibreOJ/102/data/3.out b/LibreOJ/102/data/3.out new file mode 100644 index 00000000..2d415159 --- /dev/null +++ b/LibreOJ/102/data/3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:dbf7b362e424fd0640a2f28f6e8b1db10d730ce45ec42c2ff7b9518cc5d648f8 +size 8 diff --git a/LibreOJ/102/data/4.in b/LibreOJ/102/data/4.in new file mode 100644 index 00000000..a3baed0e --- /dev/null +++ b/LibreOJ/102/data/4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c7f38810fc940226f54cea09d485496e7a43191591a310dfdfaa7b04ac67ef6c +size 31640 diff --git a/LibreOJ/102/data/4.out b/LibreOJ/102/data/4.out new file mode 100644 index 00000000..f699cd14 --- /dev/null +++ b/LibreOJ/102/data/4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e7a8b9ba298859c46e20cbf3b3930c3142f93ed513ead5fe9ee32871ef8347c2 +size 11 diff --git a/LibreOJ/102/data/5.in b/LibreOJ/102/data/5.in new file mode 100644 index 00000000..52b9ca28 --- /dev/null +++ b/LibreOJ/102/data/5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:baa7189b7eb64eb08b0d930ca511af0ee202e08bde489b943a9a3d472f076796 +size 62258 diff --git a/LibreOJ/102/data/5.out b/LibreOJ/102/data/5.out new file mode 100644 index 00000000..8f51095f --- /dev/null +++ b/LibreOJ/102/data/5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:80dda12fbebd93e5e797cb4fc0c8f0001f188f185144fb8f2afb86d070b9983d +size 12 diff --git a/LibreOJ/102/data/6.in b/LibreOJ/102/data/6.in new file mode 100644 index 00000000..fb09b2f1 --- /dev/null +++ b/LibreOJ/102/data/6.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8378e8b6f0ce54b10c384b7e31060ea49b75b4db90db3a08257839481ff76826 +size 62308 diff --git a/LibreOJ/102/data/6.out b/LibreOJ/102/data/6.out new file mode 100644 index 00000000..5c9df6e1 --- /dev/null +++ b/LibreOJ/102/data/6.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a1fd867b691d58c3d3a97a1e6ecccc91634b8b617d629cb39745814509b08e46 +size 12 diff --git a/LibreOJ/102/data/7.in b/LibreOJ/102/data/7.in new file mode 100644 index 00000000..a5bd1c97 --- /dev/null +++ b/LibreOJ/102/data/7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7ce80b3fa63ce96e049dc032e7b3bb93c939042e6a97f7f6fc23e1e0dbfb7d42 +size 62304 diff --git a/LibreOJ/102/data/7.out b/LibreOJ/102/data/7.out new file mode 100644 index 00000000..02f923d0 --- /dev/null +++ b/LibreOJ/102/data/7.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e24f57ea6da66c3730ed948b08eb5085b015e9bf805f9b6439f534543e293cf0 +size 12 diff --git a/LibreOJ/102/data/8.in b/LibreOJ/102/data/8.in new file mode 100644 index 00000000..e29e9725 --- /dev/null +++ b/LibreOJ/102/data/8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bbbe1107bb71cd729c51be70e7e0aef1ed60f5d0fbd68909dbd4ed65f1ca8dec +size 173595 diff --git a/LibreOJ/102/data/8.out b/LibreOJ/102/data/8.out new file mode 100644 index 00000000..cba22122 --- /dev/null +++ b/LibreOJ/102/data/8.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:74ab11db590daa7b2f9310acb0452eb9168dd2c3158cd167e224b31e813972a0 +size 13 diff --git a/LibreOJ/102/data/9.in b/LibreOJ/102/data/9.in new file mode 100644 index 00000000..0a4a6373 --- /dev/null +++ b/LibreOJ/102/data/9.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:549f63c1b8c1bb3be7b788f8ef95cab763bcdd3e251bb9c298613e5d2d7262a5 +size 175340 diff --git a/LibreOJ/102/data/9.out b/LibreOJ/102/data/9.out new file mode 100644 index 00000000..e61953ce --- /dev/null +++ b/LibreOJ/102/data/9.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b7644e25d8bdfa7f9a4f1afe704858eee28ff4b502d384dd81dac2c02e314ee8 +size 13