hdu-5876-Sparse Graph
2016-09-12 12:42
375 查看
Sparse Graph
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
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 notadjacent 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
题意:给定一个具有n个点,m条边的无向图G,求在其“补图”中一起点st到其余所有定点的最短距离。
题目链接:Sparse Graph
解题思路:
注意到G图中最多只有200 00条边,但定点可达2 000 00,是稀疏图,因此还原其补图H的话肯定受不了时间,
考虑枚举G图中的边,只要任意点u到v没边,那么在H中就是有边的,因此只要处理G图中有边的顶点,然后从已
经处理好的顶点Dijkstra求最短路即可,
但G图中的边少,意味着H图中很多点之间都是有边的,那么循环过程中可以很快就能求除答案,需要提前退出循
环,cnt记录还需处理的顶点个数
代码:
#include<cstdio> #include<algorithm> #include<vector> #include<cstring> #include<string> #include<cmath> #include<iostream> #include<map> #include<queue> #include<list> using namespace std; typedef long long LL; const int N = 1e5; int n, m, st; vector<int> G[N*2+10]; struct node { int u, c; node(int uu, int cc) : u(uu), c(cc) {} node() {} bool operator < (const node& t) const { return c > t.c; } }; void solve() { int path[n+10], cnt = G[st].size();//cnt为需要处理的点 for(int i = 0;i <= n;i++) { path[i] = 1; } priority_queue<node> qe; for(int i = 0,k = G[st].size();i < k;i++) { int v = G[st][i]; path[v] = 0; //需要处理 } for(int i = 1;i <= n;i++) {//从已经处理好的点开始,处理需要处理的点 if(path[i] == 1) { qe.push(node (i,1) ); } } while(!qe.empty() && cnt > 0) { node u = qe.top(); qe.pop(); for(int i = 1, j, k;i <= n;i++) if(path[i] == 0){ //因为边最多只有5500条,所以这个循环执行的次数只有几次 for(j = 0,k = G[u.u].size();j < k;j++) { //枚举u的边 int v = G[u.u][j]; if(v == i) break; //i点到u点在G图中有路,则在H图中无法确定 } if(j == k) {//可以确定起点到i点的距离 path[i] = u.c+1; cnt--;//printf("st %d i = %d c = %d cnt =%d\n",u.u,i,path[i],cnt); qe.push(node (i,u.c+1)); } } } for(int i = 1;i <= n;i++) {//输出 if(i == st) continue; if(path[i] == 0) path[i] = -1; printf("%d%c",path[i],i == n?'\n':' '); } if(st == n) printf("\n"); } void init() { for(int i = 1;i <= n;i++) { G[i].clear(); } } int main() { int T; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); init(); int u, v; for(int i = 0;i < m;i++) { scanf("%d%d",&u, &v); G[u].push_back(v); G[v].push_back(u); } scanf("%d",&st); solve(); } return 0; }
相关文章推荐
- HDU 5876 Sparse Graph
- HDU 5876 Sparse Graph
- HDU 5876 Sparse Graph
- HDU 5876 Sparse Graph
- Sparse Graph(HDU 5876)
- HDU - 5876 Sparse Graph (补图的最短路)
- HDU 5876 Sparse Graph (补图BFS+(链表||set))
- HDU 5876 Sparse Graph
- HDU-5876 Sparse Graph
- HDU 5876 Sparse Graph
- HDU 5876 - Sparse Graph
- HDU 5876 Sparse Graph(bfs求解补图中的单源最短路)——2016 ACM/ICPC Asia Regional Dalian Online
- HDU 5876 Sparse Graph(2016 ACM/ICPC Asia Regional Dalian Online)
- HDU 5876 Sparse Graph (补图找最短路/BFS)
- HDU-5876-Sparse Graph(BFS)
- HDU 5876 Sparse Graph【补图BFS+set】
- hdu 5876 Sparse Graph 无权图bfs求最短路
- HDU 5876 Sparase Graph(补图上的bfs)
- HDU-5876 & SCU-4444 (完全图的最短路)
- hdu 5876