题目:这里

题意:

相当于一开始给一个初始好了的无向完全图给你,然后给让你删除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的更多相关文章

  1. HDU 5876 补图 单源 最短路

    ---恢复内容开始--- Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (J ...

  2. HDU 5876 补图最短路

    开两个集合,一个存储当前顶点可以到达的点,另一个存储当前顶点不能到达的点.如果可以到达,那肯定由该顶点到达是最短的,如果不能,那就留着下一次再判. #include<bits/stdc++.h& ...

  3. 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 ...

  4. HDU 5876:Sparse Graph(BFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=5876 Sparse Graph Problem Description   In graph theory, t ...

  5. HDU 5876 Sparse Graph BFS 最短路

    Sparse Graph Problem Description   In graph theory, the complement of a graph G is a graph H on the ...

  6. hdu 5876 Sparse Graph 无权图bfs求最短路

    Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) P ...

  7. HDU 5876 关于补图的bfs

    1.HDU 5876  Sparse Graph 2.总结:好题,把STL都过了一遍 题意:n个点组成的完全图,删去m条边,求点s到其余n-1个点的最短距离. 思路:把点分为两个集合,A为所有没有到达 ...

  8. HDU 5876 Sparse Graph

    Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)To ...

  9. HDU 5876 大连网络赛 Sparse Graph

    Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) T ...

随机推荐

  1. dedecms讲解-arc.listview.class.php分析,列表页展示

    ./plus/list.php - 动态展示栏目列表页(也可能是频道封面)arc.listview.class.php 是dedecms的列表页的相关处理类__construct()         ...

  2. 通过DIV+span方式模拟进度条的实现方法

    上上周用FusionCharts做报表时,有个图是进度条的形式,其实在FusionCharts 3.0之后已经支持了(Linear Gauge),可惜现有系统用的还是1.2.3版本的,重新引入新版本有 ...

  3. UIImage类扩展返回一个带边框的圆形图片

    /** * 将image转换为圆型带边框的图片(最好写一个UIImage的类扩展) * * @param name 图片的名字 * @param borderWidth 外层边框的宽度 * @para ...

  4. 关于JDK 安装,以及Java环境的设置

    关于JDK 安装,以及Java环境的设置 1.下载JDK1.6,选择对应的安装路径 2.配置相应的Java 环境变量 A.属性名称:JAVA_HOME 属性值:C:\Program Files\Jav ...

  5. 在Android中自定义捕获Application全局异常,可以替换掉系统的强制退出对话框(很有参考价值与实用价值)

    转载自: http://blog.csdn.net/jdsjlzx/article/details/7606423

  6. cocos2d-js callFunc传参

    1.传递一个参数: pg.TestScene.prototype.init = function () { if (cc.Scene.prototype.init.call(this)) { var ...

  7. Python:python中math模块中提供的基本数学函数

    sin(x):求x的正弦 cos(x):求x的余弦 asin(x):求x的反正弦 acos(x):求x的反余弦 tan(x):求x的正切 atan(x):求x的反正切 hypot(x,y):求直角三角 ...

  8. php 之跨域上传图片

    因为要将所有上传的图片上传到一台独立的图片服务器上面,js上传时存在跨域问题,网上找到这种,通过php curl方式,将图片重新发送到另外一台服务器上保存,并返回图片路径!这种方式存在一定问题:1,上 ...

  9. 偶的《javascript框架设计》终于出版

    #cnblogs_post_body p{ text-indent:2em!important; } 历时两年多,我的书终于付梓出版了.应各方面的要求,写软文一篇,隆重介绍一下此书对各位程序员的钱途有 ...

  10. 4.struts2中的文件上传,下载

    Struts2中文件的上传下载,是借用commons里面的包实现文件的上传,需要导入两个jar commons-fileupload-1.2.2.jar commons-io-2.0.1.jar 实现 ...