Submission #526941

#TimeUsernameProblemLanguageResultExecution timeMemory
526941oneloveforeverAutobus (COCI22_autobus)C++14
70 / 70
195 ms8292 KiB
#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];
ll dist[M][M];
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(int node=1;node<=n;node++)
    {
        if(dist[x][node]!=inf)ans=min(ans,calc(path-1,node,y)+dist[x][node]);
    }
    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;

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)dist[i][j]=inf;
    }
    for(int i=1;i<=n;i++)dist[i][i]=0;
    for(ll i=1;i<=m;i++)
    {
        ll x,y,value;
        cin>>x>>y>>value;
        minimize(dist[x][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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...