随笔分类 -  搜索

摘要:zoj 3611同类型的题目poj 2688化简模型即可得这样的问题,一幅图给定起点 终点 ,求起点到终点最多经过几个点,如果两种方案经过的点数相同,选路径总长短的注意,这幅图最多只有12个点,所以立刻可以想到用状态压缩来做dp[i][j]表示以i为终点的路径 为j状态(经过了那些点)时的最短路径ps:在预处理最短路的时候不要将'$'算进去,就看成一个普通的可以走的格子就可以了因为可能会出现如下情况,'$'的费用会算了四次,所以还是先数一下有几个'$'然后再独立算吧View Code #include<cstdio>#include& 阅读全文
posted @ 2012-06-25 22:04 Because Of You 阅读(2072) 评论(1) 推荐(1)
摘要:ABC水题D:n个二元组,求最长的一个序列组seq,序列的每一个元素的两个值都分别大于前一个元素记忆化搜索或者筛选后DP都可以两种方法View Code #include<cstdio>#include<cstring>#include<set>#include<string>#include<iostream>#include<map>#include<vector>#include<algorithm>using namespace std;#define DEBUG printf(" 阅读全文
posted @ 2012-05-22 17:56 Because Of You 阅读(244) 评论(0) 推荐(0)
摘要:A 给你一个环,环上的每条边都有一个方向,按某个方向(顺时针或逆时针)走一遍,把与行走的方向相反的边反向,并加上相应的费用,判断走哪个方向的费用比较少直接dfs搞即可View Code #include<cstdio>#include<cstring>#include<vector>#include<algorithm>using namespace std;int out[110];vector<int> edge[110];int flag[110][110];int mm[110][110];int vis[110];vecto 阅读全文
posted @ 2012-05-17 05:51 Because Of You 阅读(388) 评论(0) 推荐(0)
摘要:要求去掉树中的一个点,使得分开的连通块中不存在总点数大于n/2的块直接dfs记录u节点下总共有几个儿子即可,水View Code #include<cstdio>#include<vector>#include<cstring>using namespace std;const int maxn = 10010;vector<int> edge[maxn];int sum[maxn],ans[maxn],n;void dfs(int u,int fa){ sum[u]=1; int i,j; bool flag=true; for(i=0;i&l 阅读全文
posted @ 2012-04-08 19:44 Because Of You 阅读(229) 评论(0) 推荐(0)
摘要:求出所有的交点,去重,如果是警察点,就不放入点集再把不在线段交点上的警察点加入点集,排序由于一个点只能和同一线段上相邻的点连边,所以枚举每条直线,相邻的点如果不是警察,建边最后bfs判断能否搜到即可View Code #include <math.h>#include <cstdio>#include <vector>#include <queue>#include<algorithm>using namespace std;const int maxn = 10001;const double eps = 1e-8;inline d 阅读全文
posted @ 2012-04-05 23:26 Because Of You 阅读(478) 评论(0) 推荐(0)
摘要:因为题目告诉我们每两点间只有一条路相连that there is exactly one path between any two free blocks.就可以把点之间形成的路径看成是一棵树题目又要我们找最长的一条路,瞬间就可以转换成树的最长路来做了结论是从树上的任何一点出发,能搜到的最远的点肯定是最长链上的一端,再从这一端出发,能搜到的最远的点的距离即为答案证明过程详见View Code #include<queue>#include<cstdio>#include<cstring>#include<algorithm>using names 阅读全文
posted @ 2012-04-01 21:59 Because Of You 阅读(276) 评论(0) 推荐(0)
摘要:先用两个dfs暴力求出所有的lucky numbers,然后二分lucky number数组的下标,求出答案View Code #include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<vector>using namespace std;typedef __int64 lld;vector<lld> lu,res;int tot;void dfs(lld num,int de){ if(de==12) return ; l 阅读全文
posted @ 2012-03-08 16:47 Because Of You 阅读(441) 评论(0) 推荐(0)
摘要:跑的好慢啊,看到别人的几百ms,就知道这道题目不能AC了就算数先记在这View Code #include<cstdio>#include<cstring>#include<queue>#include<algorithm>using namespace std;const int M =1010;int map[M][M];int vis[M][M];int dir[8][2]={-1,0,-1,1,0,1,1,1,1,0,1,-1,0,-1,-1,-1};int r,c;int sx,sy,ex,ey;struct node{ int x,y 阅读全文
posted @ 2012-01-14 06:17 Because Of You 阅读(297) 评论(0) 推荐(0)
摘要:基本功不够扎实 dubug 了好久View Code #include<cstdio>#include<cstring>int h[50][50];int a[50];int sum[50];int ans[50];int re[50];int n,m;int tot;bool goal;void dfs(int dep,int p){ int i,j; goal=true; for(i=1;i<=n;i++) if(sum[i]<a[i]) goal=false; if(goal && dep>tot) { ... 阅读全文
posted @ 2011-12-21 11:17 Because Of You 阅读(287) 评论(0) 推荐(0)
摘要:题目要求从目标状态变到终点状态至少需要几步,数据较小,而且终点状态是一样的,所以直接从终点状态开始反向广搜,把所有的状态都搜出来记录一下就好了记录状态可以用哈希,把一个状态映射成一个7进制的数同时用一个数组记录一下所走的步数广搜的方向View Code #include<cstdio>#include<queue>#include<cstring>#include<cstdlib>#include<algorithm>#include<iostream>using namespace std;int head[10];in 阅读全文
posted @ 2011-12-16 13:58 Because Of You 阅读(304) 评论(0) 推荐(0)
摘要:真想知道那些几百ms的是怎么写的View Code #include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>using namespace std;struct point { double x,y;}p[100010],q[100010];double ans;inline double dis(point a,point b){ return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}inline doubl 阅读全文
posted @ 2011-12-09 16:15 Because Of You 阅读(324) 评论(0) 推荐(0)
摘要:关键点:对每个箱子的dimension 排个序,这样才能判断某个箱子是否真的能放入另一个箱子内建好图后求一个图中的最长路即可View Code #include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int inf = 100000000;int box[510][1010];int map[510][510];int n,d;bool ok;int count=0;bool solve(int a,int b){ int i,j; bool flag=t 阅读全文
posted @ 2011-12-06 22:01 Because Of You 阅读(407) 评论(0) 推荐(0)
摘要:要求输出以n为结尾的一个数列,要求是数列的每一个数分解成的两个数都在数列中,且使得数列最短直接dfs过掉了View Code #include<stdio.h>#include<string.h>int n,dep,vis[110],ans[110];int dfs(int p,int sum){ int i; if(p==dep) { if(sum==n) return 1; return 0; } for(i=0;i<=p;i++) { sum+=ans[i]; if(!vis[sum]&&sum<=n... 阅读全文
posted @ 2011-11-28 23:05 Because Of You 阅读(356) 评论(0) 推荐(0)
摘要:给定一棵树,每个节点有一个权值。任意去掉一条边会形成两颗子树,每颗子树有一个权值和,求二者相差的最小值。dfs的时候求出当前节点和其子树的节点的总权值w,并和sum-w相减,和ans比较即可View Code 1 #include<stdio.h> 2 #include<string.h> 3 #include<vector> 4 using namespace std; 5 vector<int> mm[100010]; 6 int W[100010]; 7 bool vis[100010]; 8 __int64 ans,sum; 9 __in 阅读全文
posted @ 2011-11-11 13:24 Because Of You 阅读(378) 评论(0) 推荐(0)
摘要:!A的感觉真是好,继续加油!View Code 1 #include<stdio.h> 2 #include<string.h> 3 #include<math.h> 4 #include<algorithm> 5 using namespace std; 6 #define M 16 7 const double eps=1e-8; 8 int map[M][M]; 9 const int inf = 1000000;10 bool flag[M];11 int D[M],nodes[M],w[M];12 int n,m,rest[M];13 阅读全文
posted @ 2011-11-05 22:10 Because Of You 阅读(378) 评论(0) 推荐(0)
摘要:用位运算来实现广搜,每一条边就是一个方向View Code #include<stdio.h>#include<queue>#include<string.h>using namespace std;int steps;queue<int> Q;int vis[1<<17];int dir[32][2]={0,1,0,2,0,8,0,4, 1,3,1,9,1,5, 3,7,3,2,3,11, 11,9,11,15,11,10, 10,2,10,8,10,14, 8,12,8,9, 9,13, 2,6, 4,5,4,6,4,12, 5, 阅读全文
posted @ 2011-10-07 18:40 Because Of You 阅读(260) 评论(0) 推荐(0)