QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#397762 | #7185. Poor Students | zhangtx123 | TL | 1ms | 6084kb | C++14 | 2.6kb | 2024-04-24 16:47:55 | 2024-04-24 16:47:56 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) fprintf(stdout, ##__VA_ARGS__)
#define debag(...) fprintf(stderr, ##__VA_ARGS__)
#else
#define debug(...) void(0)
#define debag(...) void(0)
#endif
#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define Z(x) (x)*(x)
#define pb push_back
#define fi first
#define se second
#define M 12
//#define mo
#define N 50010
struct node {
int x, dis;
bool operator < (const node &A) const {
return dis < A.dis;
}
};
set<node> s[M][M];
priority_queue< pair<int, int> >q;
int vis[N], a[N], xuan[N], lst[N];
int c[N][M], ans;
stack<int>z;
int n, m, i, j, k, T;
void spfa(int x) {
// s[u][v] k -> u ==> k -> v
// debag("In %lld\n", x);
int i, v, y;
for(i = 1; i <= m; ++i) if(!a[i]) q.push({vis[i], i});
while(!q.empty()) {
auto t = q.top(); q.pop(); int u = t.se;
if(t.fi != vis[u]) continue;
// debag("$ %lld\n", u);
for(v = 1; v <= m; ++v) {
if(s[u][v].begin() == s[u][v].end()) continue;
set<node> :: iterator it = s[u][v].begin();
if(vis[u] + (it -> dis) < vis[v]) {
// debug("%lld == %lld(%lld) to %lld\n", u, (it -> dis), (it -> x), v);
vis[v] = vis[u] + (it -> dis); lst[v] = u;
if(!a[v]) q.push({vis[v], v});
}
}
}
for(i = 1, j = 0, vis[0] = 1e18; i <= m; ++i)
if(a[i] && vis[i] < vis[j]) j = i;
// for(i = 1; i <= m; ++i) debug("%lld ", vis[i]); debug("| %lld\n", j);
ans += vis[j]; --a[j];
while(lst[j]) {
set<node> :: iterator it = s[lst[j]][j].begin();
y = (it -> x);
for(i = 1; i <= m; ++i)
s[xuan[y]][i].erase({y, c[y][i] - c[y][xuan[y]]});
xuan[y] = j; z.push(y);
j = lst[j];
}
xuan[x] = j; z.push(x);
// debug("xuan[%lld] -> %lld\n", x, xuan[x]);
assert(z.size() <= k + 5);
while(!z.empty()) {
y = z.top(); z.pop();
// debug("adjust %lld to %lld\n", y, xuan[y]);
for(i = 1; i <= m; ++i)
s[xuan[y]][i].insert({y, c[y][i] - c[y][xuan[y]]});
}
}
signed main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
// srand(time(NULL));
// T=read();
// while(T--) {
//
// }
n = read(); m = read();
for(i = 1; i <= n; ++i) for(j = 1; j <= m; ++j) c[i][j] = read();
for(i = 1; i <= m; ++i) a[i] = read();
for(i = 1; i <= n; ++i) {
for(j = 1; j <= m; ++j) vis[j] = c[i][j], lst[i] = 0;
spfa(i);
}
// for(i = 1; i <= n; ++i) debug("%lld ", xuan[i]); debug("\n");
printf("%lld", ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 6084kb
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: 5892kb
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...