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;
#define ll long long
#define x first
#define y second
#define ii pair<ll,ll>
const ll inf=1e16+7;
const ll M=100;
ll dp[M][M][M];
vector<vector<ii> >a;
ll n,m;
struct node
{
ll x,used,value;
node(ll _value=0,ll _x=0,ll _used=0)
{
x=_x,used=_used,value=_value;
}
};
ll calc(ll path,ll x,ll y)
{
if(x==y)return 0;
if(!path)return inf;
if(dp[path][x][y]!=-1)return dp[path][x][y];
ll ans=inf;
for(ii u:a[x])
{
ll node=u.x;
ll cost=u.y;
ans=min(ans,calc(path-1,node,y)+cost);
}
dp[path][x][y]=ans;
return ans;
}
bool minimize(ll &a,ll b)
{
if(a>b)
{
a=b;
return true;
}
return false;
}
int main()
{
//freopen("test.inp","r",stdin);
//freopen("test.out","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
a.resize(n+7);
for(ll i=1;i<=m;i++)
{
ll x,y,value;
cin>>x>>y>>value;
a[x].push_back({y,value});
}
ll q,k;
cin>>k>>q;
memset(dp,-1,sizeof(dp));
while(q--)
{
ll node_x,node_y;
cin>>node_x>>node_y;
ll ans=calc(min(k,n-1),node_x,node_y);
cout<<(ans>=inf?-1:ans)<<endl;
}
}
# | 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... |