Sparse Graph---hdu5876(set+bfs+补图求最短路)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5876
题意:有一个含有n个点的无向图,已知图的补图含有m条边u, v;求在原图中,起点s到其他n-1个点的最短距离,默认边的距离为1;
由于点的个数较大,不能建原图,只能从补图入手;从起点s开始,与s不直接相连的点的最短距离是1,然后再从这些点开始搜,循环即可,用队列表示,队列空了就结束了;
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<set>
using namespace std;
#define met(a, b) memset(a, b, sizeof(a))
#define N 400005
#define INF 0x3f3f3f3f
typedef long long LL; vector<vector<int> >G;
int dist[N]; void bfs(int s, int n)
{
set<int> s1, s2;
for(int i=; i<=n; i++)
{
if(i!=s) s1.insert(i);
dist[i] = INF;
}
queue<int>Q;
Q.push(s);
dist[s] = ; while(Q.size())
{
int p = Q.front();Q.pop();
for(int i=,len=G[p].size(); i<len; i++)
{
int q = G[p][i];
if(s1.find(q) == s1.end())continue;///判断q点是否已经确定距离了;
s1.erase(q);
s2.insert(q);
}
set<int>::iterator it;
for(it=s1.begin(); it!=s1.end(); it++)///那些到达不了的点都是可以由p点到达的;
{
dist[*it] = dist[p] + ;
Q.push(*it);
}
s1.swap(s2);///交换s2和s1;
s2.clear();
}
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
int n, m, start;
scanf("%d %d", &n, &m);
G.clear();
G.resize(n+);
for(int i=; i<=m; i++)
{
int u, v;
scanf("%d %d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
scanf("%d", &start);
bfs(start, n);
int f = ;
for(int i=; i<=n; i++)
{
if(i == start) continue;
f++;
if(dist[i] == INF) dist[i] = -;
printf("%d%c", dist[i], f == n-?'\n':' ');
}
}
return ;
}
Sparse Graph---hdu5876(set+bfs+补图求最短路)的更多相关文章
- 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 ...
- hdu 5876 Sparse Graph 无权图bfs求最短路
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) P ...
- hdu5876 Sparse Graph(补图最短路 bfs)
题目链接:hdu5876 Sparse Graph 详见代码.. #include<cstdio> #include<cstring> #include<algorith ...
- 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)
http://acm.hdu.edu.cn/showproblem.php?pid=5876 Sparse Graph Problem Description In graph theory, t ...
- HDU 5876 Sparse Graph BFS 最短路
Sparse Graph Problem Description In graph theory, the complement of a graph G is a graph H on the ...
- 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
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) T ...
- 图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。
这道题目甚长, 代码也是甚长, 但是思路却不是太难.然而有好多代码实现的细节, 确是十分的巧妙. 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, DFS方法都大有裨益, 敬请有兴趣者 ...
随机推荐
- BZOJ1107 : [POI2007]驾驶考试egz
i可以作为起点说明把边反向后可以从1和n到达i. 设fl[i]表示从1到达i至少需要加几条边,fr[i]表示从n到达i至少需要加几条边. 把图上下翻转后,从左往右依次计算fl[i],有fl[i]=i- ...
- JAVA计算文件大小
File f = new File(save_path+File.separator + resouce_id+".zip"); FileInputStream fis = new ...
- 【wikioi】1403 新三国争霸(dp+kruskal)
http://wikioi.com/problem/1403/ 一开始的确感觉和bzoj1003很像,不同的是这里还要求联通,求最小的边. 我们可以想到用最小生成树(为嘛我自己想不到呢..) 我们可以 ...
- Graph database_neo4j 底层存储结构分析(8)
3.8 示例1:neo4j_exam 下面看一个简单的例子,然后看一下几个主要的存储文件,有助于理解<3–neo4j存储结构>描述的neo4j 的存储格式. 3.8.1 neo4j ...
- Qt 程序退出时断言错误——_BLOCK_TYPE_IS_VALID(pHead->nBlockUse),由setAttribute(Qt::WA_DeleteOnClose)引起
最近在学习QT,自己仿写了一个简单的QT绘图程序,但是在退出时总是报错,断言错误: 报错主要问题在_BLOCK_TYPE_IS_VALID(pHead->nBlockUse),是在关闭窗口时报的 ...
- U-Mail邮件服务系统任意文件上传+执行漏洞(runtime缺陷与验证绕过)
http://www.wooyun.org/bugs/wooyun-2010-061859
- DirSync: List of attributes that are synced by the Azure Active Directory Sync Tool
http://social.technet.microsoft.com/wiki/contents/articles/19901.dirsync-list-of-attributes-that-are ...
- 【收集】JAVA多文件 上传文件接口代码 -兼容app
原文:http://www.verydemo.com/demo_c143_i23854.html 我们在 multifile 中可以很容易的发现如何使用,这里就简单说说了,首先在页面上我们需要有这样几 ...
- 文件上传(springMVC+ckeditor)
1.首先添加springMVC文件上传的jar commons-fileupload-1.2.2.jar和commons-io-2.0.1.jar (maven项目可以使用 <dependenc ...
- HDU 5773 最长上升子序列
题意 给出一个序列 问它的最长严格上升子序列多长 这个序列中的0可以被替代为任何数 n的范围给出了1e5 所以平常的O(n*n)lis不能用了 在kuangbin的模板里有O(nlogn)的模板 套上 ...