hdu5876Sparse Graph
来源:互联网 发布:ipad可以淘宝直播吗 编辑:程序博客网 时间:2024/06/11 03:33
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5876
题意:给定一个无向图的m条边,求其补图以点s为起点的最短路。
分析:用一个队列维护用于更新的点和一个set维护哪些点还没有被更新,每次将于当前点直接相连的边丢进没有被更新的set中即可。
代码:
#include<map>#include<set>#include<cmath>#include<queue>#include<bitset>#include<math.h>#include<vector>#include<string>#include<stdio.h>#include<cstring>#include<iostream>#include<algorithm>#pragma comment(linker, "/STACK:102400000,102400000")using namespace std;const int N=200010;const int mod=1000000007;const int MOD1=1000000007;const int MOD2=1000000009;const double EPS=0.00000001;typedef long long ll;const ll MOD=1000000007;const int INF=1000000010;const ll MAX=1ll<<55;const double eps=1e-5;const double inf=~0u>>1;const double pi=acos(-1.0);typedef double db;typedef unsigned int uint;typedef unsigned long long ull;queue<int>D;set<int>S,B;vector<int>Q[N];int ans[N];int main(){ int a,b,i,n,m,s,T,now; scanf("%d", &T); while (T--) { scanf("%d%d", &n, &m); for (i=1;i<=n;i++) Q[i].clear(); for (i=1;i<=m;i++) { scanf("%d%d", &a, &b); Q[a].push_back(b);Q[b].push_back(a); } scanf("%d", &s); for (i=1;i<=n;i++) ans[i]=-1; ans[s]=0;D.push(s);S.clear(); for (i=1;i<=n;i++) if (i!=s) S.insert(i); while (!D.empty()) { now=D.front();D.pop(); for (i=0;i<Q[now].size();i++) if (ans[Q[now][i]]==-1) { S.erase(Q[now][i]);B.insert(Q[now][i]); } for (set<int>::iterator it=S.begin();it!=S.end();it++) { ans[*it]=ans[now]+1;D.push(*it); } S.clear(); for (set<int>::iterator it=B.begin();it!=B.end();it++) S.insert(*it); B.clear(); } if (s==n) { for (i=1;i<n-1;i++) printf("%d ", ans[i]); printf("%d\n", ans[n-1]); } else { for (i=1;i<n;i++) if (i!=s) printf("%d ", ans[i]); printf("%d\n", ans[n]); } } return 0;}
0 0
- hdu5876Sparse Graph
- HDU5876Sparse Graph(补图最短路)
- hdu5876Sparse Graph(求补图的最短路)
- graph
- Graph
- Graph
- graph
- Graph
- Graph
- Graph
- graph
- Graph
- Graph
- graph
- Graph
- Graph
- graph
- Graph
- Linux搭建SVN服务器
- Servlet 客户端 HTTP 请求
- <中秋节番外>编程之美之课后练习(一)
- 关于jtag
- 逆序对在OI中实际问题里的细节处理
- hdu5876Sparse Graph
- Centos7安装Redis
- 排序算法总结
- Linux进程与线程的区别
- USACO 2.1 Ordered Fractions
- LeetCode[312. Burst Balloons] 难度[hard]
- 46. Permutations
- iOS逆向工程二:Mac工具集之class-dump的使用
- Linux发行版的基础目录名称、命名法则及功能规定