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. 请用漂亮欢呼-------Day38

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/marSmile_tbo/article/details/31108557 周末,双休,疯了两天.敲了 ...

  2. Spark DataFrame vector 类型存储到Hive表

    1. 软件版本 软件 版本 Spark 1.6.0 Hive 1.2.1 2. 场景描述 在使用Spark时,有时需要存储DataFrame数据到Hive表中,一般的存储方式如下: // 注册临时表 ...

  3. mybatis中获取参数

    1.${parameter}方式: parameter是数字时用:模糊查询%${parameter}%时用. 例:select * from account where userId = ${para ...

  4. PAT 1030 Travel Plan[图论][难]

    1030 Travel Plan (30)(30 分) A traveler's map gives the distances between cities along the highways, ...

  5. Linux实验楼学习之二

    新建文件1-1.c touch 1-1.c 编辑文件1-1.c gedit 1-1.c 生成可执行文件1-1.c gcc -o 1-1 1-1.c 执行可执行文件1-1 ./1-1

  6. Java多线程的下载器(1)

    实现了一个基于Java多线程的下载器,可提供的功能有: 1. 对文件使用多线程下载,并显示每时刻的下载速度. 2. 对多个下载进行管理,包括线程调度,内存管理等. 一:单个文件下载的管理 1. 单文件 ...

  7. 001-Two Sum

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  8. webpack 3 & React 的配置 。

    今天真是难过的一天

  9. Makefile小结

    Makefile最基本的规则:target....:prerequisites..... command 或:target....:prerequisites.....;command target: ...

  10. cc150 --链表中倒数第k个节点

    题目描述 输入一个链表,输出该链表中倒数第k个结点.   快指针先走K步,然后快慢同时走,快走到末尾时,慢指针就是倒数第个.     public class Solution { public Li ...