http://acm.hdu.edu.cn/showproblem.php?pid=5876

题意:

在补图中求s到其余各个点的最短路。

思路:
因为这道题目每条边的距离都是1,所以可以直接用bfs来做。

处理的方法是开两个集合,一个存储当前顶点可以到达的点,另一个存储当前顶点不能到达的点。如果可以到达,那肯定由该顶点到达是最短的,如果不能,那就留着下一次再判。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn=+; int T;
int n, m, s;
int d[maxn];
vector<int> G[maxn]; void bfs()
{
queue<int> Q;
Q.push(s);
set<int> t1,t2;
set<int>::iterator it;
for(int i=;i<=n;i++) if(i!=s) t1.insert(i);
while(!Q.empty())
{
int u=Q.front(); Q.pop();
for(int i=;i<G[u].size();i++)
{
int v=G[u][i];
if(!t1.count(v)) continue;
t1.erase(v); //删去u点不能到达的点
t2.insert(v); //将这些点放入t2中供下一次使用
}
for(it=t1.begin();it!=t1.end();it++) //这些点都是u能到达的
{
d[*it]=d[u]+;
Q.push(*it);
}
t1.swap(t2); //t2中存储的是还没到达过的点
t2.clear();
}
bool flag=true;
for(int i=;i<=n;i++)
{
if(i==s) continue;
if(!flag) printf(" ");
if(flag) flag=false;
if(d[i]==-) printf("-1");
else printf("%d",d[i]);
}
printf("\n");
} int main()
{
//freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) G[i].clear();
while(m--)
{
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
scanf("%d",&s);
memset(d,-,sizeof(d));
d[s]=;
bfs();
}
return ;
}

HDU 5876 Sparse Graph(补图中求最短路)的更多相关文章

  1. HDU 5876 Sparse Graph(补图上BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意: 有一个 n 个点无向图,再给你 m 对顶点, 代表着这 m 对顶点之间没有边, 除此之外 ...

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

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

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

  4. HDU 5876 Sparse Graph

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

  5. HDU 5876 Sparse Graph BFS 最短路

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

  6. hdu 5876 Sparse Graph icpc大连站网络赛 1009 补图最短路

    BFS+链表 代码改自某博客 #include<stdio.h> #include<iostream> #include<algorithm> #include&l ...

  7. HDU 5876 Sparse Graph BFS+set删点

    Problem Description In graph theory, the complement of a graph G is a graph H on the same vertices s ...

  8. HDU 3416 Marriage Match IV (求最短路的条数,最大流)

    Marriage Match IV 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/Q Description Do not si ...

  9. HDU 5867 Sparse Graph (2016年大连网络赛 I bfs+补图)

    题意:给你n个点m条边形成一个无向图,问你求出给定点在此图的补图上到每个点距离的最小值,每条边距离为1 补图:完全图减去原图 完全图:每两个点都相连的图 其实就是一个有技巧的bfs,我们可以看到虽然点 ...

随机推荐

  1. 全局ID的重要性

    全局ID的重要性 体现在sharding的时候 gtid MySQL:global transaction id uuid:universally unique identifier guid:glo ...

  2. 【深入理解javascript】闭包

    1.作用域 “javascript没有块级作用域”.所谓“块”,就是大括号“{}”中间的语句.例如if语句: 再比如for语句: 所以,我们在编写代码的时候,不要在“块”里面声明变量,要在代码的一开始 ...

  3. Ubuntu搭建solr搜索服务器

    参考:http://blog.csdn.net/makang110/article/details/50971705 一:搭建solr服务器 1:安装jdk1.7,并配置环境变量 2:下载tomcat ...

  4. POJ2488:A Knight's Journey(dfs)

    http://poj.org/problem?id=2488 Description Background The knight is getting bored of seeing the same ...

  5. Andrew Ng-ML-第十二章-机器学习系统设计

    1.确定执行的优先级 图1.邮件垃圾分类举例 选择100个单词作为指示是否是垃圾邮件的指标,将这些单词作为特征向量,只用0/1表示,出现多次也只用1表示,特征变量用来表示邮件. 通常情况下,会选择训练 ...

  6. 去掉python的警告

    1.常规警告 import warnings warnings.filterwarnings("ignore") 2.安装gensim,在python中导入的时候出现一个警告: w ...

  7. selenium webdriver窗口切换(上)

    selenium webdriver窗口切换,有时候在做自动化的时候需要打开很多很多的页面, 当在操作不同的页面的时候需要切换窗口,下面是如何切换到前后页面窗口的操作: package test201 ...

  8. 企业中如何批量更改mysql中表的存储引擎?

    一.首先必须熟悉Mysql中有哪些基本的数据库,在mysql中database等价于schema,默认的基本库有四个:mysql,information_schema,performance_sche ...

  9. SV搭建验证环境

    1)首先定义纯虚类Sv_object,主要实现下边两个function: 定义local static 变量nextobjectID; 虚方法 virtual function void copy(S ...

  10. cda转MP3

    首先启动Windows Media Player播放器(依次单击“开始”——“所有程序”——“附件”——“娱乐”——“Windows Media Player”     在工具栏切换到“翻录”选项,这 ...