Submission #701656

# Submission time Handle Problem Language Result Execution time Memory
701656 2023-02-21T16:54:47 Z karrigan Relay Marathon (NOI20_relaymarathon) C++17
17 / 100
6000 ms 169376 KB
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+9;
const long long inf=1e13;
struct edg{
int v;
long long w;
};
vector<edg>a[maxn];
vector<int>bit[2];
int b[maxn];
struct suzy{
int u;
long long w;
int lena;
bool operator < (const suzy &p)const {
return w>p.w;
}
};
int n,m,k;
long long dis[maxn];
int vis[maxn];
pair<long long,int>d[maxn][21];
int x=0,y=0,fix=0,fiy=0;
long long another=inf;
void sp(int bt){
if (bit[0].empty()||bit[1].empty())return;
for (int i=1;i<=n;i++){
    dis[i]=inf;
    vis[i]=0;
}
priority_queue<suzy>c;
for (auto v:bit[0]){
    c.push({v,0,v});
    dis[v]=0;
}
while (!c.empty()){
    auto u=c.top();
    c.pop();
    if (vis[u.u]==1)continue;
    vis[u.u]=1;
    if (u.w!=0)d[u.u][bt]={u.w,u.lena};
    //cerr<<u.lena<<'\n';
    for (auto v:a[u.u]){
        long long newdis=u.w+v.w;
        if (newdis<dis[v.v]){
            dis[v.v]=newdis;
            c.push({v.v,newdis,u.lena});
        }
    }
}
}
struct yoonjung{
int u;
long long w;
bool operator < (const yoonjung &p)const {
return w>p.w;
}
};
long long p[maxn][2];
int vi[maxn];
long long prefix[maxn];
long long suffix[maxn];
void anothersp(int u1,int t){
for (int i=1;i<=n;i++){
    p[i][t]=inf;
    vi[i]=0;
}
p[u1][t]=0;
priority_queue<yoonjung>c;
c.push({u1,0});
while (!c.empty()){
    auto u=c.top();
    c.pop();
    if (vi[u.u])continue;
    vi[u.u]=1;
    for (auto v:a[u.u]){
        long long newdis=u.w+v.w;
        if (p[v.v][t]>newdis){
            p[v.v][t]=newdis;
            c.push({v.v,newdis});
        }
    }
}
}
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	//freopen("GH.INP","r",stdin);
	//freopen("GH.OUT","w",stdout);
	cin>>n>>m>>k;
	for (int i=1;i<=m;i++){
        int u,v;
        long long w;
        cin>>u>>v>>w;
        a[u].push_back({v,w});
        a[v].push_back({u,w});
	}
	for (int i=1;i<=k;i++)cin>>b[i];
	long long ans=inf;
	for (int j=20;j>=0;j--){
        bit[0].clear();
        bit[1].clear();
        for (int i=1;i<=k;i++){
            bit[(i>>j&1)].push_back(b[i]);
        }
        //cout<<bit[0].size()<<" "<<bit[1].size()<<'\n';
        sp(j);
        for (auto v:bit[1]){
            if (d[v][j].first<ans&&d[v][j].second!=0){
                ans=d[v][j].first;
                x=d[v][j].second,y=v;
            }
        }
        swap(bit[0],bit[1]);
        sp(j);
        for (auto v:bit[1]){
            if (d[v][j].first<ans&&d[v][j].second!=0){
                ans=d[v][j].first;
                x=d[v][j].second,y=v;
            }
        }
	}
	fix=x,fiy=y;
	for (int i=1;i<=n;i++){
        for (int j=20;j>=0;j--){
            d[i][j]={0,0};
        }
	}
	for (int j=20;j>=0;j--){
        bit[0].clear();
        bit[1].clear();
        for (int i=1;i<=k;i++){
            if (b[i]!=fix&&b[i]!=fiy)bit[(i>>j&1)].push_back(b[i]);
        }
        sp(j);
        for (auto v:bit[1]){
            if (d[v][j].first<another&&d[v][j].second!=0){
                another=min(another,d[v][j].first);
            }
        }
        swap(bit[0],bit[1]);
        sp(j);
        for (auto v:bit[1]){
            if (d[v][j].first<another&&d[v][j].second!=0){
                another=min(another,d[v][j].first);
            }
        }
	}
	long long dd=ans+another;
	anothersp(x,0);
	anothersp(y,1);
	prefix[0]=inf;
	p[x][0]=p[x][1]=p[y][0]=p[y][1]=inf;
	for (int i=1;i<=k;i++){
        prefix[i]=min(prefix[i-1],p[b[i]][1]);
	}
	suffix[k+1]=inf;
	for (int i=k;i>=1;i--){
        suffix[i]=min(suffix[i+1],p[b[i]][1]);
	}
	for (int i=1;i<=k;i++){
        long long value=p[b[i]][0]+min(prefix[i-1],suffix[i+1]);
        dd=min(dd,value);
	}
	cout<<dd;
 
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2676 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2772 KB Output is correct
14 Correct 2 ms 2772 KB Output is correct
15 Correct 2 ms 2772 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2772 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2772 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 2 ms 2772 KB Output is correct
22 Correct 1 ms 2644 KB Output is correct
23 Correct 3 ms 2772 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 2 ms 2772 KB Output is correct
29 Correct 2 ms 2772 KB Output is correct
30 Correct 2 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2676 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2772 KB Output is correct
14 Correct 2 ms 2772 KB Output is correct
15 Correct 2 ms 2772 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2772 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2772 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 2 ms 2772 KB Output is correct
22 Correct 1 ms 2644 KB Output is correct
23 Correct 3 ms 2772 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 2 ms 2772 KB Output is correct
29 Correct 2 ms 2772 KB Output is correct
30 Correct 2 ms 2772 KB Output is correct
31 Correct 2 ms 2900 KB Output is correct
32 Correct 3 ms 2900 KB Output is correct
33 Correct 3 ms 2900 KB Output is correct
34 Correct 2 ms 2900 KB Output is correct
35 Correct 2 ms 2900 KB Output is correct
36 Correct 8 ms 3156 KB Output is correct
37 Correct 9 ms 3156 KB Output is correct
38 Correct 3 ms 2900 KB Output is correct
39 Correct 40 ms 7460 KB Output is correct
40 Correct 15 ms 4000 KB Output is correct
41 Correct 3 ms 2900 KB Output is correct
42 Correct 14 ms 3924 KB Output is correct
43 Correct 7 ms 3028 KB Output is correct
44 Correct 3 ms 2900 KB Output is correct
45 Correct 2 ms 2900 KB Output is correct
46 Correct 38 ms 7508 KB Output is correct
47 Correct 12 ms 3572 KB Output is correct
48 Correct 30 ms 7496 KB Output is correct
49 Correct 34 ms 7468 KB Output is correct
50 Correct 3 ms 2900 KB Output is correct
51 Correct 5 ms 2900 KB Output is correct
52 Correct 3 ms 2900 KB Output is correct
53 Correct 26 ms 5268 KB Output is correct
54 Correct 35 ms 7508 KB Output is correct
55 Correct 3 ms 2900 KB Output is correct
56 Correct 2 ms 2900 KB Output is correct
57 Correct 3 ms 2900 KB Output is correct
58 Correct 41 ms 7552 KB Output is correct
59 Correct 2 ms 2772 KB Output is correct
60 Correct 5 ms 2980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 382 ms 42484 KB Output is correct
2 Correct 19 ms 39156 KB Output is correct
3 Execution timed out 6042 ms 169376 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2676 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2772 KB Output is correct
14 Correct 2 ms 2772 KB Output is correct
15 Correct 2 ms 2772 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2772 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2772 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 2 ms 2772 KB Output is correct
22 Correct 1 ms 2644 KB Output is correct
23 Correct 3 ms 2772 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 2 ms 2772 KB Output is correct
29 Correct 2 ms 2772 KB Output is correct
30 Correct 2 ms 2772 KB Output is correct
31 Correct 2 ms 2900 KB Output is correct
32 Correct 3 ms 2900 KB Output is correct
33 Correct 3 ms 2900 KB Output is correct
34 Correct 2 ms 2900 KB Output is correct
35 Correct 2 ms 2900 KB Output is correct
36 Correct 8 ms 3156 KB Output is correct
37 Correct 9 ms 3156 KB Output is correct
38 Correct 3 ms 2900 KB Output is correct
39 Correct 40 ms 7460 KB Output is correct
40 Correct 15 ms 4000 KB Output is correct
41 Correct 3 ms 2900 KB Output is correct
42 Correct 14 ms 3924 KB Output is correct
43 Correct 7 ms 3028 KB Output is correct
44 Correct 3 ms 2900 KB Output is correct
45 Correct 2 ms 2900 KB Output is correct
46 Correct 38 ms 7508 KB Output is correct
47 Correct 12 ms 3572 KB Output is correct
48 Correct 30 ms 7496 KB Output is correct
49 Correct 34 ms 7468 KB Output is correct
50 Correct 3 ms 2900 KB Output is correct
51 Correct 5 ms 2900 KB Output is correct
52 Correct 3 ms 2900 KB Output is correct
53 Correct 26 ms 5268 KB Output is correct
54 Correct 35 ms 7508 KB Output is correct
55 Correct 3 ms 2900 KB Output is correct
56 Correct 2 ms 2900 KB Output is correct
57 Correct 3 ms 2900 KB Output is correct
58 Correct 41 ms 7552 KB Output is correct
59 Correct 2 ms 2772 KB Output is correct
60 Correct 5 ms 2980 KB Output is correct
61 Correct 382 ms 42484 KB Output is correct
62 Correct 19 ms 39156 KB Output is correct
63 Execution timed out 6042 ms 169376 KB Time limit exceeded
64 Halted 0 ms 0 KB -