Submission #2861301
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 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); } 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); } } dick(); }
Submission Info
Submission Time | |
---|---|
Task | E - Snuke's Subway Trip |
User | haimeo1201 |
Language | C++14 (GCC 5.4.1) |
Score | 0 |
Code Size | 1638 Byte |
Status | WA |
Exec Time | 3156 ms |
Memory | 76592 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 | 867 ms | 42032 KB |
03.txt | AC | 371 ms | 76592 KB |
04.txt | AC | 265 ms | 74544 KB |
05.txt | AC | 270 ms | 60720 KB |
06.txt | WA | 105 ms | 49332 KB |
07.txt | AC | 140 ms | 49460 KB |
08.txt | AC | 350 ms | 76592 KB |
09.txt | AC | 161 ms | 53812 KB |
10.txt | AC | 157 ms | 53812 KB |
11.txt | TLE | 3156 ms | 33712 KB |
12.txt | TLE | 3156 ms | 31280 KB |
13.txt | TLE | 3156 ms | 31404 KB |
14.txt | AC | 1604 ms | 46128 KB |
15.txt | AC | 2916 ms | 42160 KB |
16.txt | TLE | 3156 ms | 36912 KB |
17.txt | TLE | 3156 ms | 33580 KB |
18.txt | TLE | 3156 ms | 32816 KB |
19.txt | TLE | 3156 ms | 32552 KB |
20.txt | TLE | 3156 ms | 32816 KB |
21.txt | TLE | 3156 ms | 34608 KB |
22.txt | TLE | 3156 ms | 31664 KB |
23.txt | AC | 275 ms | 58928 KB |
24.txt | TLE | 3156 ms | 32944 KB |
25.txt | TLE | 3156 ms | 31920 KB |
26.txt | TLE | 3156 ms | 33324 KB |
27.txt | AC | 474 ms | 52272 KB |
28.txt | TLE | 3014 ms | 49200 KB |
29.txt | AC | 1603 ms | 51120 KB |
30.txt | TLE | 3156 ms | 33580 KB |
31.txt | AC | 2971 ms | 38832 KB |
32.txt | TLE | 3156 ms | 36136 KB |
33.txt | AC | 898 ms | 52400 KB |
34.txt | TLE | 3156 ms | 31280 KB |
35.txt | TLE | 3156 ms | 40880 KB |
36.txt | TLE | 3156 ms | 30764 KB |
37.txt | AC | 1636 ms | 42672 KB |
38.txt | TLE | 3156 ms | 31924 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 | 32432 KB |
w10.txt | AC | 298 ms | 73776 KB |
w11.txt | TLE | 3156 ms | 29872 KB |
w12.txt | TLE | 3156 ms | 30128 KB |
w13.txt | AC | 482 ms | 47536 KB |
w14.txt | AC | 294 ms | 70064 KB |
w15.txt | TLE | 3156 ms | 32176 KB |
w16.txt | TLE | 3106 ms | 32816 KB |
w17.txt | AC | 304 ms | 33200 KB |
w18.txt | AC | 156 ms | 38704 KB |
w2.txt | TLE | 3156 ms | 32688 KB |
w3.txt | TLE | 3156 ms | 32560 KB |
w4.txt | TLE | 3156 ms | 36912 KB |
w5.txt | TLE | 3156 ms | 32688 KB |
w6.txt | TLE | 3156 ms | 31920 KB |
w7.txt | TLE | 3156 ms | 30512 KB |
w8.txt | AC | 2107 ms | 72880 KB |
w9.txt | AC | 484 ms | 73392 KB |