fork download
  1. // ~~ icebear love attttttt ~~
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef pair<int, int> ii;
  7. typedef pair<int, ii> iii;
  8.  
  9. template<class T>
  10. bool minimize(T &a, const T &b) {
  11. if (a > b) return a = b, true;
  12. return false;
  13. }
  14.  
  15. template<class T>
  16. bool maximize(T &a, const T &b) {
  17. if (a < b) return a = b, true;
  18. return false;
  19. }
  20.  
  21. #define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
  22. #define FORR(i,a,b) for(int i=(a); i>=(b); --i)
  23. #define REP(i, n) for(int i=0; i<(n); ++i)
  24. #define RED(i, n) for(int i=(n)-1; i>=0; --i)
  25. #define MASK(i) (1LL << (i))
  26. #define mp make_pair
  27. #define pb push_back
  28. #define fi first
  29. #define se second
  30. #define all(x) x.begin(), x.end()
  31. #define task "icebearat"
  32.  
  33. const int MOD = 1e9 + 7;
  34. const int inf = 1e9 + 27092008;
  35. const ll INF = 1e18 + 27092008;
  36. const int N = 2e5 + 5;
  37. int n, m, H;
  38. int l[N], s[N];
  39. vector<int> G[N];
  40. ll dist[N];
  41.  
  42. void init(void) {
  43. cin >> n >> m >> H;
  44. FOR(i, 1, n) cin >> l[i];
  45. FOR(i, 1, n) cin >> s[i];
  46. FOR(i, 1, m) {
  47. int u, v;
  48. cin >> u >> v;
  49. G[u].pb(v);
  50. G[v].pb(u);
  51. }
  52. }
  53.  
  54. ll extended_euclid(ll a, ll b, ll &x, ll &y) {
  55. if (b == 0) {
  56. x = 1;
  57. y = 0;
  58. return a;
  59. }
  60.  
  61. ll x1, y1;
  62. ll d = extended_euclid(b, a % b, x1, y1);
  63. x = y1;
  64. y = x1 - y1 * (a / b);
  65. return d;
  66. }
  67.  
  68. ll find_weight(int l_u, int s_u, int l_v, int s_v, ll lower) {
  69. // l_u + k * s_u = l_v + k * s_v (mod H) and k > lower
  70. ll a = s_v - s_u, b = H, c = l_u - l_v;
  71. ll x, y;
  72. ll g = extended_euclid(abs(a), abs(b), x, y);
  73.  
  74. if (c % g) return INF;
  75.  
  76. ll x0 = (c / g) * x, y0 = (c / g) * y;
  77. if (a < 0) x0 = -x0;
  78. if (b < 0) y0 = -y0;
  79. // k = x + T * b / g > lower -> T > (lower - x) * g / b
  80. ll T = (ll)ceil(1.0 * (lower - x0) * g / b);
  81. return x0 + T * b / g;
  82. }
  83.  
  84. void process(void) {
  85. priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
  86. FOR(i, 0, n) dist[i] = INF;
  87. dist[1] = -1;
  88. pq.push(mp(-1, 1));
  89. while(!pq.empty()) {
  90. ll du; int u;
  91. tie(du, u) = pq.top(); pq.pop();
  92. if (du != dist[u]) continue;
  93.  
  94. for(int v : G[u]) {
  95. ll w = find_weight(l[u], s[u], l[v], s[v], dist[u]);
  96. if (minimize(dist[v], w)) pq.push(mp(w, v));
  97. }
  98. }
  99. cout << (dist[n] >= dist[0] ? -1 : dist[n] + 1);
  100. }
  101.  
  102. int main() {
  103. ios_base::sync_with_stdio(0);
  104. cin.tie(0); cout.tie(0);
  105. if (fopen(task".inp", "r")) {
  106. freopen(task".inp", "r", stdin);
  107. freopen(task".out", "w", stdout);
  108. }
  109. int tc = 1;
  110. // cin >> tc;
  111. while(tc--) {
  112. init();
  113. process();
  114. }
  115. return 0;
  116. }
  117.  
  118.  
  119.  
Success #stdin #stdout 0.01s 9660KB
stdin
Standard input is empty
stdout
-1