QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#273406#7883. Takeout Deliveringucup-team1134#WA 150ms47956kbC++232.5kb2023-12-02 23:40:532023-12-02 23:40:54

Judging History

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

  • [2023-12-02 23:40:54]
  • 评测
  • 测评结果:WA
  • 用时:150ms
  • 内存:47956kb
  • [2023-12-02 23:40:53]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=300005,INF=1<<30;

struct UF{
    int n;
    vector<int> par,size,edge;
    
    void init(int n_){
        n=n_;
        par.assign(n,-1);
        size.assign(n,1);
        edge.assign(n,0);
        
        for(int i=0;i<n;i++){
            par[i]=i;
        }
    }
    
    int root(int a){
        if(par[a]==a) return a;
        else return par[a]=root(par[a]);
    }
    
    void unite(int a,int b){
        edge[root(a)]++;
        if(root(a)!=root(b)){
            size[root(a)]+=size[root(b)];
            edge[root(a)]+=edge[root(b)];
            par[root(b)]=root(a);
        }
    }
    
    bool check(int a,int b){
        return root(a)==root(b);
    }
};

vector<pair<int,ll>> G[MAX];
ll dis[2][MAX];

void DFS(int u,int p,int q){
    for(auto [to,co]:G[u]){
        if(to==p) continue;
        dis[q][to]=max(dis[q][u],co);
        DFS(to,u,q);
    }
}

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int N,M;cin>>N>>M;
    UF uf;uf.init(N);
    
    vector<pair<pair<int,int>,int>> E(M);
    for(int i=0;i<M;i++){
        int a,b,c;cin>>a>>b>>c;a--;b--;
        E[i]=mp(mp(a,b),c);
    }
    
    sort(all(E),[](auto a,auto b){
        return a.se<b.se;
    });
    
    ll ans=1LL<<60;
    
    for(auto [e,c]:E){
        auto [a,b]=e;
        if(!uf.check(0,N-1)){
            if(!uf.check(a,b)){
                uf.unite(a,b);
                G[a].push_back(mp(b,c));
                G[b].push_back(mp(a,c));
            }
            
            if(uf.check(0,N-1)){
                for(int q=0;q<2;q++){
                    for(int i=0;i<N;i++) dis[q][i]=1LL<<60;
                    int st;
                    if(q==0) st=0;
                    else st=N-1;
                    dis[q][st]=0;
                    DFS(st,-1,q);
                }
            }
        }
        
        if(uf.check(0,N-1)){
            chmin(ans,dis[0][a]+dis[1][b]+c);
            chmin(ans,dis[0][b]+dis[1][a]+c);
        }
    }
    cout<<ans<<endl;
    
    
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 12720kb

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
Wrong Answer
time: 150ms
memory: 47956kb

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:

2999971848

result:

wrong answer 1st numbers differ - expected: '1999991697', found: '2999971848'