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

求最久时间即在无环有向图里求最远路径 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…
Sum It Up Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 4   Accepted Submission(s) : 1 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Given a specified total t and…
题意:告诉格子规格,颜色个数,以及每个颜色能涂得格子数目,问是否能够实现相邻两个格子的颜色数目不相同. 分析:因为数据很小,格子最多是5 * 5大小的,因此可以dfs.TLE了一次之后开始剪枝,31ms过.剪枝看代码. #include <cstdio> #include <iostream> #include <sstream> #include <cmath> #include <cstring> #include <cstdlib&g…
Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried despe…
#include<iostream>#include<cstdio>#include<cstdlib>#include<ctime>using namespace std;int num=1;int sum=0;int A=1,B=1,C=1; // num=a+b/c ,a,b,c所对应的长度 int abc[11];//储存a,b,c的值 int is[10]={0}; int a=0,b=0,c=0; int power(int x,int y){ i…
题目大意:几根棒子能否组成一个正方形 Sample Input3           //测试组数4 1 1 1 1   //棒子数目以及每根棒子的长度5 10 20 30 40 508 1 7 2 6 4 4 3 5 Sample Output yesnoyes 虽然不用pos直接从0开始枚举也可以有答案,但会超时,加个pos,以前dfs过的情况就不会再出现了,想起以前bc的一道题也是这样 #include<cstdio> #include<iostream> #include&…
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5887 题解:这题一看像是背包但是显然背包容量太大了所以可以考虑用dfs+剪枝,贪心得到的不一定是正确答案.当然这题还可以用背包来写,其实这就用到了dp的一些优化就是存状态,递推过程中有些状态是多余的没必要计算这样就可以大大减少空间的利用和时间的浪费 第一份是dfs+剪枝的写法第二份是背包+map存状态的写法. #include <iostream> #include <cstri…
Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 539    Accepted Submission(s): 204 Problem Description A clique is a complete graph, in which there is an edge between every pair…
Equation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 92    Accepted Submission(s): 24 Problem Description Little Ruins is a studious boy, recently he learned addition operation! He was rewa…
Description In mathematics, the four color theorem, or the four color map theorem, states that, given any separation of a plane into contiguous regions, producing a figure called a map, no more than four colors are required to color the regions of th…
HDU 1501 Zipper [DFS+剪枝] Problem Description Given three strings, you are to determine whether the third string can be formed by combining the characters in the first two strings. The first two strings can be mixed arbitrarily, but each must stay in…
http://acm.hdu.edu.cn/showproblem.php?pid=1010 题意很好理解,不是最短路,而是dfs,虽然地图不算大,稍微注意一点的dfs也能险过,但是700+ms和78ms一对比还是让人难受 对dfs的剪枝处理,实际上就是一个逻辑推理 面对整个地图我可以做出以下判断如果 n * m - wall <= t那就一定say NO n * m 是整个地图的大小,wall是地图中墙的个数相减之后就是最多可以走的步数,之所以取到等号是因为在第0秒的时候,已经走了一个地方了(…
http://acm.hdu.edu.cn/showproblem.php?pid=1010 折磨我一下午 题目大意: 从s点走到d点能否恰好走k步   刚开始以为是广搜,其实是深搜. dfs多优化一下才会过. #include<stdio.h> #include<stdlib.h> #include<algorithm> #include<math.h> #include<string.h> #include<iostream> #…
Herbs Gathering Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 687    Accepted Submission(s): 145 Problem Description Collecting one's own plants for use as herbal medicines is perhaps one of t…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2433 ,最短路(SPFA或优化过的Dijstra) + 剪枝优化 这道题关键还是在几个剪枝上面,没有剪枝会TLE. 先说下几个数组的意义: a[]存储M条边的信息:vis[i]表示点i是否访问过:path[i][u][v]表示在点i的所有最短路里,边u - v是否属于某条最短路:cnt[u][v]表示边u - v出现的次数:pos[u][i]表示第i条边在邻接表e[u]中的位置:sum[i]表示预处…
[题目描述] 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体. 设从下往上数第i(1 <= i <= M)层蛋糕是半径为Ri, 高度为Hi的圆柱.当i < M时,要求Ri > Ri+1且Hi > Hi+1. 由于要在蛋糕上抹奶油,为尽可能节约经费,我们希望蛋糕外表面(最下一层的下底面除外)的面积Q最小. 令Q = Sπ 请编程对给出的N和M,找出蛋糕的制作方案(适当的Ri和Hi的值),使S最小. (除Q外,以上所有数据皆为…
[Codeforces 163D]Large Refrigerator (DFS+剪枝) 题面 已知一个长方体的体积为V,三边长a,b,c均为正整数,求长方体的最小表面积S V以质因数分解的形式给出 分析 暴力做法很容易想到,按照质因子的指数枚举a,b,然后就能直接算出c,然后就可以得到表面积S=2(ab+bc+ac) 考虑优化: 1.搜索顺序:保证\(a \geq b \geq c\),枚举质因数时从大到小枚举a,从小到大枚举b 2.可行性剪枝:枚举a的时候保证\(a^3 \leq V\),枚…
题目链接:https://www.vijos.org/p/1048 很多人一看就想出了思路,不就是一个裸的dfs蛮...但是..在n<=50的情况下,朴素会直接tle..... 然后我就开始剪枝,剪了很多地方,然后多过了一个点.....然后我就发现我的剪枝和我的dfs打的不行 然后我们先看看朴素的打法(打法多,我是一种非常原始的朴素算法) bool check(int x,int dep){ ;i<=dep;i++) ); ; } void dfs(int dep,int pos){ ){an…
Sticks(UVA - 307) 题目链接 算法 DFS+剪枝 1.这道题题意就是说原本有一些等长的木棍,后来把它们切割,切割成一个个最长为50单位长度的小木棍,现在想让你把它们组合成一个个等长的大木棍,要求这个拼接成的大木棍的长度最小.问最小长度是多少.(注意,在接下来的介绍中,将最后的大木棍表述为拼接木棍,小木棍还是叫小木棍) 2.看完这个题,基本思路是从一个小木棍开始找,找到一个未使用的小木棍后拼接,当拼接成的长度是所有小木棍长度和的约数时,就暂时把它定义为拼接木棍的最小长度,然后计算最…
Leetcode组合总和系列--回溯(剪枝优化)+动态规划 组合总和 I 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制重复被选取. 说明: 所有数字(包括 target)都是正整数. 解集不能包含重复的组合. 示例 1: 输入:candidates = [2,3,6,7], target = 7, 所求解集为: [ [7], [2,2,3] ] 来源…
题意:       给你一个n*m的格子,然后给你一个起点,让你遍历所有的垃圾,就是终点不唯一,问你最小路径是多少? 思路:       水题,方法比较多,最省事的就是直接就一个BFS状态压缩暴搜就行了,时间复杂度20*20*1024的,完全可以接受,但是被坑了,一开始怎么交都TLE,后来又写了一个BFS+DFS优化,就是跑之前先遍历一遍图,看看是不是所有的垃圾点都能遍历到,这样还是超时,无奈看了下讨论,有人说用G++交就行了,我用G++交了结果两个方法都AC了,哎!下面是两个方法的代码,比较简…
Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9779    Accepted Submission(s): 2907 Problem Description George took sticks of the same length and cut them randomly until all parts became…
POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Accepted: 6725 Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from our…
ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10777   Accepted: 3961 Description N cities named with numbers 1 ... N are connected with one-way roads. Each road has two parameters associated with it : the road length and the toll…
题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索:5 若新的搜索连第一条都没组合出来,直接break: 详细解释:http://blog.csdn.net/lyy289065406/article/details/6647960 http://www.cnblogs.com/devil-91/archive/2012/08/03/2621787.…
题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - len -1,r]; */ #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #include <queue> using namespace std;…
题意:容易理解... 思路:我开始是用dfs剪枝做的,968ms险过的,后来在网上学习了记忆化搜索=深搜形式+dp思想,时间复杂度大大降低,我个人理解,就是从某一个点出发,前面的点是由后面的点求出的,然后一直递归先求出后面的点,最后达到求解的效果. 代码实现: #include<iostream> #include<string.h> #include<stdio.h> using namespace std; ][],count[][]; int n,k; ][]={…
题目链接 Solution DFS+剪枝 对于一个走过点k,如果有必要再走一次,那么一定是走过k后在k点的最大弹药数增加了.否则一定没有必要再走. 记录经过每个点的最大弹药数,对dfs进行剪枝. #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include <map> using namespace std; map<string…
Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 127771   Accepted: 29926 Description George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the or…
题目链接:hdu 2829 Lawrence 题意: 在一条直线型的铁路上,每个站点有各自的权重num[i],每一段铁路(边)的权重(题目上说是战略价值什么的好像)是能经过这条边的所有站点的乘积之和..然后给你m个炮弹,让你选择破坏掉m段铁路,使剩下的整条铁路的战略价值最小. 题解: 和hdu 3480 Division(斜率优化DP)这题相同,只是方程不同而已,改改就行了. #include<bits/stdc++.h> #define F(i,a,b) for(int i=a;i<=…