博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hdu 5876 SparseGraph 2016ACM ICPC 大连网络赛
阅读量:7121 次
发布时间:2019-06-28

本文共 3692 字,大约阅读时间需要 12 分钟。

Sparse Graph

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)

Total Submission(s): 2612    Accepted Submission(s): 911

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 N1 other vertices.
 

 

Input
There are multiple test cases. The first line of input is an integer
T(1T<35) denoting the number of test cases. For each test case, the first line contains two integers N(2N200000) and M(0M20000). The following M lines each contains two distinct integers u,v(1u,vN) denoting an edge. And S (1SN) is given on the last line.
 

 

Output
For each of
T test cases, print a single line consisting of N1 space separated integers, denoting shortest distances of the remaining N1 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
 
题意:给定一个N个顶点,M条边的无向图G,求其补图H中给定的源点S到其他N-1个点的最短距离,每条边的长度为单位长度1。
 
解法:利用补图求最短路,由于点比较多,所以利用链式前向星存图,建图后将已存在边标记,然后BFS收缩补图。
 
补图:图G的补图,通俗的来讲就是完全图K
n去除G的边集后得到的图K
n-G。在 里面,一个图
G
补图(complement)或者 (inverse)是一个图有着跟
G相同的点,而且这些点之间有边相连 在
G里面他们没有边相连。
的补图就是

 

对于BFS我们这样编写

void bfs(int start,int n){    set
s1; set
s2; set
::iterator it; for(int i=1;i<=n;i++) { if(i!=start) s1.insert(i); } dis[start]=0; queue
que; que.push(start); while(!que.empty()) { int u=que.front(); que.pop(); for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].in; if(!s1.count(v))continue; s1.erase(v); s2.insert(v); } for(it=s1.begin();it!=s1.end();it++) { que.push(*it); dis[*it]=dis[u]+1; } s1.swap(s2); s2.clear(); }}

利用两个set容器进行对已求得最短路的点、未求得的点和连接了原图边的点的区分和筛选。

例如对上诉图

先将3和2编入再依次对剩下的点进行搜索。

 

 

 最后得到定点搭配其他点的最短路

全部代码如下
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define zuida 100000using namespace std;#define INF 0x3f3f3f3fstruct node{ int in,next,vil;}edge[1000000];int dis[1000000],vis[1000000],head[1000000];int totl;void init(){ totl=0; memset(head,-1,sizeof(head));}void add(int u,int v,int val){ edge[totl].in=v; edge[totl].vil=val; edge[totl].next=head[u]; head[u]=totl++;}void bfs(int start,int n){ set
s1; set
s2; set
::iterator it; for(int i=1;i<=n;i++) { if(i!=start) s1.insert(i); } dis[start]=0; queue
que; que.push(start); while(!que.empty()) { int u=que.front(); que.pop(); for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].in; if(!s1.count(v))continue; s1.erase(v); s2.insert(v); } for(it=s1.begin();it!=s1.end();it++) { que.push(*it); dis[*it]=dis[u]+1; } s1.swap(s2); s2.clear(); }}int main(){ int T; scanf("%d",&T); while(T--) { init(); int n,m; int a,b,c; cin>>n>>m; for(int i=1;i<=m;i++) { scanf("%d%d",&a,&b); add(a,b,1); add(b,a,1); } scanf("%d",&c); bfs(c,n); for(int i=1;i<=n;i++) { if(i==c)continue; if(dis[i]!=INF)printf("%d",dis[i]); else printf("-1"); if(i!=n)printf(" "); else printf("\n"); } } return 0;}

 

 

 

 

 

转载于:https://www.cnblogs.com/SparkPhoneix/p/8710784.html

你可能感兴趣的文章
区块链应用场景:征信和权属管理
查看>>
邱剑 | 美团云容器实践之路
查看>>
js实现限制输入框只能输入数字
查看>>
营销人员为何要读《笑傲江湖》?
查看>>
《Microduino实战》——3.5 I/O操作——现学现用
查看>>
《网页设计与前端开发 Dreamweaver+Flash+Photoshop+HTML+CSS+JavaScript 从入门到精通》—— 1.2 网页的基本构成元素...
查看>>
《21天学通Java(第6版)》—— 1.1 Java语言
查看>>
《图数据库》——第 2 章 关联数据的存储选择
查看>>
《SQL学习指南(第2版)(修订版)》———1.4 内容前瞻
查看>>
使用Redis作为一个LRU缓存
查看>>
《易学C++(第2版)》——1.7 C++学习的常见问题
查看>>
《Google软件测试之道》—第1章1.3节组织结构
查看>>
Processing编程学习指南3.1 程序的运行流程
查看>>
ROS机器人程序设计(原书第2版)2.2 理解ROS计算图级
查看>>
Predicate和Consumer接口– Java 8中java.util.function包下的接口
查看>>
前端常见兼容问题系列5:¥符号在部分Android APP的WebView中不见了
查看>>
基于Reactjs实现webapp(加精)
查看>>
超强、超详细Redis数据库入门教程
查看>>
《C++语言基础》实践项目——多重继承
查看>>
京颐集团上云之路:如何助力中小型医疗行业信息化与全面上云?
查看>>