Submission #952859

# Submission time Handle Problem Language Result Execution time Memory
952859 2024-03-25T03:23:01 Z sondos225 Relay Marathon (NOI20_relaymarathon) C++17
17 / 100
6000 ms 111788 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T>using ordered_set = tree<T,null_type,less_equal<T>,rb_tree_tag,tree_order_statistics_node_update>;//find_by_order(ind);
//order_of_key()
//#define int long long
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define yes "YES"
#define no "NO"
#define bigg INT_MAX
#define debug(x) cout<<(#x)<<" = " <<x<<endl;
#define all(x) x.begin(),x.end()
#define sz size()
#define nn '\n'
#define mms(x,y) memset(x,y,sizeof(x))
#define forr(i,j,n) for (int i=j; i<n; i++)
#define forn(i,j,n) for (int i=j; i>n; i--)
#define fi first
#define se second
#define la "LA"
#define cinn(x,y) for(int i=0; i<y; i++) cin>>x[i];
#define pii pair<int,int>
int par[100005]= { };
int fin(int x)
{
    if (x==par[x]) return x;
    return par[x]=fin(x);
}
signed main()
{
//    #ifndef LOCAL
//    freopen("lifeguards.in","r",stdin);
//    freopen("lifeguards.out","w", stdout);
//    #endif
    fast
    int n,m,k;
    cin>>n >>m >>k;
    vector<pii> a[n+1];
    forr(i,0,m)
    {
        int x,y,z;
        cin>>x >>y >>z;
        a[x].pb({y,z});
        a[y].pb({x,z});
    }
    int s[k];
    priority_queue<array<int,3>> q;
    bool b[n+1]= { };
    forr(i,0,k)
    {
        cin>>s[i];
        q.push({0,s[i],s[i]});
        b[s[i]]=1;
        par[s[i]]=s[i];
    }
    set <pii>vis;
    int ans1=0;
    int a1=0, a2=0;
    while(!q.empty())
    {
        int c=q.top()[0], x=q.top()[1], last=q.top()[2];
        q.pop();
        if (vis.find({x,fin(last)})!=vis.end()) continue;
        par[x]=fin(last);
        //  cout<<c<<' '<<x<<endl;
        if (b[x] && c!=0)
        {
            //    cout<<x<<' '<<c<<endl;
            ans1=-c;
            a1=x;
            a2=par[x];
            break;
        }
        // if (!b[x])
        vis.insert({x,par[x]});
//       cout<<"PUT"<<x<<' '<<last<<endl;
        for(auto y:a[x])
        {
//            cout"DWAR"<<y.fi<<' '<<par[x]<<endl;
            if (vis.find({y.fi,par[x]})==vis.end() && par[x]!=y.fi)
            {
                int o=abs(c);
                o+=y.se;
                q.push({-o,y.fi,par[x]});

                //     cout<<yes<<y.fi<<' '<<y.se<<' '<<last<<endl;
            }
        }
    }
    /////
    ///new1
    while(!q.empty()) q.pop();
    // q.clear();
    vis.clear();
    mms(par,0);
    forr(i,0,k)
    {
        //cin>>s[i];
        if (s[i]==a1 || s[i]==a2) continue;
        q.push({0,s[i],s[i]});
        //  b[s[i]]=1;
        par[s[i]]=s[i];
        // last[s[i]]=s[i];
    }
    ///////////////
    while(!q.empty())
    {
        int c=q.top()[0], x=q.top()[1], last=q.top()[2];
        q.pop();
        if (vis.find({x,fin(last)})!=vis.end()) continue;
        par[x]=fin(last);
        //  cout<<c<<' '<<x<<endl;
        if (b[x] && c!=0)
        {
            //    cout<<x<<' '<<c<<endl;
            ans1+=(-c);
            // a1=x;
            // a2=par[x];
            break;
        }
        // if (!b[x])
        vis.insert({x,par[x]});
//       cout<<"PUT"<<x<<' '<<last<<endl;
        for(auto y:a[x])
        {
//            cout"DWAR"<<y.fi<<' '<<par[x]<<endl;
            if (vis.find({y.fi,par[x]})==vis.end() && par[x]!=y.fi && y.fi!=a1 && y.fi!=a2)
            {
                int o=abs(c);
                o+=y.se;
                q.push({-o,y.fi,par[x]});

                //     cout<<yes<<y.fi<<' '<<y.se<<' '<<last<<endl;
            }
        }
    }
    /////////////////
    ///ans1 done
    /////
    ///new2
    while(!q.empty()) q.pop();
    // q.clear();
    vis.clear();
    mms(par,0);
    forr(i,0,k)
    {
        //cin>>s[i];
        if (s[i]==a1) continue;
        q.push({0,s[i],s[i]});
        //  b[s[i]]=1;
        par[s[i]]=s[i];
        // last[s[i]]=s[i];
    }
    ///////////////
    int ans2=0;
    int b1=0, b2=0;
    while(!q.empty())
    {
        int c=q.top()[0], x=q.top()[1], last=q.top()[2];
        q.pop();
        if (vis.find({x,fin(last)})!=vis.end()) continue;
        par[x]=fin(last);
        //  cout<<c<<' '<<x<<endl;
        if (b[x] && c!=0)
        {
            //    cout<<x<<' '<<c<<endl;
            ans2=(-c);
            b1=x;
            b2=par[x];
            break;
        }
        // if (!b[x])
        vis.insert({x,par[x]});
//       cout<<"PUT"<<x<<' '<<last<<endl;
        for(auto y:a[x])
        {
//            cout"DWAR"<<y.fi<<' '<<par[x]<<endl;
            if (vis.find({y.fi,par[x]})==vis.end() && par[x]!=y.fi && y.fi!=a1)
            {
                int o=abs(c);
                o+=y.se;
                q.push({-o,y.fi,par[x]});

                //     cout<<yes<<y.fi<<' '<<y.se<<' '<<last<<endl;
            }
        }
    }
    /////////////////
    //new22
    while(!q.empty()) q.pop();
    // q.clear();
    vis.clear();
    mms(par,0);
    forr(i,0,k)
    {
        //cin>>s[i];
        if (s[i]==a1) continue;
        q.push({0,s[i],s[i]});
        //  b[s[i]]=1;
        par[s[i]]=s[i];
        // last[s[i]]=s[i];
    }
    ///////////////
    // int ans2=0;
    //int b1=0, b2=0;
    while(!q.empty()) q.pop();
    // q.clear();
    vis.clear();
    mms(par,0);
    forr(i,0,k)
    {
        //cin>>s[i];
        if (s[i]==b1 || s[i]==b2) continue;
        q.push({0,s[i],s[i]});
        //  b[s[i]]=1;
        par[s[i]]=s[i];
        // last[s[i]]=s[i];
    }
    while(!q.empty())
    {
        int c=q.top()[0], x=q.top()[1], last=q.top()[2];
        q.pop();
        if (vis.find({x,fin(last)})!=vis.end()) continue;
        par[x]=fin(last);
        //  cout<<c<<' '<<x<<endl;
        if (b[x] && c!=0)
        {
            //    cout<<x<<' '<<c<<endl;
            ans2+=(-c);
            //  b1=x;
            // b2=par[x];
            break;
        }
        // if (!b[x])
        vis.insert({x,par[x]});
//       cout<<"PUT"<<x<<' '<<last<<endl;
        for(auto y:a[x])
        {
//            cout"DWAR"<<y.fi<<' '<<par[x]<<endl;
            if (vis.find({y.fi,par[x]})==vis.end() && par[x]!=y.fi && y.fi!=b1 && y.fi!=b2)
            {
                int o=abs(c);
                o+=y.se;
                q.push({-o,y.fi,par[x]});

                //     cout<<yes<<y.fi<<' '<<y.se<<' '<<last<<endl;
            }
        }
    }
    ///ans2 done
    cout<<min(ans1,ans2);
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 856 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 0 ms 604 KB Output is correct
7 Correct 1 ms 856 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
11 Correct 0 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 1 ms 1112 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 860 KB Output is correct
18 Correct 1 ms 860 KB Output is correct
19 Correct 2 ms 860 KB Output is correct
20 Correct 1 ms 860 KB Output is correct
21 Correct 1 ms 860 KB Output is correct
22 Correct 0 ms 604 KB Output is correct
23 Correct 1 ms 860 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 856 KB Output is correct
28 Correct 1 ms 860 KB Output is correct
29 Correct 1 ms 860 KB Output is correct
30 Correct 1 ms 856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 856 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 0 ms 604 KB Output is correct
7 Correct 1 ms 856 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
11 Correct 0 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 1 ms 1112 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 860 KB Output is correct
18 Correct 1 ms 860 KB Output is correct
19 Correct 2 ms 860 KB Output is correct
20 Correct 1 ms 860 KB Output is correct
21 Correct 1 ms 860 KB Output is correct
22 Correct 0 ms 604 KB Output is correct
23 Correct 1 ms 860 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 856 KB Output is correct
28 Correct 1 ms 860 KB Output is correct
29 Correct 1 ms 860 KB Output is correct
30 Correct 1 ms 856 KB Output is correct
31 Correct 1 ms 856 KB Output is correct
32 Correct 1 ms 856 KB Output is correct
33 Correct 2 ms 860 KB Output is correct
34 Correct 1 ms 860 KB Output is correct
35 Correct 0 ms 860 KB Output is correct
36 Correct 4 ms 1116 KB Output is correct
37 Correct 9 ms 1116 KB Output is correct
38 Correct 1 ms 860 KB Output is correct
39 Correct 25 ms 3676 KB Output is correct
40 Correct 14 ms 1624 KB Output is correct
41 Correct 1 ms 860 KB Output is correct
42 Correct 19 ms 1624 KB Output is correct
43 Correct 3 ms 860 KB Output is correct
44 Correct 1 ms 860 KB Output is correct
45 Correct 1 ms 860 KB Output is correct
46 Correct 124 ms 6856 KB Output is correct
47 Correct 13 ms 1308 KB Output is correct
48 Correct 35 ms 3428 KB Output is correct
49 Correct 69 ms 4316 KB Output is correct
50 Correct 1 ms 860 KB Output is correct
51 Correct 1 ms 860 KB Output is correct
52 Correct 1 ms 860 KB Output is correct
53 Correct 15 ms 2072 KB Output is correct
54 Correct 26 ms 3476 KB Output is correct
55 Correct 1 ms 860 KB Output is correct
56 Correct 1 ms 860 KB Output is correct
57 Correct 1 ms 860 KB Output is correct
58 Correct 72 ms 4412 KB Output is correct
59 Correct 1 ms 600 KB Output is correct
60 Correct 2 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 5988 KB Output is correct
2 Correct 4 ms 3676 KB Output is correct
3 Execution timed out 6039 ms 111788 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 856 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 0 ms 604 KB Output is correct
7 Correct 1 ms 856 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
11 Correct 0 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 1 ms 1112 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 860 KB Output is correct
18 Correct 1 ms 860 KB Output is correct
19 Correct 2 ms 860 KB Output is correct
20 Correct 1 ms 860 KB Output is correct
21 Correct 1 ms 860 KB Output is correct
22 Correct 0 ms 604 KB Output is correct
23 Correct 1 ms 860 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 856 KB Output is correct
28 Correct 1 ms 860 KB Output is correct
29 Correct 1 ms 860 KB Output is correct
30 Correct 1 ms 856 KB Output is correct
31 Correct 1 ms 856 KB Output is correct
32 Correct 1 ms 856 KB Output is correct
33 Correct 2 ms 860 KB Output is correct
34 Correct 1 ms 860 KB Output is correct
35 Correct 0 ms 860 KB Output is correct
36 Correct 4 ms 1116 KB Output is correct
37 Correct 9 ms 1116 KB Output is correct
38 Correct 1 ms 860 KB Output is correct
39 Correct 25 ms 3676 KB Output is correct
40 Correct 14 ms 1624 KB Output is correct
41 Correct 1 ms 860 KB Output is correct
42 Correct 19 ms 1624 KB Output is correct
43 Correct 3 ms 860 KB Output is correct
44 Correct 1 ms 860 KB Output is correct
45 Correct 1 ms 860 KB Output is correct
46 Correct 124 ms 6856 KB Output is correct
47 Correct 13 ms 1308 KB Output is correct
48 Correct 35 ms 3428 KB Output is correct
49 Correct 69 ms 4316 KB Output is correct
50 Correct 1 ms 860 KB Output is correct
51 Correct 1 ms 860 KB Output is correct
52 Correct 1 ms 860 KB Output is correct
53 Correct 15 ms 2072 KB Output is correct
54 Correct 26 ms 3476 KB Output is correct
55 Correct 1 ms 860 KB Output is correct
56 Correct 1 ms 860 KB Output is correct
57 Correct 1 ms 860 KB Output is correct
58 Correct 72 ms 4412 KB Output is correct
59 Correct 1 ms 600 KB Output is correct
60 Correct 2 ms 860 KB Output is correct
61 Correct 23 ms 5988 KB Output is correct
62 Correct 4 ms 3676 KB Output is correct
63 Execution timed out 6039 ms 111788 KB Time limit exceeded
64 Halted 0 ms 0 KB -