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. SQLServer和MySQL job和 event定时器的差别

    SQLServer和MySQL job和 event定时器的差别

  2. [LeetCode] 207 Course Schedule_Medium tag: BFS, DFS

    There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prereq ...

  3. C语言typeof详解

    前言:     typeof关键字是C语言中的一个新扩展,这个特性在linux内核中应用非常广泛. 一,说明     typeof的参数可以是两种形式:表达式或类型. 1,表达式的的例子:       ...

  4. Leetcode: Binary Tree Postorder Transversal

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  5. GCC编译器ABI

    ABI与EABI 1)ABI(Application Binary Interface for the ARM Architecture),描述了应用程序与cpu内核的低级接口. ABI允许编译好的目 ...

  6. redis和memcached相关

    应该选择哪一种缓存机制 redis相较于memcached更加年轻,功能更加强大. 对小型静态数据进行缓存处理,最具代表性的例子就是HTML代码片段.使用memcached所消耗内存更少. 其他情况下 ...

  7. zw版【转发·台湾nvp系列Delphi例程】HALCON FastThreshold1

    zw版[转发·台湾nvp系列Delphi例程]HALCON FastThreshold1 procedure TForm1.Button1Click(Sender: TObject);var img ...

  8. hdu5195 二分+线段树+拓扑序

    这题说的给了n个点m条边要求保证是一个有向无环图,可以删除至多k条边使得这个图的拓扑序的字典序最大,我们知道如果我们要排一个点的时候一定要考虑比他大的点是否可以.通过拆边马上拆出来,如果可以拆当然是拆 ...

  9. VS添加节点

    很喜欢添加节点来减少代码的长度,方便阅读:VS快捷键和相关设置

  10. Python: ord()函数

    ch() , unichr() , ord() ①chr()函数用一个范围在range(256)内的整数作参数,返回一个对应的字符. >>>chr(65) 'A' ②unichr() ...