Submission #2860670
Source Code Expand
#include<bits/stdc++.h> #define inf 1e8 using namespace std; struct edge{ int c,u,v; edge(int a,int b,int d):u(a),v(b),c(d) {} }; vector<edge> e; set<int> adj[300005]; vector<int> g[300005]; int par[300005],dis[300005],n,m; void init(){ for(int i=0;i<=300005;i++){ par[i]=i; dis[i]=1e8; } } int find(int i){ if(par[i]==i) return par[i]; return par[i]=find(par[i]); } void dsu(int i,int j){ par[find(i)]=find(j); } void bfs(){ dis[1]=0; queue<int> q; q.push(1); while(!q.empty()){ int u=q.front();q.pop(); for(set<int>::iterator it=adj[u].begin();it!=adj[u].end();it++){ if(dis[*it]>dis[u]+1){ dis[*it]=min(dis[*it],dis[u]+1); q.push(*it); } } } if(dis[n]==inf){ cout<<"-1"; exit(0); } else cout<<dis[n]/2; exit(0); } int main(){ cin>>n>>m; for(int i=1; i<=m; i++){ int c,u,v; cin>>u>>v>>c; g[u].push_back(e.size()); g[v].push_back(e.size()); e.push_back(edge(u,v,c)); } init(); for(int i=0;i<e.size();i++){ int u=e[i].u; for(int j: g[u]){ if(e[j].c==e[i].c&&i!=j){ dsu(i,j); } } int v=e[i].v; for(int j: g[v]){ if(e[j].c == e[i].c&&i!=j){ dsu(i,j); } } } for(int i=0;i<e.size();i++){ adj[par[i]+n+1].insert(e[i].u); adj[par[i]+n+1].insert(e[i].v); adj[e[i].v].insert(par[i]+1+n); adj[e[i].u].insert(par[i]+1+n); } bfs(); }
Submission Info
Submission Time | |
---|---|
Task | E - Snuke's Subway Trip |
User | haimeo1201 |
Language | C++14 (GCC 5.4.1) |
Score | 0 |
Code Size | 1404 Byte |
Status | WA |
Exec Time | 3160 ms |
Memory | 68528 KB |
Compile Error
./Main.cpp: In function ‘void init()’: ./Main.cpp:15:11: warning: iteration 300005u invokes undefined behavior [-Waggressive-loop-optimizations] par[i]=i; ^ ./Main.cpp:14:15: note: containing loop for(int i=0;i<=300005;i++){ ^
Judge Result
Set Name | Sample | All | ||||||
---|---|---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 0 / 600 | ||||||
Status | AC |
|
Set Name | Test Cases |
---|---|
Sample | |
All | 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, sample_01.txt, sample_02.txt, sample_03.txt, w1.txt, w10.txt, w11.txt, w12.txt, w13.txt, w14.txt, w15.txt, w16.txt, w17.txt, w18.txt, w2.txt, w3.txt, w4.txt, w5.txt, w6.txt, w7.txt, w8.txt, w9.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
01.txt | AC | 10 ms | 23680 KB |
02.txt | AC | 902 ms | 43312 KB |
03.txt | AC | 421 ms | 68528 KB |
04.txt | AC | 348 ms | 67888 KB |
05.txt | AC | 374 ms | 57392 KB |
06.txt | WA | 146 ms | 45620 KB |
07.txt | WA | 168 ms | 46132 KB |
08.txt | AC | 402 ms | 68144 KB |
09.txt | AC | 189 ms | 48948 KB |
10.txt | AC | 180 ms | 48820 KB |
11.txt | TLE | 3156 ms | 30256 KB |
12.txt | TLE | 3156 ms | 28336 KB |
13.txt | TLE | 3156 ms | 28844 KB |
14.txt | WA | 2637 ms | 52656 KB |
15.txt | TLE | 3156 ms | 30000 KB |
16.txt | TLE | 3156 ms | 30384 KB |
17.txt | TLE | 3156 ms | 30124 KB |
18.txt | TLE | 3156 ms | 28592 KB |
19.txt | TLE | 3156 ms | 30376 KB |
20.txt | AC | 2830 ms | 50608 KB |
21.txt | TLE | 3156 ms | 29616 KB |
22.txt | TLE | 3160 ms | 29740 KB |
23.txt | AC | 331 ms | 55088 KB |
24.txt | TLE | 3156 ms | 30256 KB |
25.txt | TLE | 3156 ms | 28592 KB |
26.txt | TLE | 3156 ms | 28972 KB |
27.txt | TLE | 3026 ms | 48560 KB |
28.txt | TLE | 3156 ms | 30256 KB |
29.txt | TLE | 3156 ms | 30000 KB |
30.txt | TLE | 3156 ms | 30252 KB |
31.txt | TLE | 3156 ms | 28720 KB |
32.txt | TLE | 3156 ms | 31016 KB |
33.txt | TLE | 3156 ms | 29872 KB |
34.txt | TLE | 3156 ms | 29744 KB |
35.txt | TLE | 3156 ms | 28592 KB |
36.txt | TLE | 3156 ms | 29356 KB |
37.txt | AC | 846 ms | 53168 KB |
38.txt | TLE | 3156 ms | 29616 KB |
sample_01.txt | AC | 10 ms | 23680 KB |
sample_02.txt | AC | 9 ms | 23680 KB |
sample_03.txt | AC | 9 ms | 23680 KB |
w1.txt | TLE | 3156 ms | 30256 KB |
w10.txt | AC | 390 ms | 66480 KB |
w11.txt | TLE | 3156 ms | 28336 KB |
w12.txt | AC | 2877 ms | 29616 KB |
w13.txt | WA | 458 ms | 44720 KB |
w14.txt | AC | 380 ms | 62768 KB |
w15.txt | TLE | 3156 ms | 28336 KB |
w16.txt | TLE | 3156 ms | 28336 KB |
w17.txt | AC | 946 ms | 28848 KB |
w18.txt | WA | 340 ms | 36400 KB |
w2.txt | TLE | 3156 ms | 30640 KB |
w3.txt | TLE | 3156 ms | 30768 KB |
w4.txt | TLE | 3156 ms | 31536 KB |
w5.txt | TLE | 3156 ms | 30512 KB |
w6.txt | TLE | 3156 ms | 30384 KB |
w7.txt | TLE | 3156 ms | 28464 KB |
w8.txt | AC | 2724 ms | 65200 KB |
w9.txt | AC | 560 ms | 65584 KB |