补图BFS(hdu 5876)】的更多相关文章

1.HDU 5876  Sparse Graph 2.总结:好题,把STL都过了一遍 题意:n个点组成的完全图,删去m条边,求点s到其余n-1个点的最短距离. 思路:把点分为两个集合,A为所有没有到达过的点,B为当前不可到达的点,每次拓展A中可到过的点加入队列. #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #inc…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意: 有一个 n 个点无向图,再给你 m 对顶点, 代表着这 m 对顶点之间没有边, 除此之外每两个点之间都有一条边, 且权值为 1.然后还有一个源点 S, 让你计算源点到其他各点之间的最短距离,如果不存在则输出 -1.也就是说让你在所给的图的补图上求源点到其他各点的最短路径. 思路: 补图上求最短路径算是比较经典的题.在这里所求的最短路其实并不需要用到 dijkstra 之类的算法,由于每…
题目大意: 给出一个图和起点S,求补图中S到其他点的最短距离. http://acm.hdu.edu.cn/showproblem.php?pid=5876 我自己的垃圾做法: 用线段树来维护dijkstra的dis数组.每次取出dis最小的点来更新其他点. 假设x连出去的边是y1 < y2 < y3 ... < yk.  那么对于dis[1, y1 - 1] [y1 + 1, y2 - 1] [y2 + 1, y3 - 1]...这些区间做区间取min操作. 比较容易出错的的地方是每次…
Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 689    Accepted Submission(s): 238 Problem Description In graph theory, the complement of a graph G is a graph H on the same vertic…
In graph theory, the complement of a graph G is a graph H on the same vertices such that two distinct vertices of H are adjacent if and only if they are not adjacent in G. Now you are given an undirected graph G of N nodes and M bidirectional edges o…
题目:这里 题意: 相当于一开始给一个初始好了的无向完全图给你,然后给让你删除m条边,再给你一个点v,最后问你在剩下的图里从这个点v出发能到达所有边点的最小路径是多少? 一看是所有点的最小路径,一看就觉得是个bfs,记忆化搜一下然后加个优化什么的,由于数据不知道是个什么奇葩而且比赛中还改数据,所以很多人wa的莫名其妙, 过也过的莫名其妙,我虽然过了但觉得有点不靠谱,赛后看了https://async.icpc-camp.org/d/546-2016的题解思路写了一发,总感觉更靠谱一点. 之前自己…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意:给定一个图(n个顶点m条边),求其补图最短路 思路:集合a表示当前还未寻找到的点,集合b表示本次bfs之后仍未寻找到的点 #include<cstdio> #include<set> #include<queue> #include<cstring> using namespace std; const int N = 2e5 + 5; set &l…
http://acm.hdu.edu.cn/showproblem.php?pid=5876 Sparse Graph Problem Description   In graph theory, the complement of a graph G is a graph H on the same vertices such that two distinct vertices of H are adjacent if and only if they are not adjacent in…
http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意: 在补图中求s到其余各个点的最短路. 思路:因为这道题目每条边的距离都是1,所以可以直接用bfs来做. 处理的方法是开两个集合,一个存储当前顶点可以到达的点,另一个存储当前顶点不能到达的点.如果可以到达,那肯定由该顶点到达是最短的,如果不能,那就留着下一次再判. #include<iostream> #include<algorithm> #include<cstring>…
---恢复内容开始--- Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 2590    Accepted Submission(s): 902 Problem Description In graph theory, the complement of a graph G is a graph H on t…
BFS+链表 代码改自某博客 #include<stdio.h> #include<iostream> #include<algorithm> #include<math.h> #include<string.h> #include<string> #include<map> #include<set> #include<vector> #include<queue> using nam…
Problem Description In graph theory, the complement of a graph G is a graph H on the same vertices such that two distinct vertices of H are adjacent if and only if they are not adjacent in G. Now you are given an undirected graph G of N nodes and M b…
开两个集合,一个存储当前顶点可以到达的点,另一个存储当前顶点不能到达的点.如果可以到达,那肯定由该顶点到达是最短的,如果不能,那就留着下一次再判. #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> pll; const int INF = 0x3f3f3f3f; +; int T; int n, m, s; int d[maxn]; vector<i…
Sparse Graph Problem Description   In graph theory, the complement of a graph G is a graph H on the same vertices such that two distinct vertices of H are adjacent if and only if they are notadjacent in G. Now you are given an undirected graph G of N…
Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Problem Description In graph theory, the complement of a graph G is a graph H on the same vertices such that two distinct vertices of H are adjacent if…
题目链接 分析:这叫补图上的BFS,萌新第一次遇到= =.方法很简单,看了别人的代码后,自己也学会了.方法就是开两个集合,一个A表示在下一次bfs中能够到达的点,另一个B就是下一次bfs中到不了的点.一开始先把出了起点的所有点都加入A,然后从bfs的点跑一遍边, 把边相连的点从A中取出放到B中.然后遍历A集合,进行bfs.然后把B全部放入A中,清空B.于是又回到了通过边把边连接的点从A移动到B,重复bfs...解释地不是很清楚,大体意思就是这样的了.在这个方法中,每个点和每条边都值操作过一次,复…
http://www.lydsy.com/JudgeOnline/problem.php?id=1098 显然答案是补图连通块..... 想到用并查集...可是连补图的边都已经...n^2了...怎么做.. 好神的题! 考虑我们是如何建补图和并查集:是不是都是先枚举一个点,然后标记能到达的点,然后没到达的点就是要连的边.最后在来一次并查集. 是吧.. 那么考虑:为什么要并查集...我们找到没到达的点就行了么...那么肯定就是在一个连通块,问题在于如何将整个连通块找出来.bfs! 考虑:我们之前放…
Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1520    Accepted Submission(s): 537 Problem Description In graph theory, the complement of a graph G is a graph H on the same verti…
题目链接:hdu_5876_Sparse Graph 附上叉姐的题解: 1009 Sparse Graph [by ftiasch] 题意:n 个点的无向完全图中删除 m 条边,问点 s 到其他点的最短路长度. 题解: 补图上的 BFS 是非常经典的问题.一般的做法是用链表(或者偷懒用 std::set)维护还没 BFS 过的点.当要扩展点 u 的时候,遍历一次还没访问过的点 v,如果 uv 没边,那么将 v 入队.否则将 v 留在未扩展点中. 很明显,后者只会发生 m 次,前者只会发生 n 次…
Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 928    Accepted Submission(s): 312 Problem Description In graph theory, the complement of a graph G is a graph H on the same verti…
http://acm.hdu.edu.cn/showproblem.php?pid=1195 这道题虽然只是从四个数到四个数,但是状态很多,开始一直不知道怎么下手,关键就是如何划分这些状态,确保每一个状态都能遍历到. 得到四个数之后,分三种情况处理,每次改变一个数之后都要加入队列,最先输出的就是步数最少. #include <cstdio> #include <cstring> #include <queue> using namespace std; struct p…
看题传送门: ZOJ http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649 HDU http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目大意初始位置在r,要求到达a的地点,地图上"."通过需要1s,"x"代表守卫,通过耗时2s,"#"不能走. BFS的应用. BFS求最短路径的原理是每一次向外扩张一格,(就像树的层次遍历一样…
题意: n(n<=200000)个点的完全图删去了m(m<=200000)条边,求剩下图的连通分量. 分析: 将未访问过的点用一个链表串起来 仍旧进行BFS,每次BFS扩展一个点u的时候,暴力去for链表,如果发现有与u相连的点则该点入队且从链表删除 直至链表为空 我们来分析一下这个的复杂度,首先明显每个点只会删除一次,O(n) 但是一个点会被for很多次,我们发现被for很多次是在原图有边的情况,所以是O(2*m) 总的复杂度O(n+m) 具体在实现过程中,要判断两点是否右边,所以要用一个s…
题意: 给你9*9的矩阵.对于每一个数字.能减16代表上面有墙,能减32代表以下有墙. .. 最后剩下的数字是0代表这个位置数要求,不是0代表这个数已知了. 然后通过墙会被数字分成9块. 然后做数独,这里的数独不是分成9个3*3的小块而是通过墙分成的. 思路: 首先通过数字作出墙. 然后bfs求连通块.dfs也能够.目的是分块. 然后就是dlx数独模板题了. 这里要注意的是假设找到答案2次就说明有多组解了.就应该停止返回了.不然会TLE. 代码: #include"stdio.h" #…
可以吃饭啦!!! A:连通块 ZOJ 1709 Oil Deposits(dfs,连通块个数) B:素数变换 打表+bfs POJ 3216 Prime Path(打表+bfs) C:水bfs HDU 2717 Catch That Cow(常规bfs) D:bfs+状态压缩 HDU 1429 胜利大逃亡(续)(bfs+状态压缩,很经典) E:bfs+优先队列 CSU 1726: 你经历过绝望吗?两次!(bfs+优先队列) F:bfs+优先队列(还有点问题) G:很有意思的bfs CodeFor…
康托展开+反向bfs hdu 1043 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1043 #include <iostream> #include <cstdio> #include <queue> using namespace std; struct Step_s{ int parent; char dir; }step_set[500010]; typedef struct Node_s{ int board…
不定时更新博客,该博客仅仅是一篇关于最短路的题集,题目顺序随机. 算法思想什么的,我就随便说(复)说(制)咯: Dijkstra算法:以起始点为中心向外层层扩展,直到扩展到终点为止.有贪心的意思. 大部分题用Dij+队列优化都能解决..但有负权边不行哦. Bellman-Ford:反复对边集E中的每条边进行松弛操作. 求含负权图(有负环输出错误提示)的单源最短路径,效率很低,至于对多余松弛的优化就是设置个标记,还是很慢,不如写SPFA. 判断负环:在每次松弛时把每条边都更新一下,若在n-1次松弛…
E - Connected Components? 思路: 补图bfs,将未访问的点存进set里 代码: #include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define mem(a,b) memset(a,b,sizeof(a)) ; bool vis[N]; int head[N]; int a[N]; ,ans=; struct edge{ int to,next;…
转载自hr_whisper大佬的博客 [ 一.Dijkstra 比较详细的迪杰斯特拉算法讲解传送门 Dijkstra单源最短路算法,即计算从起点出发到每个点的最短路.所以Dijkstra常常作为其他算法的预处理. 使用邻接矩阵的时间复杂度为O(n^2),用优先队列的复杂度为O((m+n)logn)近似为O(mlogn) (一) 过程 每次选择一个未访问过的到已经访问过(标记为Known)的所有点的集合的最短边,并用这个点进行更新,过程如下: Dv为最短路,而Pv为前面的顶点. 初始 在v1被标记…
新的整理版本版的地址见我新博客 http://www.hrwhisper.me/?p=1952 一.Dijkstra Dijkstra单源最短路算法,即计算从起点出发到每个点的最短路.所以Dijkstra常常作为其他算法的预处理. 使用邻接矩阵的时间复杂度为O(n^2),用优先队列的复杂度为O((m+n)logn)近似为O(mlogn) (一)  过程 每次选择一个未访问过的到已经访问过(标记为Known)的所有点的集合的最短边,并用这个点进行更新,过程如下: Dv为最短路,而Pv为前面的顶点.…