hdu5876 Sparse Graph(补图最短路 bfs)
题目链接:hdu5876 Sparse Graph
详见代码。。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
using namespace std;
const int N = ;
const int inf = 0x3f3f3f3f;
int n, m;
vector<vector<int> >g;
int d[N];
void bfs(int s){
int u,v,i,j;
queue<int>q;
set<int>a; //不邻接的点
set<int>b; //未扩展的点
set<int>::iterator it;
for(i = ; i <= n; ++i)
a.insert(i);
a.erase(s);
q.push(s);
while(!q.empty()){
u=q.front();
q.pop();
for(j=;j<g[u].size();++j){
v=g[u][j];
if(!a.count(v))
continue;
b.insert(v);
a.erase(v);
}
for(it = a.begin(); it != a.end(); it++){
d[*it] = d[u] + ;
q.push(*it);
}
a.swap(b);
b.clear();
}
}
int main(){
int t, i, j, x, y, s, f;
scanf("%d", &t);
while(t--){
scanf("%d %d", &n, &m);
memset(d, inf, sizeof(d));
g.clear();
g.resize(N+);
for(i = ; i < m; ++i){
scanf("%d %d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
scanf("%d", &s);
d[s] = ;
bfs(s);
f = ;
for(i = ; i <= n; ++i){
if(i == s)continue;
if(d[i] == inf)
printf("-1\n");
else if(!f){
printf("%d", d[i]);
f = ;
}
else
printf(" %d",d[i]);
}
printf("\n");
}
return ;
}
hdu5876 Sparse Graph(补图最短路 bfs)的更多相关文章
- 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)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意: 有一个 n 个点无向图,再给你 m 对顶点, 代表着这 m 对顶点之间没有边, 除此之外 ...
- HDU 5876 Sparse Graph BFS 最短路
Sparse Graph Problem Description In graph theory, the complement of a graph G is a graph H on the ...
- hdu 5876 Sparse Graph 无权图bfs求最短路
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) P ...
- hdu_5876_Sparse Graph(补图BFS)
题目链接:hdu_5876_Sparse Graph 附上叉姐的题解: 1009 Sparse Graph [by ftiasch] 题意:n 个点的无向完全图中删除 m 条边,问点 s 到其他点的最 ...
- 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
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...
- HDU 5876 大连网络赛 Sparse Graph
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) T ...
- SCU 4444 Travel (补图最短路)
Travel The country frog lives in has \(n\) towns which are conveniently numbered by \(1, 2, \dots, n ...
随机推荐
- vim功能使用
转自:http://blog.csdn.net/xiajun07061225/article/details/7039413 vi与vim vi编辑器是所有Unix及Linux系统下标准的编辑器,他就 ...
- 加快Win7整体运行速度的12个小技巧
在整体运行速度方面,微软Windows 7系统超越了它的前任Vista,拥有明显的提升;但是相比最新的Windows 8,似乎又有所不及,至少很少有Windows用户能够体会到15秒的开机速度.虽然如 ...
- bootstrap-datepicker 日期格式设置
[参考文档]http://bootstrap-datepicker.readthedocs.org/en/latest/ datepicker插件默认是时间格式为mm/dd/yyyy,如何更改时间的格 ...
- centos下安装nginx和php-fpm
安装这两个花了大约七个小时,简直呵呵,安装nginx就是直接 yum install nginx ,但发现一打开php文件就是直接下载该php文件,也就是不能识别php文件,解决这个花了好久,但其实看 ...
- 流媒体基础实践之——RTMP和HLS分发服务器nginx.conmf配置文件(解决了,只能播放RTMP流而不能够播放HLS流的原因)
user www www; worker_processes ; error_log logs/error.log debug; #pid logs/nginx.pid; events { worke ...
- Can of Worms 【迭代/线段树】
题意:一条直线上有n个炸弹,给出每个炸弹的爆炸半径,可以引爆另一个炸弹爆炸.问:每个炸弹爆炸后,最多有几个炸弹一起爆炸? 迭代,用线段树更新. #include <cstdio> #inc ...
- poj1244Slots of Fun
链接 几何的简单题,建立坐标,判断相等以及不共线 #include <iostream> #include<cstdio> #include<cstring> #i ...
- dateTimePicker的使用,时间控件
<li> <label>促销时间<span class="imprt">*</span></label> <inp ...
- python中if __name__ == '__main__'
python 中__name__ = '__main__' 的作用,到底干嘛的? 有句话经典的概括了这段代码的意义: “Make a script both importable and execut ...
- Android最佳性能实践(二)——分析内存的使用情况
由于Android是为移动设备开发的操作系统,我们在开发应用程序的时候应当始终把内存问题充分考虑在内.虽然Android系统拥有垃圾自动回收机制,但这并不意味着我们就可以完全忽略何时去分配或释放内存. ...