QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#555726 | #7185. Poor Students | Yansuan_HCl | RE | 0ms | 0kb | C++20 | 2.9kb | 2024-09-10 08:43:00 | 2024-09-10 08:43:00 |
answer
#include <bits/stdc++.h>
#define ms(x, v) memset(x, v, sizeof(x))
#define il __attribute__((always_inline)) static
#define U(i,l,r) for(int i(l),END##i(r);i<=END##i;++i)
#define D(i,r,l) for(int i(r),END##i(l);i>=END##i;--i)
using namespace std;
using ll = long long;
#define IC isdigit(c)
#define GC c=getchar()
void rd(auto &x) { x = 0; char GC; bool f = 0;
for (; !IC; GC) f |= c == '-';
for (; IC; GC) x = x * 10 + c - 48;
if (f) x = -x;
}
void rd(auto &x, auto &...y) { rd(x); rd(y...); }
#define meow(...) fprintf(stderr, __VA_ARGS__)
#define Assert(e, v) if (!(e)) exit(v);
#define vc vector
#define pb push_back
#define eb emplace_back
const int N = 50005, M = 13;
int n, m;
ll c[N][M], f[M], g[N]; bool dir[N][M];
multiset<pair<int, int>> e[M][M];
const ll INF = 0x3f3f3f3f'3f3f3f3f;
int main() {
// freopen("ava.in", "r", stdin);
rd(n, m);
U (i, 1, n) U (j, 1, m) rd(c[i][j]);
U (i, 1, m) rd(f[i]);
U (i, 1, m) U (k, 1, n)
e[0][i].emplace(c[k][i], k);
ll ans = 0;
U (_, 1, n) {
ll dis[M][M]; ms(dis, 0x3f);
U (u, 1, m) U (v, 1, m) {
if (u == v) dis[u][v] = 0;
else dis[u][v] = e[u][v].size() ? e[u][v].begin()->first : INF;
}
U (u, 1, m) if (e[0][u].size())
dis[m + 1][u] = e[0][u].begin()->first;
U (u, 1, m) if (f[u])
dis[u][m + 2] = 0;
int tr[M][M] {};
U (u, 1, m + 2) U (v, 1, m + 2) U (w, 1, m + 2) if (dis[u][v] + dis[v][w] < dis[u][w]) {
dis[u][w] = dis[u][v] + dis[v][w];
tr[u][w] = v;
}
ll cur = dis[m + 1][m + 2];
vc<int> path;
auto get = [&](const auto &self, int u, int w)->void {
int v = tr[u][w];
if (!v) return;
self(self, u, v);
path.pb(v);
self(self, v, w);
};
path.pb(m + 1);
get(get, m + 1, m + 2);
path.pb(m + 2);
vc<pair<int, int>> flip;
U (t, 0, path.size() - 2) {
int u = path[t], v = path[t + 1];
if (u == m + 1) {
int w = e[0][v].begin()->second;
assert(g[w]);
g[w] = 0;
U (i, 1, m) {
auto it = e[0][i].find({c[w][i], w});
if (it != e[0][i].end())
e[0][i].erase(it);
}
flip.eb(w, v);
} else if (v == m + 2) {
assert(f[u] > 0);
--f[u];
} else {
int w = e[u][v].begin()->second;
flip.eb(w, u);
flip.eb(w, v);
}
}
for (auto [x, v] : flip) {
if (dir[x][v]) {
U (u, 1, m) if (!dir[x][u])
e[v][u].erase({-c[x][v] + c[x][u], x});
dir[x][v] = 0;
U (u, 1, m) if (dir[x][u])
e[u][v].insert({-c[x][u] + c[x][v], x});
if (g[x])
e[0][v].insert({c[x][v], x});
} else { // x to v
if (g[x]) {
auto it = e[0][v].find({c[x][v], x});
assert(it != e[0][v].end());
e[0][v].erase({c[x][v], x});
}
U (u, 1, m) if (dir[x][u])
e[u][v].erase({-c[x][u] + c[x][v], x});
dir[x][v] = 1;
U (u, 1, m) if (!dir[x][u])
e[v][u].insert({-c[x][v] + c[x][u], x});
}
}
ans += cur;
}
cout << ans << endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
6 2 1 2 1 3 1 4 1 5 1 6 1 7 3 4