HDOJ5876(补图的最短路)
Sparse Graph
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1563 Accepted Submission(s): 549
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.
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
using namespace std;
const int MAXN = ;
int n, m, s;
set<int> arc[MAXN];
int d[MAXN];
void bfs(int src)
{
memset(d, , sizeof(d));
set<int> vec;
for(int i = ; i <= n; i++)
{
vec.insert(i);
}
queue<int> que;
que.push(src);
vec.erase(src);
while(!que.empty())
{
int u = que.front(); que.pop();
for(set<int>:: iterator it = vec.begin(); it != vec.end(); it++)
{
int v = *it;
if(arc[u].find(v) == arc[u].end())
{
que.push(v);
d[v] = d[u] + ;
vec.erase(v);
}
}
if(vec.empty())
{
break;
}
}
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d %d", &n, &m);
for(int i = ; i <= n; i++) arc[i].clear();
for(int i = ; i < m; i++)
{
int u, v;
scanf("%d %d", &u, &v);
arc[u].insert(v);
arc[v].insert(u);
}
scanf("%d", &s);
bfs(s);
for(int i = ; i <= n; i++)
{
if(i == s)
{
continue;
}
else
{
if(d[i] == )
{
printf("-1");
}
else
{
printf("%d", d[i]);
}
}
if(i != n)
{
printf(" ");
}
}
printf("\n");
}
return ;
}
HDOJ5876(补图的最短路)的更多相关文章
- 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 ...
- Sparse Graph---hdu5876(set+bfs+补图求最短路)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5876 题意:有一个含有n个点的无向图,已知图的补图含有m条边u, v:求在原图中,起点s到 ...
- HDU 5876 补图 单源 最短路
---恢复内容开始--- Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (J ...
- 图论 - Travel
Travel The country frog lives in has nn towns which are conveniently numbered by 1,2,…,n. Among n(n− ...
- 【ZJOI 2018】线图(树的枚举,hash,dp)
线图 题目描述 九条可怜是一个热爱出题的女孩子. 今天可怜想要出一道和图论相关的题.在一张无向图 $G$ 上,我们可以对它进行一些非常有趣的变换,比如说对偶,又或者说取补.这样的操作往往可以赋予一些传 ...
- BZOJ 1137: [POI2009]Wsp 岛屿 半平面交
1137: [POI2009]Wsp 岛屿 Time Limit: 10 Sec Memory Limit: 162 MBSec Special JudgeSubmit: 165 Solved: ...
- 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(补图中求最短路)
http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意: 在补图中求s到其余各个点的最短路. 思路:因为这道题目每条边的距离都是1,所以可以直接用bfs来做 ...
- SCU 4444 Travel (补图最短路)
Travel The country frog lives in has \(n\) towns which are conveniently numbered by \(1, 2, \dots, n ...
随机推荐
- NFV及vIMS的部署实施
随着5G和物联网等领域的快速发展,移动数据业务飞速增长,而传统电信网络基于专用硬件的架构和封闭式的网元,已经成为运营商拓展新业务的严重障碍.NFV能够根据用户和业务需求灵活动态地进行网络资源配置,实现 ...
- hibernate学习(4)
Hibernate查询方式 1 对象导航查询 (1)根据id查询某个客户,再查询这个客户里面所有的联系人 2 OID查询 (1)根据id查询某一条记录,返回对象 3 hql查询 (1)Query对象, ...
- poj2478欧拉函数
打表欧拉函数,求2到n的欧拉函数和 #include<map> #include<set> #include<cmath> #include<queue> ...
- springboot打Jar包和War包
一:打JAR包 在工程的pom.xml中添加以下依赖 <build> <plugins> <plugin> <groupId>org.springfra ...
- AI实现五子棋机器人(一)
前言: 前几天在 csdn 下载资源的时候才发现自己 csdn 有近 200 的下载积分,看了看共享的资源,哈哈 ... 7年前写的五子棋游戏很受欢迎. 下载地址:新手入门五子棋游戏 刚入行的 ...
- C#中的异常捕获机制(try catch finally)
(转自:http://blog.csdn.net/zevin/article/details/6901489) 一.C#的异常处理所用到关键字try 用于检查发生的异常,并帮助发送任何可能的异常.ca ...
- C# 为什么用接口实例化一个实现该接口的类?
这是多态的体现. 首先接口不能实例化的. 实现接口的类 实例 = new 实现接口的类()// 这样用不好吗? //这样已经不是好不好的问题了,这样的话,要接口有什么用?//用接口就是让实例和实现类的 ...
- Android的方法和属性(2)
1.RadioButton(单选按钮) 嵌入到RsdioGroup中实现单选效果 android:checkedButton="radio的id值" int getCheckedR ...
- auto_ptr, unique_ptr, shared_ptr and weak_ptr智能指针讲解
笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D实战核心技术详解 ...
- cmd 操作WinService
1.运行--〉cmd:打开cmd命令框 2.在命令行里定位到InstallUtil.exe所在的位置 InstallUtil.exe 默认的安装位置是在C:\Windows\Microsoft.NET ...