当前位置: 首页 > news >正文

P2196 [NOIP 1996 提高组] 挖地雷

P2196 [NOIP 1996 提高组] 挖地雷 - 洛谷

题目描述

在一个地图上有N(N ≤ 20)个地窖,每个地窖中埋有一定数量的地雷。同时,给出地窖之间的连接路径。当地窖及其连接的数据给出之后,某人可以从任一处开始挖地雷,然后可以沿着指出的连接往下挖(仅能选择一条路径),当无连接时挖地雷工作结束。设计一个挖地雷的方案,使某人能挖到最多的地雷。

输入格式

有若干行。

  • 第1行只有一个数字,表示地窖的个数N。
  • 第2行有N个数,分别表示每个地窖中的地雷个数。
  • 第3至第N + 1行表示地窖之间的连接情况:
    • 第3行有n - 1个数(0或1),表示第一个地窖至第2个、第3个…第n个地窖有否路径连接。如第3行为11000…0,则表示第1个地窖至第2个地窖有路径,至第3个地窖有路径,至第4个地窖、第5个…第n个地窖没有路径。
    • 第4行有n - 2个数,表示第二个地窖至第3个、第4个…第n个地窖有否路径连接。
    • ……
    • 第n + 1行行1个数,表示第n - 1个地窖至第n个地窖至第n个地窖有否路径连接。(为0表示没有路径,为1表示有路径)

输出格式

  • 第一行表示挖得最多地雷时的挖地雷的顺序,各地窑序号间以一个空格分隔,不得有多余的空格。
  • 第二行只有一个数,表示能挖到的最多地雷数。

输入输出样例

输入 #1输出 #1
5
10 8 4 7 6
1 1 0 0
0 1
1
1
1 3 4 5
27

说明/提示

【题目来源】
NOIP 1996 提高组第三题

思路:

状态定义

设 dp[i] 表示以 节点 i 为终点 的路径能挖到的最大地雷数。

  • 对于节点 i,其路径可以是:
    • 单独以 i 为起点,此时地雷数为 cnt[i]
    • 从某个前驱节点 jj < i 且 attached[j][i] = 1)出发,经过路径 j→i,此时地雷数为 dp[j] + cnt[i]

状态转移方程

代码:

记忆化搜索:

