QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#569237 | #7185. Poor Students | masttf | TL | 0ms | 3604kb | C++20 | 2.7kb | 2024-09-16 21:26:41 | 2024-09-16 21:26:41 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
using namespace std;
#define dbg(x...) \
do { \
cout << #x << " -> "; \
err(x); \
} while (0)
void err() {
cout<<endl<<endl;
}
template<class T, class... Ts>
void err(T arg, Ts ... args) {
cout<<fixed<<setprecision(10)<<arg<< ' ';
err(args...);
}
constexpr int Max = 1e18;
void solve(){
int n, k; cin >> n >> k;
vector a(n + 1, vector<int> (k + 1));
for(int i = 1; i <= n; i++){
for(int j = 1; j <= k; j++){
cin >> a[i][j];
}
}
vector<int> lim(k + 1);
vector<int> bl(n + 1);
for(int i = 1; i <= k; i++) cin >> lim[i];
int ans = 0;
vector q(k + 1, vector (k + 1, set<pair<int ,int>>()));
auto add = [&](int x) -> void{
for(int i = 1; i <= k; i++){
if(i == bl[x])continue;
q[bl[x]][i].insert({a[x][i] - a[x][bl[x]], x});
}
};
auto del = [&](int x) -> void{
for(int i = 1; i <= k; i++){
if(i == bl[x])continue;
q[bl[x]][i].erase({a[x][i] - a[x][bl[x]], x});
}
};
for(int i = 1; i <= n; i++){
add(i);
}
for(int tot = 1; tot <= n; tot++){
vector g(k + 2, vector<int>(k + 2));
for(int i = 0; i <= k; i++){
for(int j = 1; j <= k; j++){
if(q[i][j].empty())g[i][j] = Max;
else g[i][j] = q[i][j].begin() -> first;
}
}
vector<int> dis(k + 2, Max), pre(k + 1, -1);
vector<int> vis(k + 2, 0);
queue<int> que;
dis[0] = 0;
que.push(0);
while(!que.empty()){
int u = que.front();
que.pop();
vis[u] = 1;
for(int i = 1; i <= k; i++){
if(dis[u] + g[u][i] < dis[i]){
dis[i] = dis[u] + g[u][i];
pre[i] = u;
if(!vis[i]){
vis[i] = 1;
que.push(i);
}
}
}
}
int now = -1;
for(int i = 1; i <= k; i++){
if(lim[i] && (now == -1 || dis[i] < dis[now])){
now = i;
}
}
ans += dis[now];
lim[now]--;
while(now){
int fa = pre[now];
int v = q[fa][now].begin() -> second;
del(v);
bl[v] = now;
add(v);
now = pre[now];
}
}
// for(int i = 1; i <= n; i++){
// dbg(i, bl[i]);
// }
cout << ans << '\n';
return ;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t=1;//cin>>t;
while(t--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3532kb
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: 3604kb
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...