QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#171674 | #7185. Poor Students | ucup-team870# | TL | 2ms | 10080kb | C++17 | 3.6kb | 2023-09-09 17:25:41 | 2023-09-09 17:25:41 |
Judging History
answer
#include <bits/stdc++.h>
#define rep(i,l,r) for(int i=l; i<=r; i++)
#define IOS {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);}
using namespace std;
struct node {
int to, next, flow, cost;
} ed[2000010];
typedef int ll;
typedef pair<ll, int> P;
int s, t, head[100010], sz = -1, vis[100010];
ll spend, dis[100010], h[100010], c[50010][11];
void addEdge(int from, int to, int flow, int cost) {
sz++;
ed[sz].to = to;
ed[sz].next = head[from];
ed[sz].flow = flow;
ed[sz].cost = cost;
head[from] = sz;
}
void add(int from, int to, int flow, int cost) {
addEdge(from, to, flow, cost);
addEdge(to, from, 0, -cost);
}
bool dij(int s, int t) {
memset(vis, 0, sizeof(vis));
for (int i = 0; i <=t ;i++) dis[i] = 1e9;
priority_queue<P, vector<P>, greater<P> > q;
q.push(P(0, t));
dis[t] = 0;
// cout <<s;
while (!q.empty()) {
int u = q.top().second;
q.pop();
if(vis[u]) continue;
vis[u] = 1;
for (int i = head[u]; i!=-1; i = ed[i].next) {
int v = ed[i].to;
if (ed[i ^ 1].flow && dis[v] > dis[u] + (h[u] - h[v] + ed[i^1].cost)) {
dis[v] = dis[u] + (h[u] - h[v] + ed[i^1].cost);
q.push(P(dis[v], v));
// cout << dis[v];
}
}
}
return dis[s] != ll(1e9);
}
bool spfa(int s, int t) {
memset(vis, 0, sizeof(vis));
for (int i = 0;i <= t;i++) dis[i] = 1e9;
dis[t] = 0;
vis[t] = 1;
queue <int> q;
q.push(t);
while (!q.empty()) {
int u = q.front();
q.pop();
vis[u] = 0;
for (int i = head[u];i != -1;i = ed[i].next) {
int v = ed[i].to;
if (ed[i ^ 1].flow && dis[v] > dis[u] - ed[i].cost) {
dis[v] = dis[u] - ed[i].cost;
if (!vis[v]) {
vis[v] = 1;
q.push(v);
}
}
}
}
return dis[s] != 1e9;
}
int dfs(int u, int low) {
if (u == t) {
vis[t] = 1;
return low;
}
int used = 0;
vis[u] = 1;
for (int i = head[u];~i;i = ed[i].next) {
int v = ed[i].to;
if (!vis[v] && ed[i].flow > 0 && dis[u] - dis[v] == ed[i].cost+h[v]-h[u]) {
int now = dfs(v, min(low - used, ed[i].flow));
if (now) {
spend += 1ll*now * ed[i].cost, ed[i].flow -= now;
ed[i ^ 1].flow += now, used += now;
}
if (used == low) break;
}
}
return used;
}
int cost_flow() {
int flow = 0;
spfa(s, t);
rep (i, 0, t) h[i] = dis[i];
while (dij(s, t)) {
vis[t] = 1;
while (vis[t]) {
memset(vis, 0, sizeof(vis));
flow += dfs(s, 1e9);
}
for (int i = 0; i <= t; i++) h[i] += dis[i];
}
return flow;
}
P a[50010][11];
int main() {
memset(head, -1, sizeof(head));
int n, k;
scanf("%d%d", &n, &k);
s = n + k + 1;
t = s+1;
rep (i, 1, n) {
add(s, i, 1, 0);
rep (j, 1, k) {
scanf("%d", &c[i][j]);
a[i][j].first = c[i][j];
a[i][j].second = j;
// add(i, n+j, 1, x);
}
sort(a[i]+1, a[i]+k+1);
}
rep (i, 1, k) {
int x; scanf("%d", &x);
add(n+i, t, x, 0);
}
rep (i, 1, n) {
rep (j, 1, k) {
add(i, n+a[i][j].second, 1, a[i][j].first);
cost_flow();
}
}
cout << spend;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 9760kb
input:
6 2 1 2 1 3 1 4 1 5 1 6 1 7 3 4
output:
12
result:
ok answer is '12'
Test #2:
score: 0
Accepted
time: 1ms
memory: 10080kb
input:
3 3 1 2 3 2 4 6 6 5 4 1 1 1
output:
8
result:
ok answer is '8'
Test #3:
score: -100
Time Limit Exceeded
input:
1000 10 734 303 991 681 755 155 300 483 702 442 237 256 299 675 671 757 112 853 759 233 979 340 288 377 718 199 935 666 576 842 537 363 592 349 494 961 864 727 84 813 340 78 600 492 118 421 478 925 552 617 517 589 716 7 928 638 258 297 706 787 266 746 913 978 436 859 701 951 137 44 815 336 471 720 2...