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 ...
随机推荐
- JNIjw05
ZC: 这个代码,没有真正的运行测试 1.VC6(CPP)的DLL代码: #include<stdio.h> #include "jniZ_JNIjw05.h" #in ...
- POJ3275:Ranking the Cows(Bitset加速floyd求闭包传递)
Each of Farmer John's N cows (1 ≤ N ≤ 1,000) produces milk at a different positive rate, and FJ woul ...
- HTML5 地理定位 【来自百度应用分享平台】
百度给的地图API接口相当完善,复制过来一下,以后备用 基本使用方法: <!--引入百度地图API--> <scriptsrc="http://api.map.baidu. ...
- oracle,查看锁表
(1)锁表查询的代码有以下的形式:select count(*) from v$locked_object;select * from v$locked_object;(2)查看哪个表被锁select ...
- UIPickerView/UIDatePicker/程序启动的完整过程
一.UIPickerView 1.UIPickerView的常见属性 数据源(用来告诉UIPickerView有多少列多少行) @property(nonatomic,assign) id<UI ...
- CSS:Tutorial four
1.CSS Tables To specify table borders in CSS, use the border property. The example below specifies a ...
- react: typescript-webpack项目基本配置
1.webpack.config.js basic const webpack = require('webpack'); const autoprefixer = require('autopref ...
- less 应用
链接 变量传入, 实现不同方向的三角形
- js 复制粘贴功能记录
最近工作中需要在前端页面中使用代码完成剪贴板的读写,网上搜索了下相应的资料,记录下... 这个功能有两个办法一个是js方式,一个是使用flash 一.JS方法 1.复制 首先复制的过程分为两步曲,无论 ...
- Ubuntu下VirtualBox kernel driver not installed
解決方案: 1.sudo apt-get install virtualbox-ose-source2.cd /usr/src3.sudo tar xjvf virtualbox*.bz24.cd m ...