HDU 5876 补图 单源 最短路

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

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代码

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <algorithm>
 4 #include <string.h>
 5 #include <stdlib.h>
 6 #include <string>
 7 #include <queue>
 8 #include <map>
 9 #include <set>
10 #include <vector>
11 using namespace std;
12 const int maxn=2e5+10;
13 int n,m,s;
14 vector<int> g[maxn];
15 int ans[maxn],vis[maxn];
16 set<int> a,b;
17 void bfs(int x)
18 {
19     for(int i=1;i<=n;i++)
20         if(s!=i)
21             a.insert(i);
22     queue<int> q;
23     q.push(x);
24     while(!q.empty())
25     {
26         int u=q.front();
27         q.pop();
28         for(int i=0;i<g[u].size();i++)
29         {
30             int v=g[u][i];
31             if(a.count(v))
32             {
33                 b.insert(v);
34                 a.erase(v);
35             }
36         }
37         set<int>::iterator it;
38         for(it=a.begin();it!=a.end();it++)
39         {
40             q.push(*it);
41             ans[*it]=ans[u]+1;
42         }
43         a.swap(b);
44         b.clear();
45     }
46 }
47 int main()
48 {
49     int t;
50     cin>>t;
51     while(t--)
52     {
53         cin>>n>>m;
54         a.clear(),b.clear();
55         for(int i=1;i<=n;i++)
56         {
57             g[i].clear();
58         }
59         memset(ans,-1,sizeof(ans));
60         for(int i=0;i<m;i++)
61         {
62             int u,v;
63             cin>>u>>v;
64             g[u].push_back(v);
65             g[v].push_back(u);
66         }
67         cin>>s;
68         ans[s]=0;
69  //       set<int>::iterator j;
70 //        for(j=a.begin();j!=a.end();j++)
71 //            cout<<*j<<endl;
72         bfs(s);
73         if(s!=n)
74             for(int i=1;i<=n;i++)
75             {
76                 if(i!=s)
77                 {
78                     if(i!=n)
79                         cout<<ans[i]<<" ";
80                     else
81                         cout<<ans[n]<<endl;
82                 }
83             }
84         else
85             for(int i=1;i<=n-1;i++)
86             {
87                 if(i==n-1)
88                     cout<<ans[n-1]<<endl;
89                 else
90                     cout<<ans[i]<<" ";
91             }
92     }
93     return 0;
94 }

数组超时代码

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <algorithm>
 4 #include <string.h>
 5 #include <stdlib.h>
 6 #include <string>
 7 #include <queue>
 8 #include <map>
 9 #include <vector>
10 using namespace std;
11 const int maxn=2e5+10;
12 int n,m,s;
13 vector<int> g[maxn];
14 int ans[maxn],vis[maxn],a[maxn];
15 void bfs(int x)
16 {
17     vis[x]=1;
18     queue<int> q;
19     q.push(x);
20     int cnt=1;
21     while(!q.empty())
22     {
23         int u=q.front();
24         q.pop();
25         for(int i=0;i<g[u].size();i++)
26         {
27             int v=g[u][i];
28             if(cnt%2==1)
29                 a[v]=0;
30             else
31                 a[v]=1;
32         }
33         for(int i=1;i<=n;i++)
34         {
35             if(cnt%2==1&&a[i]==1&&!vis[i])
36             {
37                 ans[i]=ans[u]+1;
38                 q.push(i);
39                 vis[i]=1;
40             }
41             if(cnt%2==0&&a[i]==0&&!vis[i])
42             {
43                 ans[i]=ans[u]+1;
44                 q.push(i);
45                 vis[i]=1;
46             }
47         }
48         cnt++;
49     }
50 }
51 int main()
52 {
53     int t;
54     cin>>t;
55     while(t--)
56     {
57         cin>>n>>m;
58         for(int i=1;i<=n;i++)
59             g[i].clear(),a[i]=1;
60         memset(ans,-1,sizeof(ans));
61         memset(vis,0,sizeof(vis));
62         for(int i=0;i<m;i++)
63         {
64             int u,v;
65             cin>>u>>v;
66             g[u].push_back(v);
67             g[v].push_back(u);
68         }
69         cin>>s;ans[s]=0;
70         bfs(s);
71         for(int i=1;i<=n;i++)
72         {
73             if(i!=s)
74                 cout<<ans[i]<<" ";
75         }
76         cout<<endl;
77     }
78     return 0;
79 }

原文地址:https://www.cnblogs.com/stranger-/p/8666410.html

时间: 2024-08-01 18:02:52

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

