#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define x first
#define y second
#define ii pair<int,int>
const int inf=1e9+7;
vector<vector<ii> >a;
int n,m;
struct node
{
int x,used,value;
node(int _value=0,int _x=0,int _used=0)
{
x=_x,used=_used,value=_value;
}
};
bool minimize(int &a,int b)
{
if(a>b)
{
a=b;
return true;
}
return false;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
a.resize(n+7);
for(int i=1;i<=m;i++)
{
int x,y,value;
cin>>x>>y>>value;
a[x].push_back({y,value});
}
int q,k;
cin>>k>>q;
vector<vector<int> >dp(n+1,vector<int>(min(k+1,m+1),inf));
while(q--)
{
int node_x,node_y;
cin>>node_x>>node_y;
for(int i=1;i<=min(k,m);i++)
{
for(int node=1;node<=n;node++)dp[node][i]=inf;
}
dp[node_x][0]=0;
queue<node>q;
q.push({0,node_x,0});
while(!q.empty())
{
int value=q.front().value;
int x=q.front().x;
int used=q.front().used;
q.pop();
if(used==min(k,m))continue;
if(dp[x][used]!=value)continue;
for(ii u:a[x])
{
int node=u.x;
int cost=u.y;
if(minimize(dp[node][used+1],dp[x][used]+cost))
{
q.push({dp[node][used+1],node,used+1});
}
}
}
int ans=inf;
for(int i=0;i<=min(k,m);i++)ans=min(ans,dp[node_y][i]);
cout<<(ans==inf?-1:ans)<<endl;;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |