题目:这里

题意:

相当于一开始给一个初始好了的无向完全图给你,然后给让你删除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. mysql中修改view的definer

    我常用的工具是navicat,但是修改definer不能用工具,只能用命令行: 数据库迁移到其他服务器上,会报definer错误,修改view的definer方法如下(比如把definer改为本地的, ...

  2. springmvc+mybatis整合

    maven 依赖 <!-- springmvc --> <dependency> <groupId>org.springframework</groupId& ...

  3. 用EXCEL内嵌的Visual Basic for Application 编程,通过 UGSimple USB-GPIB 控制器来驱动仪器34401A,并从34401A读取数据

    现在市场上有很多中USB-GPIB 控制器,或叫 USB 转GPIB链接线. 每种GPIB控制器都有它的 函数库(dll库).各种GPIB 控制器的价钱插别很大.这里以一种价钱较便宜的USB-GPIB ...

  4. spring代理模式 service远程调用,插件执行

    最近,研究了一下平台远程调用的过程,和service层插件执行的原理,记录一下. 1.远程service调用过程 首先看一下类的继承结构 封装调用处理过程 封装service调用接口 封装servic ...

  5. python包下载地址

    https://pypi.python.org/pypi http://www.lfd.uci.edu/~gohlke/pythonlibs/ 当在线安装安装不了时,需要将安装包下载到本地,进行本地p ...

  6. centos 7.2 网卡配置文件 及 linux bridge的静态配置

    在 centos 7.2 系统内, 网卡的配置文件在: /etc/sysconfig/network-scripts/ 下. 命名规则: ifcfg-xxxx.   xxx为设备名称. 通过分析 ne ...

  7. dynamic 的使用 待续

    Dynamic 使用场景之一 : 替代反射 class Me { public string Blog { get; set; } public string GetName() { return&q ...

  8. mat工具MemoryAnalyzer进行分析java内存溢出hprof文件

    java服务端程序报错后会生成hprof文件,我们可以通过mat工具MemoryAnalyzer进行分析 下载地址: http://www.eclipse.org/mat/downloads.php ...

  9. VUE 入门基础(3)

    三,模板语法 Vue将模板编译成虚拟DOM渲染函数,结合响应系统,在应用状态改变时,vue能够智能地计算出重新渲染组件的最小代价并DOM操作上. 插值,文本 数据绑定常见的形式就是使用"Mu ...

  10. elasticsearch同义词及动态更新

    第一种:参考地址:http://dev.paperlesspost.com/setting-up-elasticsearch-synonyms/271.Add a synonyms file.2.Cr ...