随笔分类 - 图论--BFS
摘要:1128写的dfs貌似不太对 bfs重写用bfs将图进行黑白染色 如果有超过一个与自己颜色相同的点 就把该点存入栈中 最后处理栈中的点 判断此点是否合法 不合法 取反 取反后再判断相邻点是否合法 不合法再存入栈中 直到栈为空 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 using namespace std; 9 #define N 80010 10 vectored[N]; 11 int n; 12 int vis[N],d[N],f[N],g; 13 vo...
阅读全文
摘要:1651终于A了 看这题容易想到最短路 看到错的很多 还特意注意了好几处后来发现 必须按给出的顺序出边 想了想 这不就是BFS 然后就是各种细节 i->i+1ori->j(a[i]==a[j]) 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 using namespace std; 9 #define N 1001010 #define INF 0xfffffff11 vectored[N];12 vector::iterator it;13 int vis[N*10],pa[N..
阅读全文
摘要:1888dfs找出连通块 块内构造数据 bfs找出最值 如果有多个连通块 那max就为49 可以起点不同 这样记得修改后面的数据写的老长了。。 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 using namespace std; 9 vectored[55]; 10 int vis[55],gg,p,g,maxz,o[55],pp[55][55],q[55]; 11 int w[55],mm[55]; 12 void dfs(int u) 13 { 14 ...
阅读全文
摘要:1008没营养的破题 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 using namespace std; 8 typedef struct node 9 { 10 int x,y; 11 }st; 12 int vis[15][15],w[15][15]; 13 int n,o[115],dis[4][2] = {{1,0},{0,1},{-1,0},{0,-1}}; 14 int s[115][8],gg; 15 char ss[115][8]; 16 st p[115]...
阅读全文
摘要:链接本来想写spfa 加点什么限制什么的可能就过了 写着写着就成裸BFS了 也没优化就水过了 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 using namespace std; 8 #define N 110 9 #define M 1001010 #define INF 0xfffffff11 struct node12 {13 int u,v,w,next,len;14 }ed[Mq;38 mode ss,st;39 int i;40 for(i = 2; i a...
阅读全文
摘要:恶心的题啊 。。先枚举哪个点是所有人集合的点 再枚举所有骑士遇见国王的点 如果全部枚举出来会大大的TLE 经大牛验证 只需要枚举国王周围的点就可以了+-2 之内然后各种繁琐 各种错误 骑士有可能不带着国王一块走 也可能在他周围选个点带着走 先预处理出来每个骑士到国王周围的最短距离 然后再按上面的枚举就可以了考虑的不全面 。。错了好多个样例 样例2,6,19,20 都模拟了一遍。。还好错在小数据上 可以手算模拟一下 就知道哪错了 变量名都被我用穷了。。 1 /* 2 ID: shangca2 3 LANG: C++ 4 TASK: camelot 5 */ ...
阅读全文
摘要:1 /* 2 ID: shangca2 3 LANG: C++ 4 TASK: msquare 5 */ 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 #define INF 0xfffffff 13 using namespace std; 14 int o[10]; 15 typedef struct node 16 { 17 int num; 18 int a[10],pre; 19 char c; 20 }st; 21 ...
阅读全文
摘要:刚开始想写最短路 没写出来 TLE了在最后一组数据上 加了点优化过了 从两个出口出 像各点BFS 取最小中的最大值View Code 1 /* 2 ID: shangca2 3 LANG: C++ 4 TASK: maze1 5 */ 6 #include <iostream> 7 #include<cstdio> 8 #include<cstring> 9 #include<algorithm> 10 #include<stdlib.h> 11 #include<queue> 12 using namespace std
阅读全文
摘要:http://poj.org/problem?id=3414六种情况 搜一下View Code 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 struct node 6 { 7 int ca,cb; 8 int flag,num; 9 int op,ob; 10 }q[1000000]; 11 int a,b,c,vis[110][110],num[100000],g; 12 void put(int x,int y) 13 ...
阅读全文
摘要:http://poj.org/problem?id=3083纠结了好久 被它的转向搞晕 了WA两次 加一else AC。。设置四个方向 初始化方向自己算出来 初始化在数组中 dfs+bfs左 就是当前方向向左 左走不动就逆时针走下一个方向 依次类推 右同样的方式View Code 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 char s[50][50],so[2]; 6 int pr[7],dis[4][2]={-1,0,0,-1,
阅读全文
摘要:http://poj.org/problem?id=3026BFS求出任意两个A或A和S的最短距离 然后最小生成树求和把所有A和S存起来求BFS任意两点 搜超时了 之后改了改过了View Code 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 using namespace std; 7 #define INF 0x3f3f3f 8 struct node 9 { 10 int
阅读全文
摘要:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2465View Code 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue> 5 using namespace std; 6 typedef struct node 7 { 8 int x1,y1,x,y,num,flag1; 9 friend bool operator<( stru
阅读全文
摘要:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=473没什么意思View Code 1 #include <iostream> 2 #include<cstdio> 3 #include<string.h> 4 using namespace std; 5 char s[31][31][31]; 6 struct node 7 { 8 int x,y,z,num; 9 }q[100001];10
阅读全文
摘要:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=598这题题意看了老半天 让求图形中的X的区域块数 每个区域有多少个X是不相连的 DFS搜有多少个区域 BFS标记 有每个区域里多少X区域块注意两点吧 WA了5次 第一个可能为X 就直接BFS 再DFS第二 BFS中遇到的*要保留起来 把相连的X标记完之后 继续搜 不然区域块数会被分开View Code 1 #include <iostream> 2 #include&l
阅读全文
摘要:动态struct node{ int u,v,w; node *next;}*first[N];//一个节点相连的所有节点 构成一条链void init(){ memset(first,NULL,sizeof(first));}void add(int u,int v,int w){ node *p = new node; p->v = v; p->w = w; p->next = first[u];//类似于正序链表的插入 first[u] = p;}for(node *p = first[u] ; p!=NULL ; p= p->next){...
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1253View Code 1 #include<stdio.h> 2 #include<string.h> 3 int a[51][51][51]; 4 struct node 5 { 6 int x,y,z,num; 7 }q[50001]; 8 int p,d,f[51][51][51]; 9 int an[6][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};10 int main()11 {12 int i,j
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1372搜索8个可到达的点View Code 1 #include <stdio.h> 2 #include<string.h> 3 struct node 4 { 5 int tx,ty,num; 6 }q[100001]; 7 int u[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}}; 8 int main() 9 {10 int i,j,k,n,m,w,x[2],y[2],d,p,f[50][50];
阅读全文
摘要:http://poj.org/problem?id=2049与一般的BFS不大一样 是把网格转换成我们常用的点以左下角的点表示整个方格 用优先队列来保证每次搜的都是最小的 注意Nemo的位置有可能不在墙内之前把sx看成tx一直调试不出来 ,,纠结了一天 第二天一眼看出它俩不一样。。改完之后 找了找结构体的优先队列怎么用 按知道上回答写的 一直WA 后来看人家的解题报告 发现不是那么用的,改过来交上AC。。View Code 1 #include<stdio.h> 2 #include<string.h> 3 #include<iostream> 4 #inc
阅读全文
摘要:http://poj.org/problem?id=3278三次RE 以为是队列开小了 一直加大队列 忘记是标记数组的事了 改了判断条件 觉得队列不用开那么大 WA。。又开到100W AC..View Code 1 #include<stdio.h> 2 #include<string.h> 3 struct node 4 { 5 int x,num; 6 }q[1000001]; 7 int f[1000010]; 8 int p,d; 9 int main()10 {11 int i,j,n,k,flag = 0;12 scanf("%d%d",
阅读全文
摘要:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2142View Code 1 #include<stdio.h> 2 #include<string.h> 3 int q[5001]; 4 int d; 5 void inque(int x) 6 { 7 d++; 8 q[d] = x; 9 }10 int main()11 {12 int t,n,m,i,j,k,f[101][101],fg[101],a,b;13 scanf("%d",&
阅读全文

浙公网安备 33010602011771号