QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#318907 | #7185. Poor Students | PhanDinhKhoi# | TL | 3200ms | 8424kb | C++20 | 3.9kb | 2024-02-01 10:13:41 | 2024-02-01 10:13:42 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
#define fi first
#define se second
#define left BAO
#define right ANH
#define pb push_back
#define pf push_front
#define mp make_pair
#define ins insert
#define btpc __builtin_popcount
#define btclz __builtin_clz
#define sz(x) (int)(x.size());
#define all(x) x.begin(), x.end()
#define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int d4x[4] = {1, 0, -1, 0}; int d4y[4] = {0, 1, 0, -1};
int d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1};
template<class X, class Y>
bool minimize(X &x, const Y &y) {
if (x > y)
{
x = y;
return true;
}
return false;
}
template<class X, class Y>
bool maximize(X &x, const Y &y) {
if (x < y)
{
x = y;
return true;
}
return false;
}
const int MOD = 1e9 + 7; //998244353
template<class X, class Y>
void add(X &x, const Y &y) {
x = (x + y);
if(x >= MOD) x -= MOD;
}
template<class X, class Y>
void sub(X &x, const Y &y) {
x = (x - y);
if(x < 0) x += MOD;
}
/* Author : Le Ngoc Bao Anh, DUT, Da Nang */
const ll INF = 1e9;
const int N = 1e5 + 10;
struct MFMC {
//O(n ^ 2 * m ^ 2)
struct Edge {
int u, v;
ll cap, cost;
Edge() : u(), v(), cap(), cost() {}
Edge(int _u, int _v, ll _cap, ll _cost) : u(_u), v(_v), cap(_cap), cost(_cost) {}
};
vector<Edge> ed;
vector<vector<int>> g;
vector<ll> dist;
vector<bool> inq;
vector<int> p;
int n;
int S, T;
MFMC() : n(), S(), T() {}
MFMC(int _n, int _S, int _T) {
n = _n;
S = _S;
T = _T;
g.resize(n + 3);
dist.resize(n + 3);
inq.resize(n + 3);
p.resize(n + 3);
ed = vector<Edge>();
for(int i = 0; i < n; i++) g[i].clear();
}
void addEdge(int u, int v, ll cap, ll cost) {
g[u].pb(ed.size());
ed.pb(Edge(u, v, cap, cost));
g[v].pb(ed.size());
ed.pb(Edge(v, u, 0, -cost));
}
bool spfa() {
for(int i = 0; i <= n; i++) {
dist[i] = INF;
inq[i] = false;
p[i] = -1;
}
dist[S] = 0;
queue<int> q;
q.push(S); inq[S] = true;
while(!q.empty()) {
int u = q.front(); q.pop(); inq[u] = false;
for(int i = 0; i < (int)g[u].size(); i++) {
Edge e = ed[g[u][i]];
if(!e.cap) continue;
if(dist[e.v] > dist[u] + e.cost) {
dist[e.v] = dist[u] + e.cost;
p[e.v] = g[u][i];
if(!inq[e.v]) {
q.push(e.v);
inq[e.v] = true;
}
}
}
}
return (p[T] >= 0);
}
pll Flow() {
//return (maxflow, cost)
ll flow = 0, cost = 0;
while(spfa()) {
int u = T;
ll f = INF;
while(u != S) {
f = min(f, ed[p[u]].cap);
u = ed[p[u]].u;
}
flow += f; cost += f * dist[T];
u = T;
while(u != S) {
ed[p[u]].cap -= f;
ed[p[u] ^ 1].cap += f;
u = ed[p[u]].u;
}
}
return make_pair(flow, cost);
}
};
void BaoJiaoPisu() {
int n, k; cin >> n >> k;
int S = n + k + 1, T = n + k + 2;
MFMC G = MFMC(n + k + 5, S, T);
for(int i = 1; i <= n; i++) {
G.addEdge(S, i, 1, 0);
for(int j = 1; j <= k; j++) {
int x; cin >> x;
G.addEdge(i, j + n, 1, x);
}
}
for(int i = 1; i <= k; i++) {
int x; cin >> x;
G.addEdge(i + n, T, x, 0);
}
cout << G.Flow().se;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
//online
#endif
int tc = 1, ddd = 0;
// cin >> tc;
while(tc--) {
//ddd++;
//cout << "Case #" << ddd << ": ";
BaoJiaoPisu();
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3668kb
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: 0ms
memory: 3832kb
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: 0
Accepted
time: 88ms
memory: 4280kb
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...
output:
92039
result:
ok answer is '92039'
Test #4:
score: 0
Accepted
time: 3200ms
memory: 8424kb
input:
5000 10 14 114 254 832 38 904 25 147 998 785 917 694 750 372 379 887 247 817 999 117 802 15 799 515 316 42 69 247 95 144 727 398 509 725 682 456 369 656 693 955 923 1 681 631 962 826 233 963 289 856 165 491 488 832 111 950 853 791 929 240 509 843 667 970 469 260 447 477 161 431 514 903 627 236 144 3...
output:
461878
result:
ok answer is '461878'
Test #5:
score: -100
Time Limit Exceeded
input:
10000 10 307 205 765 487 504 526 10 581 234 583 448 443 39 992 976 363 335 588 588 169 920 787 896 822 47 358 230 631 136 299 141 159 414 852 922 945 513 76 111 189 616 104 83 792 24 68 164 975 615 472 150 108 848 517 7 153 107 283 452 165 94 370 910 662 226 720 975 214 324 407 636 65 963 859 590 3 ...