【裸单源最短路:Dijkstra算法两种版本】hdu 1874 畅通工程续

Source : hdu 1874 畅通工程续 http://acm.hdu.edu.cn/showproblem.php?pid=1874 Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多.这让行人很困扰. 现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离. Input 本题目包含多组数据,请处理到文件结束.

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

https://vjudge.net/contest/66569#problem/A http://blog.csdn.net/wangjian8006/article/details/7871889 邻接矩阵实现的单源最短路 1 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 #include<cstring> 5 #include<algorithm> 6 #include

Dijkstra算法 --- 单源最短路

Dijkstra算法适用于边权值为正的情况,可用于计算正权图上的单元最短路. 其伪代码如下: 设d[v0] = 0, 其他d[i] = INF 循环n次{ 在所有未标号的结点中,选取d值最小的结点x 给结点x加上永久标号 对于从x出发的所有边,执行松弛操作. } //松弛操作的伪代码如下: RELAX(u,v,w) if(u.d + w(u,v) < v.d){ v.d = w.d + w(u,v); pre[v] = u; } Dijkstra算法代码: /* Dijkstra 单源最短路算法

常见模板(欧拉筛素数,最小生成树,快排,并查集,单源最短路)

欧拉筛素数: #include<cstdio> #define maxn 10000000+10 using namespace std; int n,prime[5000001],num_prime=0,m; bool if_prime[maxn]; void euler(int limit) { for(int i=2;i<=limit;i++) { if(!if_prime[i]) prime[++num_prime]=i; for(int j=1;prime[j]*i<=l

利用分支限界法求解单源最短路(Dijkstra)问题

分支限界法定义:采用BFS算法,并使用剪枝函数的算法称为分支界限法. 分支限界法解释:按广度优先的原则,有选择的在其child中进行扩展,从而舍弃不含有最优解的分支,不断重复这一过程,直到找到答案或者判定无解. 分支界限法常常用到优先队列来选择最佳扩展节点,有时也会用到普通队列,以先进先出为原则来进行筛选. 单源最短路问题定义:给定有向图和起点,寻找到达所有点的最短路径. 单源最短路的分支限界法概述:首先把节点加入优先队列,之后不断地从队列中取出最优扩展点,观察其可抵达的所有目标节点,若当前路径

UVA 658 It&#39;s not a Bug, it&#39;s a Feature! (单源最短路,dijkstra+优先队列,变形,经典)

题意:有n个bug,有m个补丁,每个补丁有一定的要求(比如某个bug必须存在,某个必须不存在,某些无所谓等等),打完出来后bug还可能变多了呢.但是打补丁是需要时间的,每个补丁耗时不同,那么问题来了:要打多久才能无bug?(同1补丁可重复打) 分析: n<=20,那么用位来表示bug的话有220=100万多一点.不用建图了,图实在太大了,用位图又不好玩.那么直接用隐式图搜索(在任意点,只要满足转移条件,任何状态都能转). 但是有没有可能每个状态都要搜1次啊?那可能是100万*100万啊,这样出题

单源最短路_SPFA_C++

当我们需要求一个点到其它所有点的最短路时,我们可以采用SPFA算法 代码特别好写,而且可以有环,但是不能有负权环,时间复杂度是O(α(n)n),n为边数,α(n)为n的反阿克曼函数,一般小于等于4 模板:http://www.cnblogs.com/hadilo/p/5934679.html 我感觉自己讲的不会很好,丢一个链接算了 算法详解:http://www.360doc.com/content/13/1208/22/14357424_335569176.shtml 伪代码是自己写的: 可以

HDU-4849 Wow! Such City! (单源最短路)

Problem Description Doge, tired of being a popular image on internet, is considering moving to another city for a new way of life. In his country there are N (2 ≤N≤ 1000) cities labeled 0 . . . N - 1. He is currently in city 0. Meanwhile, for each pa

再看最短路算法 1 —— 单源最短路

学了多年的算法,最短路问题相当之常见———— 好久没写过最短路的问题了,直到昨天闲的无聊来了一题——BZOJ3402(HansBug:额才发现我弱到只能刷水的地步了TT) 一看这不是明显的单源最短路么呵呵...于是直接上来来了个dijkstra,而且用的是邻接表存储图—— Submit之后,结果却是—— 我立刻被雷到了QAQ...于是立刻改写spfa,结果—— 4000ms+(估计还不止)和192ms究竟是怎样的差距啊QAQ,本人虽然早都听说过spfa的强大性,但是未曾想过差距会如此可怕,于是H