求最久时间即在无环有向图里求最远路径

dfs+剪枝优化

从0节点(自己添加的)出发,0到1~n个节点之间的距离为1。mt[i]表示从0点到第i个节点眼下所得的最长路径

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector> using namespace std; const int maxn = 1005;
int map[maxn][maxn];
int mt[maxn];
int N, M, ans; void init()
{
memset(map, 0, sizeof(map));
memset(mt, 0, sizeof(mt));
for(int i = 0; i <= N; i ++)
map[0][i] = 1;
} void dfs(int rt, int time)
{
for(int i = 1; i <= N; i ++)
{
if(map[rt][i] != 0 && time+map[rt][i] > mt[i]) // 剪枝优化部分
{
mt[i] = time+map[rt][i]; // mt[i]表示进行完第i个节点所须要的眼下最久时间
ans = max(ans, mt[i]);
dfs(i, mt[i]);
}
}
} int main()
{
int a, b, c;
while(scanf("%d%d", &N, &M) != EOF)
{
init();
for(int i = 0; i < M; i ++)
{
scanf("%d%d%d", &a, &b, &c);
a ++; b ++;
map[a][b] = c;
}
ans = 0;
dfs(0, 0);
printf("%d\n", ans);
} return 0;
}

hdu 4109 dfs+剪枝优化的更多相关文章

  1. poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)

    Sum It Up Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Sub ...

  2. HDU 5113 dfs剪枝

    题意:告诉格子规格,颜色个数,以及每个颜色能涂得格子数目,问是否能够实现相邻两个格子的颜色数目不相同. 分析:因为数据很小,格子最多是5 * 5大小的,因此可以dfs.TLE了一次之后开始剪枝,31m ...

  3. Tempter of the Bone HDU 1010(DFS+剪枝)

    Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...

  4. 带分数dfs+剪枝优化

    #include<iostream>#include<cstdio>#include<cstdlib>#include<ctime>using name ...

  5. hdu 1518 dfs+剪枝

    题目大意:几根棒子能否组成一个正方形 Sample Input3           //测试组数4 1 1 1 1   //棒子数目以及每根棒子的长度5 10 20 30 40 508 1 7 2 ...

  6. hdu 5887 Herbs Gathering (dfs+剪枝 or 超大01背包)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5887 题解:这题一看像是背包但是显然背包容量太大了所以可以考虑用dfs+剪枝,贪心得到的不 ...

  7. HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)

    Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  8. HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))

    Equation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  9. 搜索(剪枝优化):HDU 5113 Black And White

    Description In mathematics, the four color theorem, or the four color map theorem, states that, give ...

随机推荐

  1. windows系统扩展C盘的工具推荐(解决了C盘和压缩卷不相邻无法扩展C盘问题)

    1.下载分区工具 “分区助手3.0中文版” 下载地址:http://www.33lc.com/soft/14880.html 2.下载下来是一个压缩包,解压后运行安装程序. 3.安装完成后按以下步骤执 ...

  2. C# 插入排序(数据结构与算法)

    1                                                                   }

  3. innobackupex: fatal error: no ‘innodb_buffer_pool_filename’解决方法

    http://www.ttlsa.com/mysql/innobackupex-1-5-1-fatal-error-no-innodb_buffer_pool_filename/

  4. Golang源码探索(三) GC的实现原理

    Golang从1.5开始引入了三色GC, 经过多次改进, 当前的1.9版本的GC停顿时间已经可以做到极短. 停顿时间的减少意味着"最大响应时间"的缩短, 这也让go更适合编写网络服 ...

  5. 最简单的optparse模块的用法

    optparse模块是python自带的模块,可用于处理命令行 #!/usr/bin/env python # -*- coding: utf-8 -*- """ __a ...

  6. 第一章:关于Ehcache

    PDF官方文档:http://www.ehcache.org/generated/2.10.4/pdf/About_Ehcache.pdf 1.什么是Ehcache? Ehcache是一种开源的基于标 ...

  7. 《用Java写一个通用的服务器程序》01 综述

    最近一两年用C++写了好几个基于TCP通信类型程序,都是写一个小型的服务器,监听请求,解析自定义的协议,处理请求,返回结果.每次写新程序时都把老代码拿来,修改一下协议解析部分和业务处理部分,然后就一个 ...

  8. Animation-list,帧动画+属性动画,做出Flash般的效果

    我们会用到PS,即使不会也不要怂,只需要几步傻瓜式操作即可. 属性动画可以看看我另一篇文章:属性动画详解 效果图 相信机智的各位,看完之后一定能发挥创意,做出更酷更炫的效果 图层获取 首先你需要找一张 ...

  9. 项目实战5—企业级缓存系统varnish应用与实战

    企业级缓存系统varnish应用与实战 环境背景:随着公司业务快速发展,公司的电子商务平台已经聚集了很多的忠实粉丝,公司也拿到了投资,这时老板想通过一场类似双十一的活动,进行一场大的促销,届时会有非常 ...

  10. 认识 var、let、const

    我们通过声明.初始化.值的可变性.作用域.变量提升以及在工作中如何使用等多个方面来详细了解var.let.const等关键字功能与特点. 声明 var,let:可以先声明,后赋值(初始化),默认值是 ...