标签:space ble cout 处理 i++ tps memset 请求 mem
好题啊!~
设f[i][j][k][l]表示已经处理完前i个请求后,a在j,b在k,c在l的最小值是多少;
那么f[i][p[i]][k][l]=min(f[i][p[i]][k][l],f[i-1][j][k][l]+c(j,p[i]));
f[i][j][p[i]][l]=min(f[i][j][p[i]][l],f[i-1][j][k][l]+c(k,p[i]));
f[i][j][k][p[i]]=min(f[i][j][k][p[i]],f[i-1][j][k][l]+c(l,p[i]));
但以上的复杂度卡死你都过不去,所以优化状态;
因为对于i,肯定有一个人处于p[i];所以把p[i]舍弃
f[i+1][j][k]=min(f[i+1][j][k],f[i][j][k]+a[p[i]][p[i+1]]);
f[i+1][p[i]][k]=min(f[i+1][p[i]][k],f[i][j][k]+a[j][p[i+1]]);
f[i+1][j][p[i]]=min(f[i+1][j][p[i]],f[i][j][k]+a[k][p[i+1]]);
注意p[0]=3;
f[0][1][2]=0;
#include <bits/stdc++.h> using namespace std; int a[1010][1010]; int f[1010][210][210]; int p[10010]; int main() { int t; cin>>t; while(t--){ int l,n; cin>>l>>n; for(register int i=1;i<=l;i++){ for(register int j=1;j<=l;j++){ scanf("%d",&a[i][j]); } } for(int i=1;i<=n;i++){ scanf("%d",&p[i]); } memset(f,0x3f,sizeof(f)); f[0][1][2]=0; p[0]=3; for(register int i=0;i<n;i++){ for(register int j=1;j<=l;j++){ for(register int k=1;k<=l;k++){ if(j==p[i]) continue; if(k==p[i]) continue; if(j==k) continue; if(j!=p[i+1]&&k!=p[i+1]) f[i+1][j][k]=min(f[i+1][j][k],f[i][j][k]+a[p[i]][p[i+1]]); if(p[i]!=p[i+1]&&k!=p[i+1]) f[i+1][p[i]][k]=min(f[i+1][p[i]][k],f[i][j][k]+a[j][p[i+1]]); if(j!=p[i+1]&&p[i]!=p[i+1]) f[i+1][j][p[i]]=min(f[i+1][j][p[i]],f[i][j][k]+a[k][p[i+1]]); } } } int minn=INT_MAX; for(int i=1;i<=l;i++){ for(int j=1;j<=l;j++){ minn=min(minn,f[n][i][j]); } } cout<<minn<<endl; } }
SPOJ 703 SERVICE - Mobile Service 题解
标签:space ble cout 处理 i++ tps memset 请求 mem
原文地址:https://www.cnblogs.com/kamimxr/p/11453518.html