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;
typedef long long ll;
#define endl '\n'
int edge[71][71];
vector<pair<int,ll>> adj[71];
ll dp[71][71][70*70+1];
ll solve(int i,int j,int k){
if(k == 0) return i == j ? 0 : 1e18;
ll &ret = dp[i][j][k];
if(~ret) return ret;
ret = solve(i,j,k-1);
for(auto [ch,c] : adj[i]) ret = min(ret,solve(ch,j,k-1)+c);
return ret;
}
void solve(){
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) edge[i][j] = 1e9;
for(int i=0;i<m;i++){
int a,b,c;
cin>>a>>b>>c;
edge[a][b] = min(edge[a][b] , c);
}
int cnt = 0;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++) {
if(edge[i][j] != 1e9) adj[i].push_back({j,edge[i][j]}) , cnt++;
}
}
int k,q;
cin>>k>>q;
k = min(k,cnt);
memset(dp,-1,sizeof(dp));
while(q--){
int a,b;
cin>>a>>b;
ll ans = solve(a,b,k);
cout<<(ans == 1e18 ? -1 : ans)<<endl;
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t=1;
//cin>>t;
while(t--){
solve();
}
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'll solve(int, int, int)':
Main.cpp:13:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
13 | for(auto [ch,c] : adj[i]) ret = min(ret,solve(ch,j,k-1)+c);
| ^
# | 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... |