QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#352039#7185. Poor StudentsinstallbTL 1ms5844kbC++172.7kb2024-03-12 19:53:402024-03-12 19:53:41

Judging History

你现在查看的是最新测评结果

  • [2024-03-12 19:53:41]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:5844kb
  • [2024-03-12 19:53:40]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100005;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;

ll dis[25];
int inq[25],pre[25],prv[25];
set <pair <int,int> > st[25][25];
int c[100005][25],dir[25][100005],deg[25],n,k;

int spfa(){
    for(int i = 0;i <= k;i ++){
        dis[i] = LLINF; inq[i] = 0;
    }
    queue <int> q; q.push(0);
    inq[0] = 1; dis[0] = 0;
    while(!q.empty()){
        int u = q.front(); q.pop();
        inq[u] = 0;
        for(int v = 0;v <= k;v ++){
            if(st[u][v].size()){
                auto [w,id] = *st[u][v].begin();
                if(dis[v] > dis[u] + w){
                    dis[v] = dis[u] + w;
                    prv[v] = u; pre[v] = id;
                    if(!inq[v]){
                        inq[v] = 1;
                        q.push(v);
                    }
                }
            }
        }
    }
    int id = -1;
    for(int i = 1;i <= k;i ++){
        if(deg[i] > 0 && (id == -1 || dis[i] < dis[id])) id = i;
    }
    return id;
}

void solve(){
    cin >> n >> k;
    for(int i = 1;i <= n;i ++){
        for(int j = 1;j <= k;j ++){
            cin >> c[i][j];
        }
        c[i][0] = INF;
    }
    for(int i = 1;i <= k;i ++) cin >> deg[i];

    for(int i = 1;i <= n;i ++){
        dir[0][i] = 1;
        for(int j = 1;j <= k;j ++){
            st[0][j].insert({c[i][j] - c[i][0],i});
        }
    }
    ll ans = 0;
    for(int ti = 1;ti <= n;ti ++){
        // for(int i = 0;i <= k;i ++){
        //     for(int j = 0;j <= k;j ++){
        //         if(st[i][j].size()) cout << st[i][j].begin()->first << ',' << st[i][j].begin()->second << ' ';
        //         else cout << "0,0 ";
        //     }
        //     cout << endl;
        // }
        int v = spfa();
        deg[v] --; ans += dis[v];
        if(n >= 20 && ti >= 500) cout << ti << ' ' << v << ' ' << deg[v] << ' ' << dis[v] << endl;
        while(v){
            int u = prv[v],w = pre[v];
            // u -> w -> v
            for(int i = 0;i <= k;i ++){
                if(i != u){
                    if(!dir[i][w]) st[u][i].erase({c[w][i] - c[w][u],w});
                    else st[i][u].insert({c[w][u] - c[w][i],w});
                }
            }
            for(int i = 0;i <= k;i ++){
                if(i != v){
                    if(dir[i][w]) st[i][v].erase({c[w][v] - c[w][i],w});
                    else st[v][i].insert({c[w][i] - c[w][v],w});
                }
            }
            dir[u][w] ^= 1; dir[v][w] ^= 1;
            v = u;
        }
        deg[v] --;
    }
    cout << ans + 1ll * n * INF << '\n';
}

int main(){
    ios::sync_with_stdio(false);
    solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5844kb

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: 5636kb

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...

output:

500 10 45 -1061109502
501 1 50 -1061109501
502 2 70 -1061109501
503 2 69 -1061109501
504 3 28 -1061109501
505 6 61 -1061109501
506 1 49 -1061109500
507 3 27 -1061109500
508 4 53 -1061109500
509 5 42 -1061109500
510 9 59 -1061109500
511 4 52 -1061109499
512 5 41 -1061109499
513 10 44 -1061109499
514 ...

result: