#include <bits/stdc++.h>
//#include "includeall.h"
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define endl '\n'
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define input(x) scanf("%lld", &x);
#define input2(x, y) scanf("%lld%lld", &x, &y);
#define input3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z);
#define input4(x, y, z, a) scanf("%lld%lld%lld%lld", &x, &y, &z, &a);
#define print(x, y) printf("%lld%c", x, y);
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x,y) cerr << #x << " is " << x << " " << #y << " is " << y << endl;
#define show3(x,y,z) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << endl;
#define all(x) x.begin(), x.end()
#define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
#define FOR(i, x, n) for (ll i =x; i<=n; ++i)
#define RFOR(i, x, n) for (ll i =x; i>=n; --i)
#pragma GCC optimize("O3","unroll-loops")
//#pragma GCC target("popcnt")
using namespace std;
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
//#define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
typedef long long ll;
typedef long double ld;
typedef pair<ld, ll> pd;
typedef pair<string, ll> psl;
typedef pair<ll, ll> pi;
typedef pair<ll, pi> pii;
typedef pair<pi, pi> piii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll const INF = 1e15;
ll n, m , k, q;
ll adj[100][100], dist[100][100][100], ans[100][100];
void dijkstra(ll src)
{
for (ll i=1; i<=n; ++i) for (ll j=0; j<=k; ++j) dist[src][i][j] = INF;
dist[src][src][0] = 0;
priority_queue<pii, vector<pii>, greater<pii> > pq;
pq.push(mp(0, mp(src, 0)));
while (pq.size())
{
ll d = pq.top().f, c = pq.top().s.f, t = pq.top().s.s;
pq.pop();
if (d > dist[src][c][t]) continue;
if (ans[src][c]==-1) ans[src][c] = d;
if (t==k) continue;
for (ll i=1; i<=n; ++i)
{
if (dist[src][i][t + 1] > d + adj[c][i])
{
dist[src][i][t + 1] = d + adj[c][i];
pq.push(mp(dist[src][i][t + 1], mp(i, t + 1)));
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n >> m;
for (ll i=1; i<=n; ++i) for (ll j=1; j<=n; ++j) {adj[i][j] = ((i==j)?0:INF); ans[i][j] = ((i==j)?0:-1);}
for (ll i=0; i<m; ++i)
{
ll a, b, v; cin >> a >> b >> v;
adj[a][b] = min(adj[a][b], v);
}
cin >> k >> q;
k = min(k, n);
for (ll i=1; i<=n; ++i) dijkstra(i);
while (q--)
{
ll a, b;
cin >> a >> b;
cout << ans[a][b] << endl;
}
return 0;
}
# | 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... |