随笔分类 -  图论——搜索

摘要:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2497题意 : 给定一个 无向图 和 一个点 s 求 是否 图中的 所有环 都包含 点s (保证 图中 至少 有 一个环)题解 : dfs 求解 只要 搜到 一个 被访问过的点 而且这个点 不是 s 那么 就有环 不含有 sView Code 1#include<cstdio>2#include<cstring>3#include<cmath>4#include<iostream>5#includ 阅读全文
posted @ 2012-12-02 19:32 Szz 阅读(202) 评论(0) 推荐(0) 编辑
摘要:http://poj.org/problem?id=3635题意:有 n 个城市 ,每个 城市的 油价 不一样 ,已知 各 城市之间的距离 ,(假设 一 单位 距离 耗费一单位的 油量)一辆车 的 油箱 最多可以 装 c 升油,求从 s 到 e,的 最小 花费油量,如果不能到达 输出 impossible 。题解:一开始 自己 写了 个,到达每一点后枚举 可以 增加的 油量 结果 tle (太多 无用的状态了)。。。。。。dp[i][j] 表示 到达 城市 i 剩余油量 为 j 的 最小花费; 首先到达每个城市的后 要加的 油量 是 不确定 的 ,所以 我们要 将 每一个城市 拆成 c+ 1. 阅读全文
posted @ 2012-09-15 15:54 Szz 阅读(645) 评论(0) 推荐(0) 编辑
摘要:Party All the TimeTime Limit: 6000/2000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 926Accepted Submission(s): 341Problem DescriptionIn the Dark forest, there is a Fairy kingdom where all the spirits will go together and Celebrate the harvest every year. But there i 阅读全文
posted @ 2012-08-10 11:09 Szz 阅读(433) 评论(0) 推荐(0) 编辑
摘要:http://poj.org/problem?id=1691/* 题解: 将每个矩形看成一个点,点A在点B的上方 则b的入度加1,进行拓扑构图 标记 ,每次搜索时搜入读为零的点 构图时少考虑的一种情况 wa 汗。。。。。。。*/#include<stdio.h>#include<string.h>#define max 999999#define maxn 2000struct node{ int xl,yl,xr,yr; int c;}p[maxn];int map[20][20],in[20],n,ans,vis[20];void build(){ int ... 阅读全文
posted @ 2012-07-21 19:17 Szz 阅读(258) 评论(0) 推荐(0) 编辑
摘要:http://poj.org/problem?id=3373详解见:http://blog.csdn.net/lyy289065406/article/details/6698787 /*这道题想了半天越想月觉的麻烦,后来实在是写不出来就,参考了一下别人烦人代码,在这加了数组f[a][b]=c;a 表示位置,b表示余数,c表示剩余的次数,含义是 在a这个位置,余数为b的情况下无论是往较大的数改变,还是娇小的数,在改变此时《=c情况下,找不到满足要求的数*/#include<stdio.h>#include<string.h>int mod[110][10],test[1 阅读全文
posted @ 2012-07-19 09:28 Szz 阅读(259) 评论(0) 推荐(0) 编辑
摘要:1 1 2 http://poj.org/problem?id=1724 3 一开始就直接写了一个DFS,直接TLE,以为用dfs肯定会超,后来看到discuss里有人 4 2 有人用了DFS,所以看了 看,发现进行一些优化,后就可以AC 5 3 优化看代码 6 4 7 5 #include<stdio.h> 8 6 #include<string.h> 9 7 #define maxn 2000010 8 #define max 0x7fffffff11 9 12 10 struct node13 11 {14 12 int d;15 13 ... 阅读全文
posted @ 2012-07-17 15:12 Szz 阅读(163) 评论(0) 推荐(0) 编辑
摘要:1 http://poj.org/problem?id=1724 2 题目大意:单向边最短路问题,但是每条边都有一个费用,要求你的最短路方案的总的花费不能超过限制。 3 4 BFS+优先队列优化 5 因为题目要求的是在给定费用下的最短距离,所以 以距离优先建队列 广搜 6 7 #include<stdio.h> 8 #include<string.h> 9 #include<vector>10 #include<queue>11 using namespace std;12 #define maxn 2000013 #define max 0x7 阅读全文
posted @ 2012-07-17 15:04 Szz 阅读(157) 评论(0) 推荐(0) 编辑
摘要:1 /* http://poj.org/problem?id=3411 2 题目意思: 3 从a到b有两种付费方式 1:在a之前已经访问了c(不一定是刚访问),付费p;2:a直接到b 付费r; 4 求 1到n 的最小费用(注意有的点可以访问多次) 5 6 解题思路:dfs ,题目的关键是一个点可能被访问多次,一条边也可能被访问多次 7 某一个点是否访问过可以用f[i]标记,f[i]>0表示访问过了 8 其次还有一点很重要的,就是每个城市最多经过3次(网上说的“闸数”)就可以跳出循环了 9 (一开始以为每条边只会访问一次,wa)10 11 */12 #include<stdio.h& 阅读全文
posted @ 2012-07-16 10:25 Szz 阅读(220) 评论(0) 推荐(0) 编辑
摘要:POJ图论分类2009-07-28 23:13POJ 2449 Remmarguts' Date(中等)http://acm.pku.edu.cn/JudgeOnline/problem?id=2449题意:经典问题:K短路解法:dijkstra+A*(rec),方法很多相关:http://acm.pku.edu.cn/JudgeOnline/showcontest?contest_id=1144该题亦放在搜索推荐题中POJ 3013 - Big Christmas Tree(基础)http://acm.pku.edu.cn/JudgeOnline/problem?id=3013题意: 阅读全文
posted @ 2012-05-12 10:25 Szz 阅读(309) 评论(0) 推荐(0) 编辑
摘要:1 http://poj.org/problem?id=3662/* 2 二分+最短路径 3 题意:求解额外付出的最小的最大边,k段是免费的. 4 5 题目的关键在于枚举最小的最大边,如何枚举呢?用二分法,选一条边,重新构图; 6 在图中大于这条边的,记为1; 7 把小于等于这条边的 边记为0; 8 求 1-》n的最短距离len 9 10 题目有3种情况, 11 12 1 不连通的情况 13 14 2 不需要额外花费的情 15 3 需要花费(那么了一定存在一个边 使 len==k) 16 */ 17 #include<cstdio> 18 #include<a... 阅读全文
posted @ 2012-05-06 21:12 Szz 阅读(472) 评论(0) 推荐(0) 编辑
摘要:1 /floyd求传递闭包,裸题,当牛被打败x次,胜利y次,且x+y==n-1时,说明该牛位置确定。注意下面的程序中 2 //map[i][i]恒为false,也就是自己不能打败自己。难度1 3 4 #include<stdio.h> 5 #include<string.h> 6 int n,m; 7 int map[500][500]; 8 void floyd() 9 {10 int i,j,k;11 for(k=1;k<=n;k++)12 {13 for(i=1;i<=n;i++)14 for(j=1;j<=n;j++)15... 阅读全文
posted @ 2012-05-06 11:35 Szz 阅读(217) 评论(0) 推荐(0) 编辑
摘要:#include<stdio.h>int ans,a[22],f,step;int judge(){ int i; for(i=1;i<=20;i++) if(a[i]==1)return 0; return 1;}void change(int x){ a[x]=!a[x]; if(x-1>=1)a[x-1]=!a[x-1]; if(x+1<=20)a[x+1]=!a[x+1];}void dfs(int x,int num){ if(step==num) { if(judge()) { ... 阅读全文
posted @ 2012-04-09 17:41 Szz 阅读(179) 评论(0) 推荐(0) 编辑
摘要:http://poj.org/problem?id=2251#include<stdio.h>#include<string.h>#include<queue>#include<iostream>using namespace std;#define max 999999#define N 40int l,r,c,ans,ex,ey,ez;char map[N][N][N];int vis[N][N][N];int dx[6]={-1,0,1,0,0,0};int dy[6]={0,1,0,-1,0,0};int dz[6]={0,0,0,0,1 阅读全文
posted @ 2012-04-03 21:48 Szz 阅读(139) 评论(0) 推荐(0) 编辑
摘要:http://poj.org/problem?id=1321#include<stdio.h>#include<string.h>#define N 100char str[N][N];int n,ans,k,r[N],c[N],l;struct node{ int x; int y;}p[N];void dfs(int num,int sum){ if(num<=l&&sum==0) { ans++; return ; } if(num>=l) { return ; } int x=p[num].... 阅读全文
posted @ 2012-04-03 20:27 Szz 阅读(137) 评论(0) 推荐(0) 编辑
摘要:http://poj.org/problem?id=3009题意:溜石游戏。在一给定大小的矩形冰面上,散布若干石块,给定石头的初始位置和终点,求从起点到达终点的最小步数,超过10次则视作不可达。其中规则如下,若石头与石块有相邻则不能向该方向滑动;每次溜石只能到达有石块的地方,且将其立即敲碎;若出界则视作失败。思路:限制条件较多的dfs,每个状态下有四个方向的选择,注意每个滑动方向中的情况,较繁琐。#include<stdio.h>#include<iostream>const int inf=999999;using namespace std;int r,c,step 阅读全文
posted @ 2012-04-02 21:22 Szz 阅读(261) 评论(0) 推荐(0) 编辑
摘要:/*思路 : 优先队列 bfs ,从每一天中 病毒类型最小的开始 ,所以优先队列 先 按天 排在按类型 */ #include<iostream>#include<stdio.h>#include<queue>#include<cmath>#include<string.h>#define N 600using namespace std;struct node{ int x; int y; int day; int ty; friend bool operator < (struct node a,struct node b) 阅读全文
posted @ 2012-04-01 21:19 Szz 阅读(129) 评论(0) 推荐(0) 编辑
摘要:http://poj.org/problem?id=3083一开始没明白题意以为只是简单的(顺时针或逆时针),wa一次。。。。1、至于求最短距离,毋庸置疑,肯定是bfs,这个就不多说了2、对于向左和向右的理解上,我当初一直不明白,读了老长时间,没有看懂,到discuss里看了一个人的叙述,终于明白意思了……就是这样,一直沿着向左或向右的方向走,能走就走,不能走就回撤,所以这个dfs不能标记遍历过的点,这是很显然的。3、dfs方向的选择,就是要保证沿着向左或者向右的方向走( 向左是顺时针,向右是逆时针转)#include<stdio.h>#include<string.h> 阅读全文
posted @ 2012-03-30 21:41 Szz 阅读(150) 评论(0) 推荐(0) 编辑
摘要:题意 马走完整个棋盘的路径 (注意 搜索的顺序)http://poj.org/problem?id=2488#include<stdio.h>#include<string.h>#define N 100int d[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};int m[N],f,k,r,c,vis[N][N],read[N][N];void dfs(int x,int y,int k){ int i; if(f==1)return ; read[k][0]=x; read[k][1]. 阅读全文
posted @ 2012-03-29 16:13 Szz 阅读(192) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示