#include<bits/stdc++.h>
using namespace std;
int cnt[35], mem[35], pre[35];  // mem 存储记忆化结果,pre 记录前驱
int attached[35][35];// 记忆化搜索函数:返回以 x 为终点的最大地雷数
int dfs(int x) 
{if (mem[x] != -1) return mem[x];  mem[x] = cnt[x];  // 初始值:至少包含自己的地雷数for (int j = 1; j < x; j++) // 遍历所有前驱节点(j < x) {     if (attached[j][x]) // 存在 j->x 的路径 {        int current = dfs(j) + cnt[x];  // 计算从 j 到 x 的总地雷数if (current > mem[x])  // 更新最大值和前驱 {  mem[x] = current;pre[x] = j;}}}return mem[x];
}
void print(int x) 
{if (pre[x] != 0) print(pre[x]);  cout << x << " ";
}
int main() 
{int n;cin >> n;memset(mem, -1, sizeof(mem));for (int i = 1; i <= n; i++) {cin >> cnt[i];pre[i] = 0;}for (int i = 1; i <= n - 1; i++) {for (int j = i + 1; j <= n; j++) {int temp;cin >> temp;attached[i][j] = temp;}}int max_val = -1, end_node = 1;for (int i = 1; i <= n; i++) {int current = dfs(i);  // 计算以 i 为终点的最大地雷数if (current > max_val) {max_val = current;end_node = i;}}print(end_node);cout << endl << max_val << endl;return 0;
}

dp:

#include<bits/stdc++.h>
using namespace std;
int cnt[30],dp[30],pre[30];//设 dp[i] 表示以 节点 i 为终点 的路径能挖到的最大地雷数。pre[30]记录前驱结点 
int attached[30][30];
void dfs(int x)
{if(pre[x] != 0)dfs(pre[x]);cout << x << " ";//可以输出最后一个元素 
}
int main(void)
{int n;cin >> n;for(int i = 1 ; i <= n ; i++){cin >> cnt[i];pre[i] = 0;//初始化前驱节点 }for(int i = 1 ; i <= n-1; i++){for(int j = i + 1 ; j <= n ; j++){int temp;cin >> temp;attached[i][j] = temp;}}for(int i = 1 ; i <= n ; i++)dp[i] = cnt[i];for(int i = 1 ; i <= n ; i++){for(int j = 1 ; j < i ; j++){if(attached[j][i]){if(dp[j] + cnt[i] > dp[i]){dp[i] = dp[j] + cnt[i];pre[i] = j;	}	}	}	}int max_val = -1, end_node = 1;for(int i = 1; i <= n; i++) {if(dp[i] > max_val) {max_val = dp[i];end_node = i;}}dfs(end_node);cout << endl;cout << max_val << endl;return 0;
}
#include<bits/stdc++.h>
using namespace std;
int cnt[30],dp[30],pre[30];//设 dp[i] 表示以 节点 i 为终点 的路径能挖到的最大地雷数。pre[30]记录前驱结点 
int attached[30][30];
void dfs(int x)
{if(pre[x] == 0){cout << x << " ";return;}dfs(pre[x]);cout << x << " ";
}
int main(void)
{int n;cin >> n;for(int i = 1 ; i <= n ; i++){cin >> cnt[i];pre[i] = 0;//初始化前驱节点 }for(int i = 1 ; i <= n-1; i++){for(int j = i + 1 ; j <= n ; j++){int temp;cin >> temp;attached[i][j] = temp;}}for(int i = 1 ; i <= n ; i++)dp[i] = cnt[i];for(int i = 1 ; i <= n ; i++){for(int j = 1 ; j < i ; j++){if(attached[j][i]){if(dp[j] + cnt[i] > dp[i]){dp[i] = dp[j] + cnt[i];pre[i] = j;	}	}	}	}int max_val = -1, end_node = 1;for(int i = 1; i <= n; i++) {if(dp[i] > max_val) {max_val = dp[i];end_node = i;}}dfs(end_node);cout << endl;cout << max_val << endl;return 0;
}

相关文章:

  • Python爬虫基础总结
  • 【算法】动态规划专题一 斐波那契数列模型 1-4
  • SQL基础全面指南:从CRUD操作到高级特性实战
  • GC9D01 和 GC9A01两种TFT 液晶显示驱动芯片
  • IntelliJ IDEA
  • Socat 用法详解:网络安全中的瑞士军刀
  • 依赖倒置原则
  • Kotlin 基础
  • 软件性能测试报告:办公软件性能如何满足日常工作需求?
  • 文章一《人工智能学习框架入门指南》
  • Paddle Serving|部署一个自己的OCR识别服务器
  • 【算法滑动窗口】最大的连续1的个数III
  • 一种快速计算OTA PSRR的方法(Ⅱ)
  • ARM架构详解:定义、应用及特点
  • Qt结构体运算符重载指南
  • 爱胜品ICSP YPS-1133DN Plus黑白激光打印机报“自动进纸盒进纸失败”处理方法之一
  • patch命令在代码管理中的应用
  • 【论文速递】2025年09周 (Robotics/Embodied AI/LLM)
  • RAG技术完全指南(三):LlamaIndex架构解析与私有知识库搭建
  • 【五一培训】Day 2
  • 新加坡国会选举投票抽样结果公布,执政党已获超半数议席
  • 人民日报评论员:把造福人民作为根本价值取向
  • 经济日报:仅退款应平衡各方权益
  • 扬州市中医院“药膳面包”走红,内含党参、黄芪等中药材
  • 全文丨中华人民共和国传染病防治法
  • 宁波市纪委监委通报4起违反中央八项规定精神典型问题