---恢复内容开始---

Sparse Graph

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2590    Accepted Submission(s): 902

Problem Description
In graph theory, the complement of a graph G is a graph H on the same vertices such that two distinct vertices of H are adjacent if and only if they are not adjacent in G.

Now you are given an undirected graph G of N nodes and M bidirectional edges of unit length. Consider the complement of G, i.e., H. For a given vertex S on H, you are required to compute the shortest distances from S to all N−1 other vertices.

 
Input
There are multiple test cases. The first line of input is an integer T(1≤T<35) denoting the number of test cases. For each test case, the first line contains two integers N(2≤N≤200000) and M(0≤M≤20000). The following M lines each contains two distinct integers u,v(1≤u,v≤N) denoting an edge. And S (1≤S≤N) is given on the last line.
 
Output
For each of T test cases, print a single line consisting of N−1 space separated integers, denoting shortest distances of the remaining N−1 vertices from S (if a vertex cannot be reached from S, output ``-1" (without quotes) instead) in ascending order of vertex number.
 
Sample Input
1
2 0
1
 
Sample Output
1
 
题意  求源点 s 在补图中到其它点的最短路长
解析 因为 给的边比较少 点比较多  补图就是一个非常稠密的图 我们不能建补图 迪杰斯特拉跑一边 会超时的 我们注意到 边权都为1   我们可以直接BFS一层一层求最短路  
每个点都入队一次 然后取队顶在未被访问过的点中找在原图中不与其相邻的点  加入队列 。因为在原图中不与点s相连的点  在补图中最短路都为1   待访问的不超过m个
所以bfs是可行的 但是我们在找 与 当前队顶其不相邻的点 用数组标记 花费时间很大(每次遍历n会超时)我们用set存一下 就好了 只留下待访问的点 时间复杂度就降下来了
 
set    AC代码
 #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <queue>
#include <map>
#include <set>
#include <vector>
using namespace std;
const int maxn=2e5+;
int n,m,s;
vector<int> g[maxn];
int ans[maxn],vis[maxn];
set<int> a,b;
void bfs(int x)
{
for(int i=;i<=n;i++)
if(s!=i)
a.insert(i);
queue<int> q;
q.push(x);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=;i<g[u].size();i++)
{
int v=g[u][i];
if(a.count(v))
{
b.insert(v);
a.erase(v);
}
}
set<int>::iterator it;
for(it=a.begin();it!=a.end();it++)
{
q.push(*it);
ans[*it]=ans[u]+;
}
a.swap(b);
b.clear();
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n>>m;
a.clear(),b.clear();
for(int i=;i<=n;i++)
{
g[i].clear();
}
memset(ans,-,sizeof(ans));
for(int i=;i<m;i++)
{
int u,v;
cin>>u>>v;
g[u].push_back(v);
g[v].push_back(u);
}
cin>>s;
ans[s]=;
// set<int>::iterator j;
// for(j=a.begin();j!=a.end();j++)
// cout<<*j<<endl;
bfs(s);
if(s!=n)
for(int i=;i<=n;i++)
{
if(i!=s)
{
if(i!=n)
cout<<ans[i]<<" ";
else
cout<<ans[n]<<endl;
}
}
else
for(int i=;i<=n-;i++)
{
if(i==n-)
cout<<ans[n-]<<endl;
else
cout<<ans[i]<<" ";
}
}
return ;
}

数组超时代码

 #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <queue>
#include <map>
#include <vector>
using namespace std;
const int maxn=2e5+;
int n,m,s;
vector<int> g[maxn];
int ans[maxn],vis[maxn],a[maxn];
void bfs(int x)
{
vis[x]=;
queue<int> q;
q.push(x);
int cnt=;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=;i<g[u].size();i++)
{
int v=g[u][i];
if(cnt%==)
a[v]=;
else
a[v]=;
}
for(int i=;i<=n;i++)
{
if(cnt%==&&a[i]==&&!vis[i])
{
ans[i]=ans[u]+;
q.push(i);
vis[i]=;
}
if(cnt%==&&a[i]==&&!vis[i])
{
ans[i]=ans[u]+;
q.push(i);
vis[i]=;
}
}
cnt++;
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n>>m;
for(int i=;i<=n;i++)
g[i].clear(),a[i]=;
memset(ans,-,sizeof(ans));
memset(vis,,sizeof(vis));
for(int i=;i<m;i++)
{
int u,v;
cin>>u>>v;
g[u].push_back(v);
g[v].push_back(u);
}
cin>>s;ans[s]=;
bfs(s);
for(int i=;i<=n;i++)
{
if(i!=s)
cout<<ans[i]<<" ";
}
cout<<endl;
}
return ;
}

