src2020/1837_bestway/bestway.cpp

52 lines
1.5 KiB
C++
Raw Normal View History

2020-03-20 17:40:39 +08:00
#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 <20><>ֱ<EFBFBD><D6B1>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ż<EFBFBD>ȯ<EFBFBD><C8AF>
double f[101][21] = { 0 };//<2F><><EFBFBD><EFBFBD>1<EFBFBD><31><EFBFBD>̾<EFBFBD><CCBE><EFBFBD><EBA3AC><EFBFBD><EFBFBD><EFBFBD>Ż<EFBFBD>ȯ<EFBFBD><C8AF>
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;
}
//<2F>ظ<EFBFBD>n<EFBFBD>α<EFBFBD>֤ȫ<D6A4><C8AB><EFBFBD>ֹ<EFBFBD>
for (int e = 1; e <= n; e++) {
//<2F><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>
for (int i = 1; i <= n; i++) {
//<2F><>ǰ<EFBFBD>յ<EFBFBD>
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);
}