挖地雷
传送门:https://www.luogu.org/problemnew/show/P2196、
逆推
#include<cstdio> using namespace std; int n,f[25],c[25],a[25],x,y,t,k,ans,map[25][25]; int main() { scanf("%d",&n); for(int i = 1;i <= n;i++) scanf("%d",&a[i]); for(int i = 1;i < n;i++) for(int j = i+1;j <= n;j++) scanf("%d",&map[i][j]); f[n] = a[n]; for(int i = n - 1;i >= 1;i--) { t = 0,k = 0; for(int j = i+1;j <= n;j++) { if(map[i][j] && f[j] > t) { t = f[j];//记录最大值 k = j;//记录路径 } } f[i] = t + a[i];//更新最大值 c[i] = k; //保存路径 } k = 1; for(int j = 2;j <= n;j++) { if(f[j] > f[k]) k = j;//找到从哪个结点开始挖能获得最大数地雷 } ans = f[k]; printf("%d",k); k = c[k];//开始找路径 while(k != 0) { printf(" %d",k); k = c[k]; } printf("\n%d",ans); return 0; }