#include <bits/stdc++.h>
using namespace std;
const int maxn = 50005;
const int inf = 1000000007;
int n, m, q, k;
vector<pair<int, int>> g[maxn];
vector<pair<int, int>> rev[maxn];
struct query
{
int l;
int r;
int id;
};
query queries[maxn];
int answer[maxn];
int dist[maxn];
void calc(int source, int l, int r)
{
priority_queue<pair<int, int>> pq;
pq.push(make_pair(0, source));
dist[source] = 0;
while (!pq.empty())
{
int node = pq.top().second;
int curr = -pq.top().first;
pq.pop();
for (int i = 0; i < g[node].size(); ++i)
{
if (dist[g[node][i].first] > curr + g[node][i].second && g[node][i].first / k <= r)
{
dist[g[node][i].first] = curr + g[node][i].second;
pq.push(make_pair(-dist[g[node][i].first], g[node][i].second));
}
}
}
while (!pq.empty())
{
pq.pop();
}
pq.push(make_pair(0, source));
dist[source] = 0;
while (!pq.empty())
{
int node = pq.top().second;
int curr = -pq.top().first;
pq.pop();
for (int i = 0; i < rev[node].size(); ++i)
{
if (dist[rev[node][i].first] > curr + rev[node][i].second && rev[node][i].first / k >= l)
{
dist[rev[node][i].first] = curr + rev[node][i].second;
pq.push(make_pair(-dist[rev[node][i].first], rev[node][i].second));
}
}
}
}
void divide_and_conquer(int l, int r, vector<int> indexes)
{
if (l >= r || indexes.empty())
{
return;
}
int mid = (l + r) / 2;
vector<int> left_part;
vector<int> right_part;
for (int node_num = mid * k; node_num <= min(n, (mid + 1) * k - 1); ++node_num)
{
for (int i = 0; i < n; ++i)
{
dist[i] = inf;
}
calc(node_num, l, r);
for (int i = 0; i < indexes.size(); ++i)
{
int node_l = queries[indexes[i]].l;
int node_r = queries[indexes[i]].r;
int query_idx = queries[indexes[i]].id;
if (node_l / k <= mid && mid <= node_r / k)
{
if (dist[node_l] != inf && dist[node_r] != inf)
{
answer[query_idx] = min(answer[query_idx], dist[node_l] + dist[node_r]);
}
}
else if (node_r / k < mid)
{
left_part.push_back(indexes[i]);
}
else
{
right_part.push_back(indexes[i]);
}
}
}
divide_and_conquer(l, mid, left_part);
divide_and_conquer(mid + 1, r, right_part);
}
void fastIO()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main()
{
fastIO();
cin >> k >> n >> m >> q;
for (int i = 1; i <= m; ++i)
{
int x, y, w;
cin >> x >> y >> w;
g[x].push_back(make_pair(y, w));
rev[y].push_back(make_pair(x, w));
}
vector<int> indexes;
for (int i = 1; i <= q; ++i)
{
cin >> queries[i].l >> queries[i].r;
queries[i].id = i;
indexes.push_back(i);
answer[i] = inf;
}
int groups = n / k;
divide_and_conquer(0, groups, indexes);
for (int i = 1; i <= q; ++i)
{
if (answer[i] == inf)
{
cout << "-1\n";
}
else
{
cout << answer[i] << "\n";
}
}
return 0;
}
Compilation message
toll.cpp: In function 'void calc(int, int, int)':
toll.cpp:37:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for (int i = 0; i < g[node].size(); ++i)
| ~~^~~~~~~~~~~~~~~~
toll.cpp:61:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for (int i = 0; i < rev[node].size(); ++i)
| ~~^~~~~~~~~~~~~~~~~~
toll.cpp: In function 'void divide_and_conquer(int, int, std::vector<int>)':
toll.cpp:93:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | for (int i = 0; i < indexes.size(); ++i)
| ~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
25 ms |
6296 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
63 ms |
7304 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
25 ms |
6296 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |