The Best Path ----HDU
来源:互联网 发布:mac 查看系统运行程序 编辑:程序博客网 时间:2024/05/16 09:37
题目连接:
http://acm.hdu.edu.cn/showproblem.php?pid=5883
题目要求求一个图G的补图的各点最短路
想到先存图,然后推补图
点的范围很大,用邻接矩阵显然存不下,如果用邻接表,,时间可能会超。。
之后经过看大佬题解,终于理解。
开两个set集合,把此时能到达的节点存进去,和不能到达的点存进去。。。不断维护这两个set
补图概念,原图有的边,补图中肯定没有
比如,此时NOW点能到达的点集合,,这个集合的补集,就是now在补图中能到达的点,这样就能通过此时得到的set直接bfs最短路
#include<stdio.h>#include<iostream>#include<cstring>#include<set>#include<map>#include<vector>#include<queue>#include<algorithm>#define N 1000008using namespace std;queue<int> que;set<int> ac,nac;vector<int> g[N];int n,dis[N];void solve(int s){ memset(dis, 0, sizeof(dis)); while (!que.empty()) que.pop(); ac.clear(),nac.clear(); que.push(s); for(int i=1;i<=n;i++) { if(s!=i) ac.insert(i); } while (!que.empty()) { int x=que.front(); que.pop(); for(int i=0;i<g[x].size();i++) { if(!ac.count(g[x][i]))continue; nac.insert(g[x][i]); ac.erase(g[x][i]); } for(set<int>::iterator i=ac.begin();i!=ac.end();i++) { dis[*i]=dis[x]+1; que.push(*i); } ac.swap(nac); nac.clear(); } int f=0; for(int i=1;i<=n;i++) { if(i!=s) { if(f) printf(" "); if(dis[i]!=0) printf("%d",dis[i]); else printf("-1"); f=1; } } cout<<endl;}int main(){ int T,m,be,en; cin>>T; while(T--) { cin>>n>>m; for(int i=1;i<=n;i++) g[i].clear(); for(int i=0;i<m;i++) { cin>>be>>en; g[be].push_back(en); g[en].push_back(be); } int s; cin>>s; solve(s); } return 0;}
阅读全文
0 0
- The Best Path ----HDU
- hdu 5883 The Best Path
- HDU 5883 - The Best Path
- HDU-5883-The Best Path
- hdu 5883 The Best Path
- HDU 5883 The Best Path [欧拉图]
- 【23.59%】【hdu 5883】The Best Path
- HDU 5883 The Best Path 欧拉图
- The Best Path--欧拉路
- The Best Path
- hdu5883 The Best Path
- 5883-The Best Path
- HDU 5883 The Best Path 2016青岛网赛
- 青岛网赛 1006 HDU 5883 The Best Path
- hdu 5883 The Best Path-ICPC网络赛青岛赛区
- HDU 5883 The Best Path (枚举 欧拉通路)
- 【HDU 5883】The Best Path(欧拉路+异或和)
- hdu 5883 The Best Path 欧拉路 异或
- python里使用iterrows()对dataframe进行遍历
- 后端处理前端跨域请求
- chm页面无法显示
- matlab、1
- Hibernate之缓存详解
- The Best Path ----HDU
- 关于Java反射机制中的Field简单尝试与示例以及说明
- 解决Spring中singleton的Bean依赖于prototype的Bean的问题
- 算法期中——合并二叉树
- Android Studio提升开发效率小技巧
- jQuery基础
- Python time模块
- 数据结构-位图(可对海量数据进行处理)
- php.ini配置