Submission #2861288
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; 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 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(){ 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); } } bfs(); }
Submission Info
Submission Time | |
---|---|
Task | E - Snuke's Subway Trip |
User | haimeo1201 |
Language | C++14 (GCC 5.4.1) |
Score | 0 |
Code Size | 1491 Byte |
Status | WA |
Exec Time | 3156 ms |
Memory | 75568 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 | 24960 KB |
02.txt | AC | 925 ms | 41264 KB |
03.txt | AC | 312 ms | 75568 KB |
04.txt | AC | 262 ms | 74544 KB |
05.txt | AC | 238 ms | 59952 KB |
06.txt | WA | 101 ms | 49332 KB |
07.txt | AC | 115 ms | 49332 KB |
08.txt | AC | 307 ms | 75184 KB |
09.txt | AC | 134 ms | 53684 KB |
10.txt | AC | 133 ms | 53556 KB |
11.txt | TLE | 3156 ms | 33712 KB |
12.txt | TLE | 3156 ms | 31408 KB |
13.txt | TLE | 3156 ms | 31148 KB |
14.txt | AC | 1625 ms | 44336 KB |
15.txt | AC | 2809 ms | 42288 KB |
16.txt | TLE | 3156 ms | 36912 KB |
17.txt | TLE | 3156 ms | 33324 KB |
18.txt | TLE | 3156 ms | 32688 KB |
19.txt | TLE | 3156 ms | 32168 KB |
20.txt | TLE | 3156 ms | 32816 KB |
21.txt | TLE | 3156 ms | 34864 KB |
22.txt | TLE | 3156 ms | 31664 KB |
23.txt | AC | 240 ms | 58032 KB |
24.txt | TLE | 3156 ms | 33456 KB |
25.txt | TLE | 3156 ms | 31920 KB |
26.txt | TLE | 3156 ms | 32812 KB |
27.txt | AC | 463 ms | 50608 KB |
28.txt | AC | 2896 ms | 48304 KB |
29.txt | AC | 1541 ms | 50224 KB |
30.txt | TLE | 3156 ms | 33836 KB |
31.txt | AC | 2877 ms | 38704 KB |
32.txt | TLE | 3156 ms | 36392 KB |
33.txt | AC | 864 ms | 50480 KB |
34.txt | TLE | 3156 ms | 31280 KB |
35.txt | TLE | 3156 ms | 40624 KB |
36.txt | TLE | 3156 ms | 30636 KB |
37.txt | AC | 1654 ms | 41264 KB |
38.txt | TLE | 3156 ms | 31408 KB |
sample_01.txt | AC | 10 ms | 24832 KB |
sample_02.txt | AC | 10 ms | 24832 KB |
sample_03.txt | AC | 10 ms | 24832 KB |
w1.txt | TLE | 3156 ms | 32304 KB |
w10.txt | AC | 291 ms | 73776 KB |
w11.txt | TLE | 3156 ms | 29872 KB |
w12.txt | TLE | 3156 ms | 30644 KB |
w13.txt | AC | 478 ms | 48048 KB |
w14.txt | AC | 288 ms | 69424 KB |
w15.txt | TLE | 3156 ms | 32176 KB |
w16.txt | TLE | 3156 ms | 32688 KB |
w17.txt | AC | 333 ms | 33328 KB |
w18.txt | AC | 160 ms | 39472 KB |
w2.txt | TLE | 3156 ms | 32560 KB |
w3.txt | TLE | 3156 ms | 32560 KB |
w4.txt | TLE | 3156 ms | 37040 KB |
w5.txt | TLE | 3156 ms | 32816 KB |
w6.txt | TLE | 3156 ms | 31920 KB |
w7.txt | TLE | 3156 ms | 30512 KB |
w8.txt | AC | 2114 ms | 72880 KB |
w9.txt | AC | 470 ms | 73776 KB |