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

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. 可变类型的安全性——更锋利的C#代码小记(2)

    ReadOnlyCollection类型是.NET系统类库提供的一个只读集合类型,它与原来的List<string>不存在任何类型转换关系,因此可以从根本上阻止外部对其的修改操作using ...

  3. Android优化方案之--Fragment的懒加载实现

    一.背景 在Android应用中,ViewPager是我们不可避免使用的一个控件,因为它可以使我们在占用较少空间的同时,增强内容的丰富性,同时以其内部流淌着Google的血液,所以它几乎成了每一个Ap ...

  4. Android学习笔记(十) Activity的生命周期

    一.如何在一个应用程序中定义多个Activity -定义一个类,继承Activity -复写onCreate() setContentView(R.layout.secondLayout):设定该Ac ...

  5. CAS server 连接mysql的deployerConfigContext.xml配置

    1.deployerConfigContext.xml配置 <?xml version="1.0" encoding="UTF-8"?> <b ...

  6. C# 支持多线程

    C# 支持多线程并行执行程序 .一个程序由一个单线程开始,该单线程由CLR和操作系统创建而成,并具有多线程创建额外线程的功能. .创建线程的方法 2.1 通过Thread类来创建线程. ThreadS ...

  7. XML和JSON

    XML XML(EXtensible Markup Language),可扩展标记语言 特点 XML与操作系统.编程语言的开发平台无关 实现不同系统之间的数据交换 作用: 数据交互 配置应用程序和网站 ...

  8. 06XML JavaScript

    1. XML JavaScript XMLHttpRequest 对象 XML DOM (XML Document Object Model) 定义了访问和操作 XML 文档的标准方法.

  9. 德尔福 XE5 安卓调试

    https://stackoverflow.com/questions/2604727/how-can-i-connect-to-android-with-adb-over-tcp?page=2&am ...

  10. pycharm debug后会出现 step over /step into/step into my code /force step into /step out 分别表示

    1.debug,全部打印 2.打断点debug,出现单步调试等按钮,只运行断点前 3.setup over 调试一行代码 4.setup out 运行断点后面所有代码 5.debug窗口显示调试按钮 ...