Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <queue>
- #include <set>
- using namespace std;
- long long INF = LLONG_MAX / 3;
- int n, m;
- vector<vector<pair<int, long long>>> v;
- vector<long long> d;
- vector<bool> used;
- int main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0); cout.tie(0);
- cin >> n >> m;
- v.resize(n + 1);
- d.resize(n + 1);
- used.resize(n + 1);
- for (int _ = 0; _ < m; ++_) {
- int a, b; long long c;
- cin >> a >> b >> c;
- if (a == b) continue;
- else v[a].push_back({ b, c });
- }
- int t; cin >> t;
- for (int _ = 0; _ < t; ++_) {
- int s, f;
- cin >> s >> f;
- long long k = 0;
- fill(d.begin(), d.end(), INF);
- fill(used.begin(), used.end(), false);
- d[s] = 0;
- queue<int> q1 = {}, q2 = {}, q3 = {};
- q1.push(s);
- int cnt = 0;
- for (int step = 0; step < n; ++step) {
- while (!q1.empty()) {
- int start = q1.front();
- q1.pop();
- if (!used[start]) {
- used[start] = true;
- for (auto p : v[start]) {
- ++cnt;
- if (!used[p.first] && d[start] + p.second < d[p.first]) {
- d[p.first] = d[start] + p.second;
- if (d[p.first] >= k + 2000) {
- q3.push(p.first);
- }
- else {
- q2.push(p.first);
- }
- }
- }
- }
- }
- k += 1000;
- q1 = q2;
- q2 = q3;
- q3 = {};
- }
- cout << cnt << "\n";
- cout << ((d[f] == INF) ? -1 : d[f]) << "\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement