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 ...
随机推荐
- 共享onload事件
在做前端工作中,我们想要设置某个函数prepare,让它在网页加载完毕后执行,会触发一个onload事件,这个事件与windows对象相关联,必须把prepare函数绑定到这个时间上,语法如下:win ...
- C#实现获取文本文件的编码的一个类(区分GB2312和UTF8)-来自转载收集
using System; using System.IO; using System.Text; /// <summary> /// FileEncoding 的摘要说明 /// < ...
- Upload files to aliyunOSS with bootstrap-fileinput
本文主要涉及两个概念: 阿里云OSS:对象存储(Object Storage Service,简称OSS),是阿里云对外提供的海量.安全和高可靠的云存储服务. bootstrap-fileinput: ...
- python 实现 斐波那契数列
#!usr/bin/python#_*_coding=utf-8_*_ def fin(n): li=[0,1] for i in range(2,n): li.append(li[-1]+li[-2 ...
- Windows Server 2008下asp+access无法登陆问题总结
今日把一套陈旧的企业办公平台部署至公司新采购的服务器,因为在本机windows7环境已经反复测试通过.本以为分分钟完成的事情,结果折腾了我2天.服务器系统:windows server 2008 r2 ...
- [JS]递归对象或数组
function recursive(obj) { var output = ''; if (typeof obj === 'object') { for (var key in obj) { var ...
- Office OneNote 自动打开问题
你可能遇到如下问题: 在输入某些文本的时候,突然onenote自动打开 切换输入法,onenote自动打开 使用某些快捷键,onenote自动打开 最近我就遇到类似诡异的问题,使用了各种搜索引擎,都没 ...
- PHP实现队列及队列原理
看看各语言实现队列的方法:PHP实现队列:第一个元素作为队头,最后一个元素作为队尾 <?php /** * 队列就是这么简单 * * @link http://www.phpddt.com */ ...
- mysql使用小技巧
1.mySql 删除表中大批量的数据 假设有一个表(logs)有2000万条记录,我们要在业 务不停止的情况下删除其中status=1的所有记录,差不多有1800万条,直接执行 DELETE FROM ...
- makefile:2: *** 遗漏分隔符 。 停止
from http://hi.baidu.com/��֮��/blog/item/8ec00e2aca65a525d42af11b.html 我们在编写完makefile,然后在终端中 $make出现 ...