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 el '\n'
#define fi first
#define sc second
#define int ll
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
using namespace std;
using ll=long long;
using ull=unsigned long long;
using ld=long double;
const int mod=1e9+7;
const int N=81;
int n, m, q, k, adj[N][N], ans[N][N], dp[N][N][N];
void sol()
{
cin >> n >> m;
for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) if(i!=j) adj[i][j]=1e9;
for(int i=0; i<=n; i++) for(int j=0; j<=n; j++) for(int l=0;l<=n;l++) dp[i][j][l]=1e9;
for(int i=1; i<=m; i++)
{
int u, v, w;
cin >> u >> v >> w;
adj[u][v]=min(adj[u][v], w);
}
for(int i=1; i<=n; i++) dp[0][i][i]=0;
for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) dp[1][i][j] = adj[i][j];
cin >> k >> q;
for(int i=2; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
for(int x=1; x<=n; x++)
{
for(int y=1; y<=n; y++)
{
dp[i][j][x]=min(dp[i][j][x], dp[i-1][j][y]+adj[y][x]);
}
}
}
}
for(int x=1; x<=n; x++)
{
for(int y=1; y<=n; y++)
{
if(x!=y) ans[x][y]=1e9;
for(int i=0; i<=min(n, k); i++)
{
ans[x][y]=min(ans[x][y], dp[i][x][y]);
}
}
}
while(q--)
{
int u, v;
cin >> u >> v;
if(ans[u][v]==1e9) cout << -1 << el;
else cout << ans[u][v] << el;
}
}
signed main()
{
// freopen("divisor.INP", "r", stdin);
// freopen("divisor.OUT", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
int t=1;
//cin >> t;
while(t--)
{
sol();
}
}
# | 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... |