forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathG.cpp
More file actions
120 lines (102 loc) · 3.1 KB
/
G.cpp
File metadata and controls
120 lines (102 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define DEBUG(x) { cerr << #x << " = "; cerr << (x) << endl; }
#define PR(a,n) { cerr << #a << " = "; FOR(_,1,n) cerr << a[_] << ' '; cerr << endl; }
#define PR0(a,n) { cerr << #a << " = "; REP(_,n) cerr << a[_] << ' '; cerr << endl; }
#define sqr(x) ((x) * (x))
#define ll long long
using namespace std;
const int MN = 100111;
vector< pair<int,int> > ke[MN];
int n, m;
bool visited[MN];
int tr[MN], trc[MN], tr2[MN], len[MN];
int start;
vector<int> all;
void init() {
memset(len, 0, sizeof len);
FOR(i,1,n) ke[i].clear();
memset(visited, 0, sizeof visited);
memset(tr2, 0, sizeof tr2);
memset(tr, 0, sizeof tr);
memset(trc, 0, sizeof trc);
start = 0;
all.clear();
}
void trace(int u) {
all.push_back(u);
if (u == n) return ;
if (tr[u]) {
trace(tr[u]);
if (trc[u]) start = 1;
if (start) printf("%d", trc[u]);
}
else {
trace(tr2[u]);
}
}
int q[MN], first, last;
int main() {
while (scanf("%d%d", &n, &m) == 2) {
init();
FOR(i,1,m) {
int u, v, c; scanf("%d%d%d", &u, &v, &c);
++u; ++v;
ke[u].push_back(make_pair(c, v));
ke[v].push_back(make_pair(c, u));
}
FOR(i,1,n) sort(ke[i].begin(), ke[i].end());
first = last = 1;
q[1] = n;
tr2[n] = n;
len[n] = 1;
while (first <= last) {
int u = q[first++];
for(auto e : ke[u]) if (e.first == 0) {
int v = e.second;
if (!tr2[v]) {
len[v] = len[u] + 1;
tr2[v] = u;
q[++last] = v;
}
}
}
queue<vector<int> > qu;
vector<int> t;
FOR(i,1,last) {
t.push_back(q[i]);
visited[q[i]] = true;
}
qu.push(t);
while (!qu.empty()) {
t = qu.front(); qu.pop();
REP(turn,10) {
vector<int> r;
for(int u : t) {
for(auto e : ke[u]) {
int v = e.second, c = e.first;
if (c != turn) continue;
if (!visited[v]) {
visited[v] = true;
tr[v] = u;
trc[v] = c;
r.push_back(v);
}
}
}
if (!r.empty()) qu.push(r);
}
}
assert(visited[1]);
start = 0;
all.clear();
trace(1);
if (!start) puts("0"); else puts("");
printf("%d\n", all.size());
for(int x : all) printf("%d ", x-1); puts("");
// cerr << "-------" << endl;
}
}