-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblah.cpp
More file actions
75 lines (70 loc) · 2.2 KB
/
blah.cpp
File metadata and controls
75 lines (70 loc) · 2.2 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
#include<bits/stdc++.h>
using namespace std;
const int N = 3020;
queue<pair<int,int> > q;
int main()
{
int n,m,k;
cin >> n >> m >> k;
vector<int> V[n];
int dist[n][n],previous[n][n];
set<tuple<int,int,int> > C;
for (int i = 1; i<=n; i++)
for (int j = 1; j<=n; j++)
{
//for(int k=1;k<=n;k++) C.insert(make_tuple(i-1,j-1,k-1));
dist[i-1][j-1] = INT_MAX;
}
for (int i = 1; i<=m; i++)
{
int x,y;
cin >> x >> y;
V[x-1].push_back(y-1);
V[y-1].push_back(x-1);
}
int u,v,w;
for(int i=0;i<k;i++) { cin>>u>>v>>w; C.insert(make_tuple(u-1,v-1,w-1));}
dist[0][0] = 1;
q.push(make_pair(0,0));
while (!q.empty())
{
int v = q.front().first, u = q.front().second;
q.pop();
for (int i=0;i<V[v].size();i++)
{
w = V[v][i];
if (dist[v][w]>1+dist[u][v] && !C.count(make_tuple(u,v,w)))
{
dist[v][w] = 1+dist[u][v];
previous[v][w] = u;
q.push(make_pair(w,v));
}
}
}
int Min = INT_MAX, poz;
for (int i = 0; i<n; i++)
{ if (dist[i][n-1]<Min)
{
Min = dist[i][n-1];
poz = i;
}
}
if (Min == INT_MAX)
cout << -1;
else
{
cout << Min-1<<"\n";
int v = n-1, u = poz;
stack<int> ans;
ans.push(v);
while (v)
{
//if(v==0) break;
ans.push(u);
int z = u;
u = previous[u][v];
v = z;
}
while(!ans.empty()) {cout<<ans.top()+1<<" "; ans.pop();}
}
}