QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#278732#7883. Takeout Deliveringucup-team173#ML 9ms32856kbC++172.0kb2023-12-07 19:52:232023-12-07 19:52:25

Judging History

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

  • [2023-12-07 19:52:25]
  • 评测
  • 测评结果:ML
  • 用时:9ms
  • 内存:32856kb
  • [2023-12-07 19:52:23]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define Mp make_pair
#define SZ(x) (int((x).size()))

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define int ll
const int maxn=6e5+10,maxm=1e6+10;
int f[maxn<<1];
int val[maxn<<1];
int getf(int x){
    return x==f[x]?x:(f[x]=getf(f[x]));
}
int cnt,n,m;
array<int,3>e[maxm];
int ans;
vector<int>v[maxn<<1];
int F[maxn<<1][20];
int de[maxn<<1];
void dfs(int x,int fa){
    F[x][0]=fa;
    de[x]=de[fa]+1;
    for(int i=1;i<20;i++){
        F[x][i]=F[F[x][i-1]][i-1];
    }
    for(int y:v[x])dfs(y,x);
}
int lca(int x,int y){
    if(de[x]<de[y])swap(x,y);
    for(int i=19;~i;i--){
        if(de[F[x][i]]>=de[y])x=F[x][i];
    }
    if(x==y)return x;
    for(int i=19;~i;i--){
        if(F[x][i]!=F[y][i]){
            x=F[x][i],y=F[y][i];
        }
    }
    return F[x][0];
}
int get(int x,int y){
    return val[lca(x,y)];
}
void solve() {
    cin>>n>>m;
    ans=1e18+100;
    for(int i=1;i<=m;i++){
        cin>>e[i][0]>>e[i][1]>>e[i][2];
        if(e[i][0]>e[i][1])swap(e[i][0],e[i][1]);
        if(e[i][0]==1&&e[i][1]==n)ans=min(ans,e[i][2]);
    }
    sort(e+1,e+m+1,[&](array<int,3>a,array<int,3>b){
        return a[2]<b[2];
    });
    iota(f+1,f+n+1,1);
    cnt=n;
    for(int i=1;i<=m;i++){
        int x=e[i][0],y=e[i][1],w=e[i][2];
        if(getf(x)==getf(y))continue;
        int xx=getf(x),yy=getf(y);
        ++cnt;
        f[cnt]=cnt;
        f[xx]=cnt;
        f[yy]=cnt;
        v[cnt].pb(xx);
        v[cnt].pb(yy);
        val[cnt]=w;
    }
    dfs(cnt,0);
    for(int i=1;i<=m;i++){
        int x=e[i][0],y=e[i][1],w=e[i][2];
        int v1=get(x,1),v2=get(y,n);
        if(v1<=w&&v2<=w)ans=min(ans,w+max(v1,v2));
        v1=get(x,n),v2=get(y,1);
        if(v1<=w&&v2<=w)ans=min(ans,w+max(v1,v2));
    }
    cout<<ans<<"\n";
}
signed main() {
    ios::sync_with_stdio(false), cin.tie(0), cout.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: 9ms
memory: 32856kb

input:

4 6
1 2 2
1 3 4
1 4 7
2 3 1
2 4 3
3 4 9

output:

5

result:

ok 1 number(s): "5"

Test #2:

score: -100
Memory Limit Exceeded

input:

300000 299999
80516 80517 597830404
110190 110191 82173886
218008 218009 954561262
250110 250111 942489774
66540 66541 156425292
34947 34948 239499776
273789 273790 453201232
84428 84429 439418398
98599 98600 326095035
55636 55637 355015760
158611 158612 684292473
43331 43332 43265001
171621 171622 ...

output:

1999991697

result: