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>
using namespace std;
struct points{
int64_t node,dist,kk;
};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,m;cin>>n>>m;
vector<vector<pair<int64_t,int64_t>>>adj(n);
vector<vector<int64_t>>dist(n,vector<int64_t>(n,1e15));
//vector<vector<vector<pair<int64_t,int64_t>>>>arr(n,vector<vector<pair<int64_t,int64_t>>>(n));
for (int i = 0;i<m;++i){
int64_t x,y,z;cin>>x>>y>>z;
--x;
--y;
dist[x][y] = min(dist[x][y],z);
}
int k,q;cin>>k>>q;
k = min(k,n*n - n);
vector<vector<vector<int64_t>>>distt(n,vector<vector<int64_t>>(n,vector<int64_t>(k+1,1e15)));
for (int i = 0;i<n;++i){
for (int j =0;j<n;++j){
if (i!=j){
if (dist[i][j]!=1e15){
adj[i].push_back({j,dist[i][j]});
}
}
}
}
function<void(int)> bfs = [&](int u){
queue<points>q;
q.push({u,0,0});
while(!q.empty()){
auto v = q.front();
q.pop();
for (int i = 1;i<=k;++i){
distt[u][v.node][i] = min(distt[u][v.node][i-1],distt[u][v.node][i]);
}
for (auto x:adj[v.node]){
if (v.kk+1<=k&&distt[u][x.first][v.kk+1]>x.second + v.dist){
distt[u][x.first][v.kk+1] = x.second + v.dist;
if (v.kk+1<k)
q.push({x.first,x.second + v.dist,v.kk + 1});
}
}
}
};
for (int i = 0;i<n;++i){
bfs(i);
}
for (int i = 0;i<q;++i){
int x,y;cin>>x>>y;
--x;
--y;
if (x==y){
cout<<0<<'\n';
continue;
}
int64_t ans = 1e15;
for (int i = 0;i<=k;++i){
ans = min(distt[x][y][i],ans);
}
if (ans==1e15){
cout<<"-1"<<'\n';
}
else cout<<ans<<'\n';
}
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... |