#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 |
1 |
Correct |
4 ms |
8012 KB |
Output is correct |
2 |
Correct |
4 ms |
8012 KB |
Output is correct |
3 |
Correct |
5 ms |
8012 KB |
Output is correct |
4 |
Correct |
3 ms |
8012 KB |
Output is correct |
5 |
Correct |
4 ms |
8012 KB |
Output is correct |
6 |
Correct |
5 ms |
8140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
8140 KB |
Output is correct |
2 |
Correct |
15 ms |
8176 KB |
Output is correct |
3 |
Correct |
13 ms |
8188 KB |
Output is correct |
4 |
Correct |
12 ms |
8140 KB |
Output is correct |
5 |
Correct |
11 ms |
8140 KB |
Output is correct |
6 |
Correct |
14 ms |
8332 KB |
Output is correct |
7 |
Correct |
15 ms |
8344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8012 KB |
Output is correct |
2 |
Correct |
4 ms |
8012 KB |
Output is correct |
3 |
Correct |
5 ms |
8012 KB |
Output is correct |
4 |
Correct |
3 ms |
8012 KB |
Output is correct |
5 |
Correct |
4 ms |
8012 KB |
Output is correct |
6 |
Correct |
5 ms |
8140 KB |
Output is correct |
7 |
Correct |
17 ms |
8140 KB |
Output is correct |
8 |
Correct |
28 ms |
8140 KB |
Output is correct |
9 |
Correct |
11 ms |
8196 KB |
Output is correct |
10 |
Correct |
29 ms |
8184 KB |
Output is correct |
11 |
Correct |
27 ms |
8268 KB |
Output is correct |
12 |
Correct |
86 ms |
8268 KB |
Output is correct |
13 |
Correct |
648 ms |
17520 KB |
Output is correct |
14 |
Correct |
945 ms |
17324 KB |
Output is correct |
15 |
Execution timed out |
1090 ms |
17256 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8012 KB |
Output is correct |
2 |
Correct |
4 ms |
8012 KB |
Output is correct |
3 |
Correct |
5 ms |
8012 KB |
Output is correct |
4 |
Correct |
3 ms |
8012 KB |
Output is correct |
5 |
Correct |
4 ms |
8012 KB |
Output is correct |
6 |
Correct |
5 ms |
8140 KB |
Output is correct |
7 |
Correct |
12 ms |
8140 KB |
Output is correct |
8 |
Correct |
15 ms |
8176 KB |
Output is correct |
9 |
Correct |
13 ms |
8188 KB |
Output is correct |
10 |
Correct |
12 ms |
8140 KB |
Output is correct |
11 |
Correct |
11 ms |
8140 KB |
Output is correct |
12 |
Correct |
14 ms |
8332 KB |
Output is correct |
13 |
Correct |
15 ms |
8344 KB |
Output is correct |
14 |
Correct |
17 ms |
8140 KB |
Output is correct |
15 |
Correct |
28 ms |
8140 KB |
Output is correct |
16 |
Correct |
11 ms |
8196 KB |
Output is correct |
17 |
Correct |
29 ms |
8184 KB |
Output is correct |
18 |
Correct |
27 ms |
8268 KB |
Output is correct |
19 |
Correct |
86 ms |
8268 KB |
Output is correct |
20 |
Correct |
648 ms |
17520 KB |
Output is correct |
21 |
Correct |
945 ms |
17324 KB |
Output is correct |
22 |
Execution timed out |
1090 ms |
17256 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |