hdu 5876 (补图BFS) Sparse Graph
题目:这里
题意:
相当于一开始给一个初始好了的无向完全图给你,然后给让你删除m条边,再给你一个点v,最后问你在剩下的图里从这个点v出发能到达所有边点的最小路径是多少?
一看是所有点的最小路径,一看就觉得是个bfs,记忆化搜一下然后加个优化什么的,由于数据不知道是个什么奇葩而且比赛中还改数据,所以很多人wa的莫名其妙,
过也过的莫名其妙,我虽然过了但觉得有点不靠谱,赛后看了https://async.icpc-camp.org/d/546-2016的题解思路写了一发,总感觉更靠谱一点。
之前自己过的,就是用set记录那删掉的m条边,dis[i]数组记录每个结点的最小路径,当所有的点都搜到过的时候就可以结束了
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<set>
#include<queue>
using namespace std; const int M = 2e5 + ;
set<int>s[M];
int n;bool vis[M];
int dis[M],ans; int min(int x,int y){return x<y?x:y;} struct node{
int po,dis;
}; void bfs(int pos)
{
memset(vis,false,sizeof(vis));
queue<node>p;
node now,next;
now.po=pos;now.dis=;
p.push(now);
vis[pos]=true;
while (!p.empty()){
now=p.front();
p.pop();
for (int i= ; i<=n ; i++){
next.po=i;next.dis=now.dis+;
if (next.po==now.po) continue;
if (vis[next.po]) continue;
bool flag=false;
if (s[now.po].find(next.po)!=s[now.po].end())
flag=true;
if (flag) continue;
vis[next.po]=true;
dis[next.po]=min(next.dis,dis[next.po]);
p.push(next);ans++;
}
if (ans==n) return ;
}
} int main()
{
int t;
scanf("%d",&t);
while (t--){
int m;
scanf("%d%d",&n,&m);
for (int i= ; i<=n ; i++) s[i].clear(),dis[i]=M;
while (m--){
int u,v;
scanf("%d%d",&u,&v);
s[u].insert(v);
s[v].insert(u);
}
int pos;
scanf("%d",&pos);
ans=;
bfs(pos);
if (n!=pos){
for (int i= ; i<=n ; i++){
if (i==pos) continue;
if (i!=n) printf("%d ",dis[i]);
else printf("%d\n",dis[i]);
}
}
else{
for (int i= ; i<n ; i++){
if (i!=n-) printf("%d ",dis[i]);
else printf("%d\n",dis[i]);
}
}
}
return ;
}
后来看了别人的思路自己写的
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<set>
#include<queue>
using namespace std; const int M = 2e5 + ; int n,di[M],head[M],cas; struct Edge{
int to,next;
}edge[M*]; void add(int u,int v)
{
edge[++cas].next=head[u];
edge[cas].to=v;
head[u]=cas;
} int min(int x,int y){return x<y?x:y;} struct node{
int po,dis;
}; void bfs(int pos)
{
set<int>s,e;
set<int>::iterator it;
for (int i= ; i<=n ; i++) s.insert(i),di[i]=M;
queue<node>p;
s.erase(pos);
node now,next;
now.po=pos;now.dis=;
p.push(now);
while (!p.empty()){
now=p.front();
p.pop();
for (int i=head[now.po] ; i ; i=edge[i].next){
int v=edge[i].to;
if (s.find(v)==s.end()) continue;
s.erase(v);
e.insert(v);
}
for (it=s.begin() ; it!=s.end() ; it++){
next.po=*it;next.dis=now.dis+;
di[next.po]=min(next.dis,di[next.po]);
p.push(next);
}
s.swap(e);e.clear();
}
} int main()
{
int t;
scanf("%d",&t);
while (t--){
int m;cas=;
scanf("%d%d",&n,&m);
memset(head,,sizeof(head));
while (m--){
int u,v;
scanf("%d%d",&u,&v);
add(u,v);add(v,u);
}
int pos;
scanf("%d",&pos);
bfs(pos);
if (n!=pos){
for (int i= ; i<=n ; i++){
if (i==pos) continue;
if (i!=n) printf("%d ",di[i]);
else printf("%d\n",di[i]);
}
}
else{
for (int i= ; i<n ; i++){
if (i!=n-) printf("%d ",di[i]);
else printf("%d\n",di[i]);
}
}
}
return ;
}
hdu 5876 (补图BFS) Sparse Graph的更多相关文章
- HDU 5876 补图 单源 最短路
---恢复内容开始--- Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (J ...
- HDU 5876 补图最短路
开两个集合,一个存储当前顶点可以到达的点,另一个存储当前顶点不能到达的点.如果可以到达,那肯定由该顶点到达是最短的,如果不能,那就留着下一次再判. #include<bits/stdc++.h& ...
- HDU 5876 Sparse Graph 【补图最短路 BFS】(2016 ACM/ICPC Asia Regional Dalian Online)
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...
- HDU 5876:Sparse Graph(BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=5876 Sparse Graph Problem Description In graph theory, t ...
- HDU 5876 Sparse Graph BFS 最短路
Sparse Graph Problem Description In graph theory, the complement of a graph G is a graph H on the ...
- hdu 5876 Sparse Graph 无权图bfs求最短路
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) P ...
- HDU 5876 关于补图的bfs
1.HDU 5876 Sparse Graph 2.总结:好题,把STL都过了一遍 题意:n个点组成的完全图,删去m条边,求点s到其余n-1个点的最短距离. 思路:把点分为两个集合,A为所有没有到达 ...
- HDU 5876 Sparse Graph
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...
- HDU 5876 大连网络赛 Sparse Graph
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) T ...
随机推荐
- sybase ODBC驱动
windows64位系统ODBC数据源管理器位置 64位 C:\Windows\System32\odbcad32.exe 32位 C:\Windows\SysWOW64\odbcad32.exe s ...
- Robotframework框架AndroidLibrary库安装
1.Ruby官网(http://rubyinstaller.org/)下载系统对应安装包进行安装 2.Ruby官网(http://rubyinstaller.org/)下载对应DevKit,运行解压到 ...
- Parse the main function arguments
int main(int argc, char **argv) { std::string reportDir; std::string transURL; std::string visualEle ...
- [JSP]解决Maven创建项目失败
来源:http://lovespss.blog.51cto.com/1907593/522225 新建Maven项目时遇到这个错误: Unable to create project from arc ...
- 利用cubieboard设置samba打印服务器
#注意安装下面软件前,先将cubieboard的动态地址改为静态地址! apt-get install samba #安装samba vi /etc/samba/smb.conf //配置 workg ...
- [转载]jquery中attr和prop的区别
在高版本的jquery引入prop方法后,什么时候该用prop?什么时候用attr?它们两个之间有什么区别?这些问题就出现了. 关于它们两个的区别,网上的答案很多.这里谈谈我的心得,我的心得很简单: ...
- json与对象转化
/// <summary> /// 把JSON字符串还原为对象 /// </summary> /// <typeparam name="T">对 ...
- UIButton中setTitleEdgeInsets和setImageEdgeInsets的使用
UIButton内有两个控件titleLabel和imageView,可以用来显示一个文本和图片,这里的图片区别于背景图片.给UIButton设 置了title和image后,它们会图片在左边,文本在 ...
- 关于GC的几篇文章
http://msdn.microsoft.com/zh-cn/magazine/bb985010(en-us).aspx http://msdn.microsoft.com/zh-cn/magazi ...
- linux组、用户操作相关
Linux删除用户组和用户时常用的一些命令和参数.1.从组中删除用户编辑/etc/group 找到GROUP1那一行,删除 A或者用命令gpasswd -d A GROUP2.建用户:adduser ...