HDU 5876 补图 单源 最短路的更多相关文章

  1. HDU 5637 Transform 单源最短路

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5637 题意: http://bestcoder.hdu.edu.cn/contests/contes ...

  2. 最短路模板(Dijkstra & Dijkstra算法+堆优化 & bellman_ford & 单源最短路SPFA)

    关于几个的区别和联系:http://www.cnblogs.com/zswbky/p/5432353.html d.每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个(草儿家到 ...

  3. [ACM_图论] Domino Effect (POJ1135 Dijkstra算法 SSSP 单源最短路算法 中等 模板)

    Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...

  4. 用scheme语言实现SPFA算法(单源最短路)

    最近自己陷入了很长时间的学习和思考之中,突然发现好久没有更新博文了,于是便想更新一篇. 这篇文章是我之前程序设计语言课作业中一段代码,用scheme语言实现单源最段路算法.当时的我,花了一整天时间,学 ...

  5. 单源最短路_SPFA_C++

    当我们需要求一个点到其它所有点的最短路时,我们可以采用SPFA算法 代码特别好写,而且可以有环,但是不能有负权环,时间复杂度是O(α(n)n),n为边数,α(n)为n的反阿克曼函数,一般小于等于4 模 ...

  6. 【UVA1416】(LA4080) Warfare And Logistics (单源最短路)

    题目: Sample Input4 6 10001 3 21 4 42 1 32 3 33 4 14 2 2Sample Output28 38 题意: 给出n个节点m条无向边的图,每条边权都为正.令 ...

  7. 【算法系列学习】Dijkstra单源最短路 [kuangbin带你飞]专题四 最短路练习 A - Til the Cows Come Home

    https://vjudge.net/contest/66569#problem/A http://blog.csdn.net/wangjian8006/article/details/7871889 ...

  8. 模板C++ 03图论算法 1最短路之单源最短路(SPFA)

    3.1最短路之单源最短路(SPFA) 松弛:常听人说松弛,一直不懂,后来明白其实就是更新某点到源点最短距离. 邻接表:表示与一个点联通的所有路. 如果从一个点沿着某条路径出发,又回到了自己,而且所经过 ...

  9. 2018/1/28 每日一学 单源最短路的SPFA算法以及其他三大最短路算法比较总结

    刚刚AC的pj普及组第四题就是一种单源最短路. 我们知道当一个图存在负权边时像Dijkstra等算法便无法实现: 而Bellman-Ford算法的复杂度又过高O(V*E),SPFA算法便派上用场了. ...

随机推荐

  1. URAL1389. Roadworks(dp)

    1389 算个简单的树形DP吧 不知道是不是数据太水 竟然一A了 就是对于当前节点有没有被选中就行选最优 有没有被选中的意思是有没有与它相连的边被选中 #include <iostream> ...

  2. [转]无废话SharePoint入门教程二[SharePoint发展、工具及术语]

    本文转自:http://www.cnblogs.com/iamlilinfeng/p/3186919.html 一.前言 1.由于上一篇文章的标题命名失误,此篇标题写给百度搜索”什么是SharePoi ...

  3. Java BigDecimal类的使用和注意事项

    1.对于金额相关运算,若是精度较高,基本上用BigDecimal进行运算,精度要求低的话用Long.Double即可 2.web后台接受金额用String接受,展示到前端一般也转成 String 3. ...

  4. swiper4初始化/swiper-init/data-swiper

    用data属性初始化swiper <!DOCTYPE html> <html lang="en"> <head> <meta charse ...

  5. 在Windows7下编译调试C#程序

    要在 命令行下编译C#代码,要配置一下 1.在环境变量下新建一个变量 参数名: csc 参数值:C:\Windows\Microsoft.NET\Framework\v4.0.30319 2.在系统变 ...

  6. Python 保留n位小数

    输出a, b 且保留三位小数 a = 2.3456 b = 2.0000 三种方法: round(a, 3)('%.3f' % a)Decimal(a).quantize(Decimal('0.000 ...

  7. LeetCode887鸡蛋掉落——dp

    题目 题目链接 你将获得 K 个鸡蛋,并可以使用一栋从 1 到 N  共有 N 层楼的建筑.每个蛋的功能都是一样的,如果一个蛋碎了,你就不能再把它掉下去,如果没有碎可以继续使用.你知道存在楼层 F , ...

  8. CREATE SCHEMA - 定义一个新的模式

    SYNOPSIS CREATE SCHEMA schemaname [ AUTHORIZATION username ] [ schema_element [ ... ] ] CREATE SCHEM ...

  9. Perl字符集就是方括号(或称中括号)里一连串可能的字符,只匹配单一字符,该单一字符可以是字符集里的任何一个,“-”在字符集里有特殊含义:表示某个范围的字符。而字符集意外的连字符不具有特殊意义。

    Perl字符集就是方括号(或称中括号)里一连串可能的字符,只匹配单一字符,该单一字符可以是字符集里的任何一个,“-”在字符集里有特殊含义:表示某个范围的字符.而字符集意外的连字符不具有特殊意义.

  10. [python] 时间序列分析之ARIMA

    1 时间序列与时间序列分析 在生产和科学研究中,对某一个或者一组变量  进行观察测量,将在一系列时刻  所得到的离散数字组成的序列集合,称之为时间序列. 时间序列分析是根据系统观察得到的时间序列数据, ...