Submission #2860753
Source Code Expand
#include<bits/stdc++.h> using namespace std; #define fi first #define se second #define FOR(a, b, c) for(int a = b; a <= c; ++a) #define pb push_back #define mp make_pair typedef pair<int, int> ii; typedef pair<int, ii> iii; const int N = 2e5 + 10; const int oo = 1e9; struct state{ int dist, u, ci; }; int n, m; map<int, int> dp[N]; vector<ii> vi[N]; void solve() { deque<state> pq; pq.push_back({0, 1, 0}); while(!pq.empty()) { state cur = pq.front(); pq.pop_front(); int u = cur.u, ci = cur.ci; if(!dp[u].count(0) || (dp[u][0] > cur.dist) ){ dp[u][0] = cur.dist; pq.push_back({cur.dist, u, 0}); } if(ci) { int pos = lower_bound(vi[u].begin(), vi[u].end(), ii(ci, 0)) - vi[u].begin(); while(pos < vi[u].size() && vi[u][pos].fi == ci) { int v = vi[u][pos].se; if(!dp[v].count(ci) || dp[v][ci] > cur.dist) { dp[v][ci] = cur.dist; pq.push_front({cur.dist, v, ci}); } pos ++; } } else { for(ii v: vi[u]) { if(!dp[v.se].count(v.fi) || dp[v.se][v.fi] > cur.dist + 1) { dp[v.se][v.fi] = cur.dist + 1; pq.push_back({cur.dist + 1, v.se, v.fi}); } } } } int ans = oo; for(auto v: dp[n]) ans = min(ans, v.se); if(ans == oo) printf("-1"); else printf("%d", ans); } signed main() { //freopen("test.inp", "r", stdin); scanf("%d%d", &n, &m); FOR(i, 1, m) { int u, v, ci; scanf("%d%d%d", &u, &v, &ci); vi[u].pb(ii(ci, v)); vi[v].pb(ii(ci, u)); } for(int i = 1; i <= n; ++i) sort(vi[i].begin(), vi[i].end()); solve(); }
Submission Info
Submission Time | |
---|---|
Task | E - Snuke's Subway Trip |
User | Vinhspm |
Language | C++14 (GCC 5.4.1) |
Score | 600 |
Code Size | 1845 Byte |
Status | AC |
Exec Time | 311 ms |
Memory | 44288 KB |
Compile Error
./Main.cpp: In function ‘int main()’: ./Main.cpp:59:23: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf("%d%d", &n, &m); ^ ./Main.cpp:61:51: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] int u, v, ci; scanf("%d%d%d", &u, &v, &ci); ^
Judge Result
Set Name | Sample | All | ||
---|---|---|---|---|
Score / Max Score | 0 / 0 | 600 / 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 | 6 ms | 14336 KB |
02.txt | AC | 208 ms | 23936 KB |
03.txt | AC | 257 ms | 43904 KB |
04.txt | AC | 90 ms | 20608 KB |
05.txt | AC | 216 ms | 38016 KB |
06.txt | AC | 85 ms | 30976 KB |
07.txt | AC | 109 ms | 31744 KB |
08.txt | AC | 255 ms | 44288 KB |
09.txt | AC | 116 ms | 33408 KB |
10.txt | AC | 118 ms | 33408 KB |
11.txt | AC | 140 ms | 31996 KB |
12.txt | AC | 71 ms | 18296 KB |
13.txt | AC | 109 ms | 23160 KB |
14.txt | AC | 141 ms | 31488 KB |
15.txt | AC | 110 ms | 28668 KB |
16.txt | AC | 129 ms | 30592 KB |
17.txt | AC | 146 ms | 32376 KB |
18.txt | AC | 86 ms | 19964 KB |
19.txt | AC | 121 ms | 30452 KB |
20.txt | AC | 165 ms | 33280 KB |
21.txt | AC | 311 ms | 42492 KB |
22.txt | AC | 188 ms | 33912 KB |
23.txt | AC | 228 ms | 36480 KB |
24.txt | AC | 131 ms | 32380 KB |
25.txt | AC | 69 ms | 18296 KB |
26.txt | AC | 98 ms | 23288 KB |
27.txt | AC | 153 ms | 33920 KB |
28.txt | AC | 135 ms | 33020 KB |
29.txt | AC | 140 ms | 33408 KB |
30.txt | AC | 129 ms | 31608 KB |
31.txt | AC | 98 ms | 23164 KB |
32.txt | AC | 118 ms | 29812 KB |
33.txt | AC | 152 ms | 33792 KB |
34.txt | AC | 140 ms | 28668 KB |
35.txt | AC | 67 ms | 18296 KB |
36.txt | AC | 90 ms | 20600 KB |
37.txt | AC | 130 ms | 29056 KB |
38.txt | AC | 123 ms | 28668 KB |
sample_01.txt | AC | 6 ms | 14336 KB |
sample_02.txt | AC | 6 ms | 14336 KB |
sample_03.txt | AC | 6 ms | 14336 KB |
w1.txt | AC | 126 ms | 30436 KB |
w10.txt | AC | 109 ms | 23808 KB |
w11.txt | AC | 84 ms | 18008 KB |
w12.txt | AC | 90 ms | 18688 KB |
w13.txt | AC | 158 ms | 26624 KB |
w14.txt | AC | 145 ms | 31104 KB |
w15.txt | AC | 69 ms | 18264 KB |
w16.txt | AC | 71 ms | 18304 KB |
w17.txt | AC | 78 ms | 18816 KB |
w18.txt | AC | 110 ms | 23936 KB |
w2.txt | AC | 146 ms | 30556 KB |
w3.txt | AC | 176 ms | 34292 KB |
w4.txt | AC | 141 ms | 30964 KB |
w5.txt | AC | 291 ms | 44276 KB |
w6.txt | AC | 211 ms | 38772 KB |
w7.txt | AC | 249 ms | 36884 KB |
w8.txt | AC | 226 ms | 36608 KB |
w9.txt | AC | 200 ms | 37376 KB |