Submission #2861310
Source Code Expand
#include<bits/stdc++.h> #define inf 1e18 #define int long long 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; bool vis[200005]; set<int> adj[300005]; vector<int> g[100005]; int dis[300005],n,m,cnt; vector<int> Set[300005]; void dfs_edge(int e,int color){ vis[e]=1; Set[cnt].push_back(e); for(int j:g[E[e].u]){ if(!vis[j]&&E[j].c==color){ dfs_edge(j,color); } } for(int j:g[E[e].v]){ if(!vis[j]&&E[j].c==color){ dfs_edge(j,color); } } } void dick(){ dis[1]=0; priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q; q.push(make_pair(0,1)); while(!q.empty()){ int u=q.top().second; int dist=q.top().first; q.pop(); if(dis[u]<dist) continue; for(set<int>::iterator it=adj[u].begin();it!=adj[u].end();it++){ if(dis[*it]>dis[u]+1){ dis[*it]=dis[u]+1; q.push(make_pair(dis[*it],*it)); } } } if(dis[n]==inf){ cout<<"-1"; exit(0); } else cout<<dis[n]/2; exit(0); } signed main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin>>n>>m; cnt=0; for(int i=0;i<=300000;i++){ dis[i]=1e8; } 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)); } for(int i=0;i<E.size();i++){ if(!vis[i]){ dfs_edge(i,E[i].c); cnt++; } } cnt--; for(int i=0;i<cnt;i++){ for(int j=0;j<Set[i].size();j++){ int e=Set[i][j]; adj[i+n+1].insert(E[e].u); adj[i+n+1].insert(E[e].v); adj[E[e].u].insert(i+n+1); adj[E[e].v].insert(i+n+1); } } dick(); }
Submission Info
Submission Time | |
---|---|
Task | E - Snuke's Subway Trip |
User | haimeo1201 |
Language | C++14 (GCC 5.4.1) |
Score | 0 |
Code Size | 1665 Byte |
Status | WA |
Exec Time | 3157 ms |
Memory | 84268 KB |
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 | 26112 KB |
02.txt | AC | 917 ms | 48172 KB |
03.txt | AC | 383 ms | 84268 KB |
04.txt | WA | 265 ms | 81068 KB |
05.txt | AC | 284 ms | 68652 KB |
06.txt | WA | 104 ms | 51760 KB |
07.txt | AC | 146 ms | 52528 KB |
08.txt | AC | 348 ms | 84268 KB |
09.txt | AC | 164 ms | 56880 KB |
10.txt | AC | 165 ms | 56880 KB |
11.txt | TLE | 3156 ms | 39468 KB |
12.txt | TLE | 3156 ms | 38184 KB |
13.txt | TLE | 3156 ms | 38184 KB |
14.txt | AC | 1403 ms | 54316 KB |
15.txt | WA | 2713 ms | 49068 KB |
16.txt | TLE | 3156 ms | 43180 KB |
17.txt | TLE | 3156 ms | 39336 KB |
18.txt | TLE | 3156 ms | 39340 KB |
19.txt | TLE | 3156 ms | 38308 KB |
20.txt | TLE | 3156 ms | 39852 KB |
21.txt | TLE | 3156 ms | 39084 KB |
22.txt | TLE | 3156 ms | 37032 KB |
23.txt | AC | 271 ms | 66476 KB |
24.txt | TLE | 3156 ms | 39596 KB |
25.txt | TLE | 3156 ms | 39336 KB |
26.txt | TLE | 3156 ms | 39592 KB |
27.txt | AC | 470 ms | 59820 KB |
28.txt | AC | 2824 ms | 56620 KB |
29.txt | AC | 1517 ms | 58796 KB |
30.txt | TLE | 3156 ms | 39336 KB |
31.txt | AC | 2780 ms | 46252 KB |
32.txt | TLE | 3156 ms | 42788 KB |
33.txt | AC | 866 ms | 60204 KB |
34.txt | TLE | 3156 ms | 37932 KB |
35.txt | TLE | 3157 ms | 48808 KB |
36.txt | TLE | 3156 ms | 36908 KB |
37.txt | AC | 1557 ms | 50220 KB |
38.txt | TLE | 3156 ms | 37676 KB |
sample_01.txt | AC | 10 ms | 25984 KB |
sample_02.txt | AC | 10 ms | 26112 KB |
sample_03.txt | WA | 11 ms | 25984 KB |
w1.txt | TLE | 3156 ms | 37676 KB |
w10.txt | WA | 295 ms | 79532 KB |
w11.txt | TLE | 3156 ms | 36656 KB |
w12.txt | TLE | 3156 ms | 37680 KB |
w13.txt | AC | 492 ms | 55212 KB |
w14.txt | WA | 298 ms | 75948 KB |
w15.txt | TLE | 3156 ms | 39724 KB |
w16.txt | TLE | 3156 ms | 39724 KB |
w17.txt | AC | 297 ms | 40492 KB |
w18.txt | AC | 164 ms | 45612 KB |
w2.txt | TLE | 3156 ms | 38700 KB |
w3.txt | TLE | 3156 ms | 38828 KB |
w4.txt | TLE | 3156 ms | 43308 KB |
w5.txt | TLE | 3156 ms | 38572 KB |
w6.txt | TLE | 3156 ms | 38832 KB |
w7.txt | TLE | 3156 ms | 36268 KB |
w8.txt | AC | 2124 ms | 78892 KB |
w9.txt | AC | 510 ms | 78764 KB |