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 ...
随机推荐
- idea调节字体大小
这是调节前的 这是调节后的 步骤
- Android中获取屏幕高度和宽度
有时我们需要获取当前屏幕的高度和宽度,只需要在一个Activity的onCreate()方法中写上如下代码即可: //定义DisplayMetrics 对象 DisplayMetrics metric ...
- Spring -- 如何为applicationContext.xml 添加 util 的 *.xsd文件
- android视图概述
android视图概述 一.简介 数据和控件分开的作用: 便于引用 便于修改:修改的时候直接改一次数据就可以了
- 51nod-1201-数位dp
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1201 1201 整数划分 基准时间限制:1 秒 空间限制:131072 ...
- mongodb停止遇到shutdownServer failed: unauthorized: this command must run from localhost when running db without auth解决方法
停止mongodb use admin db.shutdownServer(); mongos> db.shutdownServer(); assert failed : unexpected ...
- JavaScript: Where to Insert JavaScript and output
1.Javescript in <head> <!DOCTYPE html> <html> <head> <script> function ...
- react-redux: modal
1.actionTpye export const INCREMENT = 'INCREMENT'; export const DECREMENT = 'DECREMENT'; export cons ...
- js 设置日期函数
前三十天: var now = new Date(); var prev = now.setDate( now.getDate() - 30 ) vm.sDate = comm.getFormatDa ...
- IOS对存放对象的数组排序
我们开发的每个程序都会使用到一些数据,而这些数据一般被封装在一个自定义的类中.例如一个音乐程序可能会有一个Song类,聊天程序则又一个 Friend类,点菜程序会有一个Recipe类等.有时候我们希望 ...