QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#385453#5102. Dungeon CrawlerSolitaryDream#WA 1ms3736kbC++171.9kb2024-04-10 19:24:062024-04-10 19:24:06

Judging History

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

  • [2024-04-10 19:24:06]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3736kb
  • [2024-04-10 19:24:06]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define FOR(i,s,t) for(int i=(s),_t=(t); i<=_t; ++i)
typedef long long ll;
const int N=2005;
vector<pair<int,int>> E[N];
int k,t;
int tid;
int Lt[N],Rt[N];
int Fa[N];
ll dis[N];
void dfs(int x,int fr) {
    Fa[x]=fr;
    Lt[x]=++tid;
    for(auto [y,w]: E[x]) {
        if(y!=fr) {
            dis[y]=dis[x]+w;
            dfs(y,x);
        }
    }
    Rt[x]=tid;
}
bool contain(int x,int y) {
    return Lt[x]<=Lt[y] && Rt[y]<=Rt[x];
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n,q;
    cin >> n >> q;
    ll tot=0;
    FOR(i,2,n) {
        int x,y,w;
        cin >> x >> y >> w;
        E[x].push_back({y,w});
        E[y].push_back({x,w});
        tot+=w*2;
    }
    while(q--) {
        int s;
        cin >> s >> k >> t;
        tid=0;
        dis[s]=0;
        dfs(s,0);
        if(contain(t,k)) {
            cout << "impossible\n";
        } else if(contain(k,t)) {
            ll mx=0;
            FOR(i,1,n) mx=max(mx,dis[i]);
            cout << tot-mx << '\n';
        } else {
            int p=s;
            FOR(i,1,n) {
                if(contain(i,k) && contain(i,t) && dis[i]>dis[p]) {
                    p=i;
                }
            }
            int q=0;
            for(auto [y,w]: E[p]) {
                if(y==Fa[p]) continue;
                if(contain(y,k)) {
                    q=y;
                    break;
                }
            }
            ll res=1e18;
            FOR(i,1,n) {
                if(contain(q,i)) {
                    res=min(res,tot-dis[i]+(dis[k]-dis[p])*2);
                } else {
                    // cerr << dis[i] << endl;
                    res=min(res,tot-dis[i]);
                }
            }
            cout << res << '\n';
        }
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

22 5
1 2 7
2 3 5
3 4 8
3 6 6
3 7 3
2 5 6
5 8 2
8 9 11
2 10 16
1 11 12
11 12 4
11 13 9
1 14 25
14 15 4
15 16 5
15 17 6
15 18 1
15 19 8
14 20 7
20 21 9
20 22 17
1 19 9
1 9 19
1 8 9
1 9 8
2 22 11

output:

316
293
293
impossible
314

result:

ok 5 lines

Test #2:

score: -100
Wrong Answer
time: 1ms
memory: 3736kb

input:

100 100
1 2 289384
2 3 930887
2 4 692778
4 5 636916
4 6 747794
4 7 238336
4 8 885387
8 9 760493
8 10 516650
8 11 641422
8 12 202363
8 13 490028
8 14 368691
8 15 520060
8 16 897764
16 17 513927
16 18 180541
16 19 383427
16 20 89173
16 21 455737
16 22 5212
16 23 595369
16 24 702568
16 25 956430
16 26 ...

output:

103526917
103484292
106288816
104379596
104405611
104775512
105613028
105291604
103838430
106026228
105161728
104175650
105894571
104509242
103971939
105376499
105223283
104153426
105082245
105423612
104130613
104800548
106846555
104138329
103769253
106879186
104044745
104385328
106973740
105460460
...

result:

wrong answer 7th lines differ - expected: '105434682', found: '105613028'