Magic Maze dfs + dp
http://swjtuoj.cn/problem/2387/
设dp[cur]表示以cur这个节点为起点的时候,能走的最大贡献。
题目保证没环,也就是没回路。
感觉和树dp差不多了。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = + ;
struct Node {
int u, v, w;
int tonext;
}e[maxn * maxn];
int first[maxn], num;
void add(int u, int v, int w) {
++num;
e[num].u = u, e[num].v = v, e[num].w = w;
e[num].tonext = first[u];
first[u] = num;
}
int vis[maxn], dp[maxn], DFN;
int dfs(int cur) {
if (vis[cur] == DFN) return dp[cur];
vis[cur] = DFN;
int res = ;
for (int i = first[cur]; i; i = e[i].tonext) {
int v = e[i].v;
res = max(res, dfs(v) + e[i].w);
}
return dp[cur] = res;
}
void work() {
memset(first, , sizeof first);
int n, m;
scanf("%d%d", &n, &m);
for (int i = ; i <= m; ++i) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
u++;
v++;
add(u, v, w);
}
++DFN;
int ans = ;
memset(dp, , sizeof dp);
for (int i = ; i <= n; ++i) {
if (vis[i] == DFN) continue;
dp[i] = dfs(i);
}
for (int i = ; i <= n; ++i) ans = max(ans, dp[i]);
printf("%d\n", ans);
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
int t;
scanf("%d", &t);
while (t--) work();
return ;
}
Magic Maze dfs + dp的更多相关文章
- dfs+dp思想的结合------hdu1078
首先是题目的意思: 从一个正方形的0,0点开始走,只能横着走,竖着走,最多走k步,下一个点的数一定要比当前这个点的值大,每走一步,就加上下一个点的数据,问数据最大能有多少. 首先遇到这种题目,走来走去 ...
- HDU1978How Many Ways 记忆化dfs+dp
/*记忆化dfs+dp dp[i][j]代表达到这个点的所有路的条数,那么所有到达终点的路的总数就是这dp[1][1]加上所有他所能到达的点的 所有路的总数 */ #include<stdio. ...
- 记忆化搜索(DFS+DP) URAL 1223 Chernobyl’ Eagle on a Roof
题目传送门 /* 记忆化搜索(DFS+DP):dp[x][y] 表示x个蛋,在y楼扔后所需要的实验次数 ans = min (ans, max (dp[x][y-i], dp[x-1][i-1]) + ...
- 记忆化搜索(DFS+DP) URAL 1501 Sense of Beauty
题目传送门 /* 题意:给了两堆牌,每次从首部取出一张牌,按颜色分配到两个新堆,分配过程两新堆的总数差不大于1 记忆化搜索(DFS+DP):我们思考如果我们将连续的两个操作看成一个集体操作,那么这个操 ...
- 【cf1111】C. Creative Snap (dfs+dp)
传送门 简单的dfs+dp即可解决.根本不用动态开点 /* * Author: heyuhhh * Created Time: 2019/11/13 10:12:42 */ #include < ...
- CodeForces 628 D Magic Numbers 数位DP
Magic Numbers 题意: 题意比较难读:首先对于一个串来说, 如果他是d-串, 那么他的第偶数个字符都是是d,第奇数个字符都不是d. 然后求[L, R]里面的多少个数是d-串,且是m的倍数. ...
- 滑雪(dfs+dp)
滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 113903 Accepted: 43478 Description ...
- HDU 1978 记忆化搜索(dfs+dp)
Y - How many ways Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- [NOIP1999提高] CODEVS 1047 邮票面值设计(dfs+dp)
dfs出邮票的各种面值,然后dp求解. ------------------------------------------------------------------------------- ...
随机推荐
- SpringBoot_Exception_01_No plugin found for prefix 'spring-boto' in the current project
一.异常现象 spingbott项目在eclipse中执行maven命令:spring-boot:run, 出现异常: No plugin found for prefix 'spring-boto' ...
- 勤于思考:IE10不支持检测IE6的代码
这句话 var isIE6 = isIE && ([/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1] == 6); 在IE6~9都没问题 ...
- [acm]HDOJ 3082 Simplify The Circuit
题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3082 字符串处理+并联电阻公式 //11481261 2014-08-18 16:52:47 Acc ...
- ACM学习历程—HDU4725 The Shortest Path in Nya Graph(SPFA && 优先队列)
Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...
- Mesos的quorum配置引发的问题
Mesos安装完毕后,发现agent无法和master关联(通过WebUI的agent页面无法看到agent信息),查看日志显示: Elected as the leading master! sta ...
- bzoj1055玩具取名——区间DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1055 区间DP,注意初始化!! 因为没记忆化,TLE了一晚上,区间DP尤其要注意不重复递归! ...
- matlab形态学图像处理之strel函数
转自:http://blog.sina.com.cn/s/blog_b1cd5d330101pmwi.html strel--structuring element 运用各种形状和大小构造元素,基本语 ...
- 按钮交互loading ---- 转圈圈 加载
按钮loading状态自定义选项(功能): 可以在元素上添加 data-am-loading 来设置选项: spinner 加载动画图标,适用于支持 CSS3 动画.非 input 元素,写图标名称即 ...
- Swoole 协程 MySQL 客户端与异步回调 MySQL 客户端的对比
Swoole 协程 MySql 客户端与 异步回调 MySql 客户端的对比 为什么要对比这两种不同模式的客户端? 异步 MySQL 回调客户端是虽然在 Swoole 1.8.6 版本就已经发布了, ...
- gridview把textbox的值修改还是旧值的解决方法
解决方法很简单,加上if(!IsPostBack) 就OK了,因为之前加载之前都会调用InitData(). protected void Page_Load(object sender, Event ...