#include<bits/stdc++.h>
using namespace std;
// #define int long long
typedef long long ll;
const int inf=1e9;
const int N=85;
int n,k,m,val[N][N];
int dp[N][N][N][N];
int dfs(int l,int r,int pos,int k){
if(dp[l][r][pos][k]!=-1) return dp[l][r][pos][k];
if(k==1) return dp[l][r][pos][k]=0;
int res=1e9;
for(int i=1;i<=n;i++){
if(i>l&&i<r&&val[pos][i]<inf){
int L=l,R=r;
if(i>pos) L=pos;else R=pos;
res=min(res,dfs(L,R,i,k-1)+val[pos][i]);
}
}
return dp[l][r][pos][k]=res;
}
int main(){
scanf("%d%d%d",&n,&k,&m);
memset(val,0x3f,sizeof val);
memset(dp,-1,sizeof dp);
for(int i=1;i<=m;i++){
int a,b,c;scanf("%d%d%d",&a,&b,&c);
val[a][b]=min(val[a][b],c);
}
int res=1e9;
for(int i=1;i<=n;i++) res=min(res,dfs(0,n+1,i,k));
cout<<(res==1e9?-1:res)<<endl;
}
Tinkoff Challenge - Elimination Round D. Presents in Bankopolis
于 2022-05-01 16:14:59 首次发布