src2020/1837_bestway/bestway.cpp

52 lines
1.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <iostream>
#include<cstdio>
using namespace std;
double bi(double a,double b){
if(a==0) return b;
if(b==0) return a;
return a<b? a:b;
}
int main()
{
int n, m, a[101][101] = { 0 },ge[101]={0};//a[i][j] i-->j 的直飞路径,已用优惠券数
double f[101][21] = { 0 };//距离1最短距离已用优惠券数
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
}
}
for (int i = 1; i <= n; i++) {
f[i][0] = a[1][i];
f[i][1] = a[1][i] / 2;
ge[i]=1;
}
//重复n次保证全部轮过
for (int e = 1; e <= n; e++) {
//当前起点
for (int i = 1; i <= n; i++) {
//当前终点
if (f[i][0] > 0) {
for (int j = 1; j <= n; j++) {
if (a[i][j] > 0) {
if(ge[i]+1<=m) ge[i]++;
for (int k = 0; k <= ge[i]; k++) {
f[j][k]=bi(f[j][k],f[i][k]+a[i][j]);
if(k!=m){
f[j][k+1]=bi(f[j][k+1],f[i][k]+a[i][j]/2);
f[j][k+1]=bi(f[j][k],f[j][k+1]);
}
}
}
}
}
}
}
double ans=f[n][0];
for(int i=1;i<=m;i++){
if(ans>f[n][i]&&f[n][i]!=0){
ans=f[n][i];
}
}
printf("%0.1f",ans);
}