# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1077446 | vjudge1 | Toll (BOI17_toll) | C++17 | 225 ms | 65772 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
void FastIO(string name = "")
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
if (name.empty() == 0)
{
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
}
void solve()
{
int k, n, m, o;
cin >> k >> n >> m >> o;
vector<vector<pair<int, int>>> nei(n);
for (int i = 1; i <= m; i++)
{
int a, b, t;
cin >> a >> b >> t;
nei[a].push_back({b, t});
}
int lg = log2(n);
const long long inf = 1e11;
vector table(n, vector(lg + 1, vector(5, inf)));
for (int i = 0; i < n; i++)
{
for (auto &[to, toll] : nei[i])
{
table[i][0][to % k] = toll;
}
}
for (int bit = 1; bit <= lg; bit++)
{
for (int i = 0; i < n; i++)
{
int jump = (1 << (bit - 1));
int ind = (i / k + jump ) * k ;
for (int rem = 0; rem < k; rem++)
{
if (ind >= n)
break;
for (int j = 0; j < k; j++)
table[i][bit][j] = min(
table[i][bit-1][rem]+table[ind][bit - 1][j],
table[i][bit][j]);
ind++;
}
}
}
for (int q = 1; q <= o; q++)
{
int a, b;
cin >> a >> b;
if (a == b)
{
cout << 0 << endl;
}
else if (b/k <= a/k)
{
cout << -1 << endl;
}
else
{
int length = (b / k) - (a / k);
vector<pair<int, long long>> node{{a, 0}};
for (int bit = 0; bit <= lg; bit++)
if (length >> bit & 1)
{
map<int, long long> dis;
for (auto &[at, d] : node)
{
int ind = (at / k + (1<<bit) ) * k ;
for (int re = 0; re < k; re++)
{
if (dis.count(ind))
dis[ind] = min(dis[ind], d + table[at][bit][re]);
else
dis[ind] = d + table[at][bit][re];
ind++;
}
}
node.clear();
for (auto &e : dis)
node.push_back(e);
}
long long ans = 2e13;
for (auto &[at, ds] : node)
if (at == b)
{
ans = ds;
}
cout << (ans >= (int)1e9 ? -1 : ans) << endl;
}
}
}
int main()
{
FastIO("");
int t = 1;
//cin >> t;
for (int i = 0; i < t; i++)
{
solve();
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |