HDU 5876 Sparse Graph BFS 最短路
Sparse Graph
Now you are given an undirected graph G of N nodes and M bidirectional edges of unit length. Consider the complement of G, i.e., H. For a given vertex S on H, you are required to compute the shortest distances from S to all N−1 other vertices.
Input
2 0
1

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<set>
using namespace std; #pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair typedef long long LL;
const long long INF = 1e18;
const double Pi = acos(-1.0);
const int N = 2e5+, M = 1e2+, mod = 1e9+, inf = 2e9; int T,n,m,vis[N],head[N],t,d[N],ans[N];
struct ss{int to,next;}e[N * ];
void add(int u,int v) {e[t].to=v;e[t].next=head[u];head[u]=t++;} int main() {
scanf("%d",&T);
while(T--) {
scanf("%d%d",&n,&m);
t =;memset(head,,sizeof(head));
for(int i = ; i <= m; ++i) {
int a,b;
scanf("%d%d",&a,&b);
add(a,b);add(b,a);
}
memset(vis,,sizeof(vis));
memset(d,-,sizeof(d));
int S;
scanf("%d",&S);
queue<int > q;
q.push(S);vis[S] = ;
d[S] = ;
set<int > s;
for(int i = ; i <= n; ++i) if(i != S) s.insert(i),vis[i] = ;
while(!q.empty()) {
int k = q.front();
q.pop();
for(int i = head[k]; i; i =e[i].next) {
int to = e[i].to;
if(s.count(to)) {
vis[to] = ;
}
}
for(set<int > ::iterator itt,it = s.begin();it != s.end(); ) {
if(vis[*it])
{
d[*it] = d[k] + ;
q.push(*it);
itt = it;
itt++;
s.erase(it);
it = itt;
} else {
it++;
}
}
for(set<int > ::iterator itt,it = s.begin();it != s.end(); it++) vis[*it] = ;
}
int cnt = ;
for(int i = ; i <= n; ++i) {
if(i != S)ans[++cnt] = d[i];
}
for(int i = ; i < cnt; ++i) printf("%d ",ans[i]);
printf("%d\n",ans[cnt]);
}
return ;
}
HDU 5876 Sparse Graph BFS 最短路的更多相关文章
- HDU 5876 Sparse Graph BFS+set删点
Problem Description In graph theory, the complement of a graph G is a graph H on the same vertices s ...
- 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求最短路
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) P ...
- 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(补图中求最短路)
http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意: 在补图中求s到其余各个点的最短路. 思路:因为这道题目每条边的距离都是1,所以可以直接用bfs来做 ...
- HDU 5876 Sparse Graph(补图上BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意: 有一个 n 个点无向图,再给你 m 对顶点, 代表着这 m 对顶点之间没有边, 除此之外 ...
- hdu 5876 Sparse Graph icpc大连站网络赛 1009 补图最短路
BFS+链表 代码改自某博客 #include<stdio.h> #include<iostream> #include<algorithm> #include&l ...
- HDU 5876 补图 单源 最短路
---恢复内容开始--- Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (J ...
- HDU 5867 Sparse Graph (2016年大连网络赛 I bfs+补图)
题意:给你n个点m条边形成一个无向图,问你求出给定点在此图的补图上到每个点距离的最小值,每条边距离为1 补图:完全图减去原图 完全图:每两个点都相连的图 其实就是一个有技巧的bfs,我们可以看到虽然点 ...
随机推荐
- iOS 小经验:UIAnimation空对象导致crash
今天调试一个老程序,发现有时运行动画会crash,仔细检查了基本的内存管理,发现都没问题!后来发现了问题,这个程序用的是下面的代码 [self performSelectorInBackground: ...
- 最近开始做Android了
最近开始做Android,在学习的过程中发现找以前知识很不方便啊,于是决定以后还是把知识记录在博客里吧,说不定也能为他人提供参考!
- 5. javacript高级程序设计-引用类型
1. 引用类型 1.1 Object类型 创建Object类型有两种方式: 使用new操作符后跟Object构造函数 var person =new Object(); 字符量表示法 var pers ...
- MySQL Got fatal error 1236原因和解决方法【转】
本文来自:http://blog.itpub.net/22664653/viewspace-1714269/ 一 前言 MySQL 的主从复制作为一项高可用特性,用于将主库的数据同步到从库,在维护主 ...
- js 中 toString( ) 和valueOf( )
1.toString()方法:主要用于Array.Boolean.Date.Error.Function.Number等对象转化为字符串形式.日期类的toString()方法返回一个可读的日期和字符串 ...
- NEFU 503 矩阵求解 (非01异或的高斯消元)
题目链接 中文题,高斯消元模板题. #include <iostream> #include <cstdio> #include <cmath> #include ...
- UliPad双击没反应,UliPad打不开
关于这个问题呢我也是蛋疼了好久,前几天是把这东西卸了重装,然后莫名其妙就可以了. 今天又遇到这问题,第一个想到的也是重装,发现不行,于是就搜了下,发现果然是网能的网友,下面贴图: 经过本屌几次尝试,鉴 ...
- 单击双击手势(UITapGestureRecognizer)
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...
- 一分钟可知css3版大白源码
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- EmguCV学习 与opencv的区别和联系
openCV是因特尔的一个开源的视觉库,里面几乎包含了所有的图像处理的经典算法,并且采用C和少量的C++编写,运行效率很高,对于做图像处理这方面工作的,认识opencv是必须的工作.不过opencv有 ...