题目链接: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+补图求最短路)的更多相关文章

  1. 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 ...

  2. hdu 5876 Sparse Graph 无权图bfs求最短路

    Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) P ...

  3. hdu5876 Sparse Graph(补图最短路 bfs)

    题目链接:hdu5876 Sparse Graph 详见代码.. #include<cstdio> #include<cstring> #include<algorith ...

  4. 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 ...

  5. HDU 5876:Sparse Graph(BFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=5876 Sparse Graph Problem Description   In graph theory, t ...

  6. HDU 5876 Sparse Graph BFS 最短路

    Sparse Graph Problem Description   In graph theory, the complement of a graph G is a graph H on the ...

  7. HDU 5876 Sparse Graph

    Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)To ...

  8. HDU 5876 大连网络赛 Sparse Graph

    Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) T ...

  9. 图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。

    这道题目甚长, 代码也是甚长, 但是思路却不是太难.然而有好多代码实现的细节, 确是十分的巧妙. 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, DFS方法都大有裨益, 敬请有兴趣者 ...

随机推荐

  1. BZOJ3276 : 磁力

    按距离建立线段树,维护区间重量最小值 然后跑一遍拓扑,每次将所有能取的加入队尾 #include<cstdio> #include<algorithm> #define N 2 ...

  2. CodeForces 359D (数论+二分+ST算法)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=47319 题目大意:给定一个序列,要求确定一个子序列,①使得该子序 ...

  3. 关于 List<T>

    System.Object   System.Collections.Generic.List<T>   list<string,string>,这种形式本身就是错误的,你可以 ...

  4. topcoder SRM 593 DIV2 RaiseThisBarn

    #include <vector> #include <string> #include <list> #include <map> #include ...

  5. 【TYVJ】1338 QQ农场(最大流+最大权闭合图)

    http://tyvj.cn/Problem_Show.aspx?id=1338 时间才排到rank7,还不快啊囧.isap我常数都写得那么小了... 最大权闭合图看我另一篇博文吧 此题很明显的模型. ...

  6. hiho 毁灭者问题

    描述 在 Warcraft III 之冰封王座中,毁灭者是不死族打三本后期时的一个魔法飞行单位. 毁灭者的核心技能之一,叫做魔法吸收(Absorb Mana): 现在让我们来考虑下面的问题: 假设你拥 ...

  7. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column '??????' in 'field list'

    严重: Servlet.service() for servlet jsp threw exceptioncom.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErro ...

  8. linux下六大IPC机制【转】

    转自http://blog.sina.com.cn/s/blog_587c016a0100nfeq.html linux下进程间通信IPC的几种主要手段简介: 管道(Pipe)及有名管道(named ...

  9. 【GK101 谐波数据生成器】上位机软件升级(版本:1.1)

    ============================= 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:ht ...

  10. 使用java代码,动态给TextView设置drawable

    Drawable country = context.getResources().getDrawable(drawableId); country.setBounds(0, 0, country.g ...