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
| #include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <cstring> #include <queue> #include <set> #include <vector> #define init(x) memset (x,0,sizeof (x)) #define ll long long #define ull unsigned long long #define INF 0x3f3f3f3f using namespace std; const int MAX = 3005; const int M = 999983; const int MOD = 1e9 + 7; inline int read (); struct tri { int x,y,z; }; int n,m,k,ok,dis[MAX][MAX],pre[MAX][MAX]; vector <int> ve[MAX]; vector <tri> hsh[M]; queue <pair <int,int> > q; int get (int x,int y,int z); void add (int u,int v); bool check (int x,int y,int z); void bfs (); void print (int u,int v); int main () { n = read ();m = read ();k = read (); for (int i = 1;i <= m;++i) { int x = read (),y = read (); ve[x].push_back (y);ve[y].push_back (x); } for (int i = 1;i <= k;++i) { int x = read (),y = read (),z = read (); hsh[get (x,y,z)].push_back ((tri){x,y,z}); } bfs (); if (!ok) puts ("-1"); return 0; } inline int read () { int s = 0;int f = 1; char ch = getchar (); while ((ch < '0' || ch > '9') && ch != EOF) { if (ch == '-') f = -1; ch = getchar (); } while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0'; ch = getchar (); } return s * f; } int get (int x,int y,int z) { return (1ll * x * y * z % M + 1ll * x + 1ll * y + 1ll * z) % M; } bool check (int x,int y,int z) { for (auto tmp : hsh[get (x,y,z)]) if (tmp.x == x && tmp.y == y && tmp.z == z) return 1; return 0; } void bfs () { q.push ({0,1}); while (!q.empty ()) { pair <int,int> x = q.front ();q.pop (); int p = x.first,u = x.second; if (u == n) { printf ("%d\n",dis[p][u]); print (p,u); ok = 1;puts (""); return ; } for (auto v : ve[u]) { if (!check (p,u,v) && !dis[u][v]) { pre[u][v] = p;dis[u][v] = dis[p][u] + 1; q.push ({u,v}); } } } } void print (int u,int v) { if (!v) return ; print (pre[u][v],u); printf ("%d ",v); }
|