#include<bits/stdc++.h>
using namespace std;
#define mems(a, v) memset(a, v, sizeof(a))
#define all(v) v.begin(), v.end()
#define f first
#define sc second
#define pb push_back
#define mp make_pair
#define speed() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pi 3.14159265358979323846
#define BFS_BLACK 1
#define BFS_WHITE -1
#define BFS_GREY 2
#define DFS_BLACK 1
#define DFS_WHITE -1
#define DFS_GREY 2
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<pair<ll,ll>> vii;
typedef vector<vi> vvi;
const ll LOG = 17;
const ll maxn = 5e5 + 5;
const ll mod = 1e9 + 7;
const ll inf = 1e9;
struct Matrix
{
ll a[5][5];
Matrix()
{
for(ll i=0; i<5; i++)
{
for (ll j=0; j<5; j++)
{
a[i][j] = inf;
}
}
}
};
Matrix up[maxn][18];
Matrix calc(Matrix x, Matrix y)
{
Matrix c;
for (ll i=0; i<5; i++)
{
for (ll j=0; j<5; j++)
{
c.a[i][j] = inf;
for (ll k=0; k<5; k++)
{
c.a[i][j] = min(c.a[i][j], x.a[i][k] + y.a[k][j]);
}
}
}
return c;
}
signed main()
{
ll k, n, m, o;
cin >> k >> n >> m >> o;
for(ll i=0; i<m; i++)
{
ll u, v, w;
cin >> u >> v >> w;
up[u/k][0].a[u%k][v%k] = w;
}
for(ll i=1; (1<<i)<=n; i++)
{
for(ll j=0; j+(1<<i)-1<=n; j++)
{
up[j][i] = calc(up[j][i-1], up[j+(1<<(i-1))][i-1]);
}
}
for(ll i=0; i<o; i++)
{
ll a, b;
cin >> a >> b;
Matrix res;
for(ll j=0; j<5; j++)
{
res.a[j][j] = 0;
}
if(a == b)
{
cout << 0 << endl;
continue;
}
ll div1 = a/k, div2 = b/k;
ll dist = div2 - div1;
for(ll j=16; j>=0; j--)
{
if(dist & (1 << j))
{
res = calc(res, up[div1][j]);
div1 += (1 << j);
}
}
if(res.a[a%k][b%k] == inf)
{
cout << -1 << endl;
}
else
{
cout << res.a[a%k][b%k] << endl;
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
230 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
214 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
228 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
228 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
230 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |