博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 5876 Sparse Graph BFS 最短路
阅读量:4481 次
发布时间:2019-06-08

本文共 2755 字,大约阅读时间需要 9 分钟。

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 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 N1 other vertices.
 

Input

 

There are multiple test cases. The first line of input is an integer 
T(1T<35) denoting the number of test cases. For each test case, the first line contains two integers N(2N200000) and M(0M20000). The following M lines each contains two distinct integers u,v(1u,vN) denoting an edge. And S (1SN) is given on the last line.
 

 

Output
 
For each of 
T test cases, print a single line consisting of N1 space separated integers, denoting shortest distances of the remaining N1 vertices from S (if a vertex cannot be reached from S, output ``-1" (without quotes) instead) in ascending order of vertex number.
 

 

Sample Input
 
1 2 0 1
 

 

Sample Output
 
1
 
 
题意:
  
  n 个点的无向完全图中删除 m 条边,问点 s 到其他点的最短路长度。
 
题解:
  
  
#include
#include
#include
#include
#include
#include
#include
#include
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
#define MP make_pairtypedef long long LL;const long long INF = 1e18;const double Pi = acos(-1.0);const int N = 2e5+10, M = 1e2+11, mod = 1e9+7, inf = 2e9; int T,n,m,vis[N],head[N],t,d[N],ans[N]; struct ss{ int to,next;}e[N * 2]; 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 =1;memset(head,0,sizeof(head)); for(int i = 1; i <= m; ++i) { int a,b; scanf("%d%d",&a,&b); add(a,b);add(b,a); } memset(vis,0,sizeof(vis)); memset(d,-1,sizeof(d)); int S; scanf("%d",&S); queue
q; q.push(S);vis[S] = 1; d[S] = 0; set
s; for(int i = 1; i <= n; ++i) if(i != S) s.insert(i),vis[i] = 1; 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] = 0; } } for(set
::iterator itt,it = s.begin();it != s.end(); ) { if(vis[*it]) { d[*it] = d[k] + 1; q.push(*it); itt = it; itt++; s.erase(it); it = itt; } else { it++; } } for(set
::iterator itt,it = s.begin();it != s.end(); it++) vis[*it] = 1; } int cnt = 0; for(int i = 1; i <= n; ++i) { if(i != S)ans[++cnt] = d[i]; } for(int i = 1; i < cnt; ++i) printf("%d ",ans[i]); printf("%d\n",ans[cnt]); } return 0;}

 

转载于:https://www.cnblogs.com/zxhl/p/5869691.html

你可能感兴趣的文章
JQ插件写法 扩展JQ方法
查看>>
[LeetCode&Python] Problem 543. Diameter of Binary Tree
查看>>
226 Invert Binary Tree 翻转二叉树
查看>>
《Pro ASP.NET MVC 3 Framework》学习笔记之十六【示例项目SportsStore】
查看>>
IntelliJ IDEA的安装及永久破解
查看>>
C#多线程编程
查看>>
替换空格
查看>>
IntelliJ IDEA2018.1、2017.3激活
查看>>
Orchard后台控制面板的介绍
查看>>
大二下第一周----开学测试
查看>>
javaweb-servlet生成简单的验证码
查看>>
apache+php+mysql环境搭建时,phpinfo里面没有mysql解决办法
查看>>
2018.10.2浪在ACM 集训队第三次测试赛
查看>>
vue3.0学习笔记(一)
查看>>
jQuery跨域
查看>>
MySQL的explain中的参数说明
查看>>
JAVA基本数据类型、引用数据类型-参数传递详解
查看>>
sun.misc.Unsafe 详解
查看>>
食堂排队问题的一个实现
查看>>
Git 回滚代码的正确姿势
查看>>