补图BFS(hdu 5876)
题目大意:
给出一个图和起点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操作。 比较容易出错的的地方是每次用x更新完其他点,需要把dis[x] 设置成INF, 对于一个区间,如果最小值就是INF,说明这个区间里的点都被扩展过了,直接return,而不能对它做取min操作。 因为需要做的取min操作是O(m)的,所以总的复杂度是O(mlogn).
简单做法:
用一个set维护哪些点还没被BFS到。 每次从队列取出一个点x, 然后把set里的点分成两类,一类是和x在原图中有边相连,一类是没有边相连,把和x没有边相连的点从set中删去,加入队列。 总的复杂度是O(mlogn)
参考代码:
//#pragma comment(linker, "/STACK:102400000,102400000")#include <bits/stdc++.h>
//zoj 3496
#include <bits/stdc++.h>
using namespace std; typedef long long ll;
#define X first
#define Y second
#define MAXN 200020
#define M 105
const int mod = 1e9 + ;
const int INF = 1e9 + ; vector<int> E[MAXN];
int d[MAXN];
queue<int> Q;
set<int> st, tmp; void BFS(int S, int n)
{
Q.push(S); d[S] = ;
for (int i = ; i <= n; ++i)
if (i != S) st.insert(i);
while (!Q.empty())
{
int x = Q.front(); Q.pop();
tmp.erase(tmp.begin(), tmp.end());
for (auto y: E[x])
{
if (st.find(y) != st.end())
tmp.insert(y);
}
for (auto v: st) if (tmp.find(v) == tmp.end()) Q.push(v), d[v] = d[x] + ;
st = tmp;
}
bool flag = false;
for (int i = ; i <= n; ++i)
{
if (i == S) continue;
if (flag) printf(" ");
printf("%d", d[i]);
flag = true;
}
printf("\n");
} int main()
{
//freopen("input", "r", stdin);
//freopen("output", "w", stdout); int T, n, m;
scanf("%d", &T);
while (T--)
{
scanf("%d %d", &n, &m);
for (int i = ; i <= m; ++i)
{
int x, y;
scanf("%d %d", &x, &y);
E[x].push_back(y);
E[y].push_back(x);
}
int S;
scanf("%d", &S);
for (int i = ; i <= n; ++i) d[i] = -;
BFS(S, n);
for (int i = ; i <= n; ++i) E[i].clear();
} return ;
}
补图BFS(hdu 5876)的更多相关文章
- HDU 5876 关于补图的bfs
1.HDU 5876 Sparse Graph 2.总结:好题,把STL都过了一遍 题意:n个点组成的完全图,删去m条边,求点s到其余n-1个点的最短距离. 思路:把点分为两个集合,A为所有没有到达 ...
- HDU 5876 Sparse Graph(补图上BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意: 有一个 n 个点无向图,再给你 m 对顶点, 代表着这 m 对顶点之间没有边, 除此之外 ...
- HDU 5876 Sparse Graph 【补图最短路 BFS】(2016 ACM/ICPC Asia Regional Dalian Online)
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...
- HDU - 5876 :Sparse Graph (完全图的补图的最短路 -BFS&set)
In graph theory, the complement of a graph G is a graph H on the same vertices such that two distinc ...
- hdu 5876 (补图BFS) Sparse Graph
题目:这里 题意: 相当于一开始给一个初始好了的无向完全图给你,然后给让你删除m条边,再给你一个点v,最后问你在剩下的图里从这个点v出发能到达所有边点的最小路径是多少? 一看是所有点的最小路径,一看就 ...
- HDU 5876 (大连网赛1009)(BFS + set)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意:给定一个图(n个顶点m条边),求其补图最短路 思路:集合a表示当前还未寻找到的点,集合b表 ...
- HDU 5876:Sparse Graph(BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=5876 Sparse Graph Problem Description In graph theory, t ...
- HDU 5876 Sparse Graph(补图中求最短路)
http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意: 在补图中求s到其余各个点的最短路. 思路:因为这道题目每条边的距离都是1,所以可以直接用bfs来做 ...
- HDU 5876 补图 单源 最短路
---恢复内容开始--- Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (J ...
随机推荐
- qs.stringify() 和JSON.stringify()的区别
var a = {name:'hehe',age:10}; qs.stringify(a) // 'name=hehe&age=10' JSON.stringify(a) // '{" ...
- 我的Android进阶之旅------>Android字符串资源中的单引號问题error: Apostrophe not preceded by 的解决的方法
刚刚在string字符串资源文件里,写了一个单引號.报错了,错误代码例如以下 error: Apostrophe not preceded by \ (in OuyangPeng's blog ) 资 ...
- MyBatis SpringBoot2.0 数据库读写分离
1.自定义DataSource import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; /** * @ ...
- windows下检測软件的网络连接
首先打开任务管理器选中你要查看的应用.右键转到进程 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGFwZW5nMDExMg==/font/5a6L5L2 ...
- struts 防止重复提交表单
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC &qu ...
- 转载:Linux下执行SVN命令时提示错误:Valid UTF-8 data
在Linux下执行svn add *时出现如下错误: svn: Valid UTF-8 data(hex: 4b)followed by invalid UTF-8 sequence(hex: ...
- FastDFS这种架构,如何配置?
FastDFS这种架构,如何配置?才能让欧洲用户可以就近下载Storage Server1的文件,而不是到中国的Storage Server 2下载?
- 每天进步一点点——Ganglia的Python扩展模块开发
转载请说明出处:http://blog.csdn.net/cywosp/article/details/39701245 注:本文涉及到的代码都在centos 6.5 64bit系统上通过验证,Gan ...
- Hibernate一级缓存和二级缓存具体解释
一.一级缓存二级缓存的概念解释 (1)一级缓存就是Session级别的缓存,一个Session做了一个查询操作,它会把这个操作的结果放在一级缓存中.假设短时间内这个 session(一定要同一个ses ...
- keystone WSGI流程
作为OpenStack两种基本的通信方式(RESTful API与消息总线)之中的一个.理解RESTful API的设计思路和运行过程,有助于我们对OpenStack有更好的理解.RESTful仅仅是 ...