QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#278731#7883. Takeout Deliveringucup-team173#RE 3ms17956kbC++172.0kb2023-12-07 19:51:452023-12-07 19:51:46

Judging History

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

  • [2023-12-07 19:51:46]
  • 评测
  • 测评结果:RE
  • 用时:3ms
  • 内存:17956kb
  • [2023-12-07 19:51:45]
  • 提交

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=3e5+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][20];
int de[maxn];
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;
}

详细

Test #1:

score: 100
Accepted
time: 3ms
memory: 17956kb

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
Runtime Error

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:


result: