BFS+链表

代码改自某博客

 #include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 2e5+;
const int INF = 0x3f3f3f3f;
const int A=INF;
const int B=;
typedef long long LL;
int h[maxn],e;
LL dis[maxn];
bool vis[maxn];
int start; struct Edge
{
int v,nxt,w;
} E[maxn<<]; void init()
{
e=;
memset(h,-,sizeof h);
memset(vis,false,sizeof vis);
} void add(int u,int v,int w)
{
E[e].v = v;
E[e].w = w;
E[e].nxt = h[u];
h[u] = e++;
} struct QNode
{
int v,c;
QNode(int _v,int _c)
{
v = _v;
c=_c;
}
bool operator < (const QNode &a) const
{
return c>a.c;
}
}; void dijkstra(int n)
{
memset(dis,INF,sizeof dis);
priority_queue<QNode>Q;
dis[] = ;
Q.push(QNode(,));
while(!Q.empty())
{
QNode tmp = Q.top();
Q.pop();
int u = tmp.v;
if(vis[u]) continue;
vis[u] = true;
for(int i=h[u]; ~i; i=E[i].nxt)
{
int v = E[i].v;
int w = E[i].w;
if(vis[v]) continue;
if(dis[u]+w<dis[v])
{
dis[v] = dis[u]+w;
Q.push(QNode(v,dis[v]));
}
}
}
} void BFS(int n,LL val)
{
set<int>ta,tb;
queue<int>Q;
Q.push(start);
dis[start] = ,dis[n] = INF;
for(int i=; i<=n; i++){
if(i==start) continue;
ta.insert(i);
}
while(!Q.empty())
{
int u = Q.front();
Q.pop();
for(int i=h[u]; ~i; i=E[i].nxt)
{
int v = E[i].v;
if(!ta.count(v)) continue;
ta.erase(v);
tb.insert(v);
}
for(set<int>::iterator it=ta.begin(); it!=ta.end(); it++)
{
Q.push(*it);
dis[*it] = dis[u] + val;
}
ta.swap(tb);
tb.clear();
}
} int main()
{
int N,M,T;
int u,v;
while(scanf("%d",&T)!=EOF)
{
while(T--)
{
scanf("%d%d",&N,&M);
init();
bool flag = false;
for(int i=; i<M; i++)
{
scanf("%d%d",&u,&v);
add(u,v,A);
add(v,u,A); }
scanf("%d",&start);
dijkstra(N);
BFS(N,B);
bool first=;
for(int i=;i<=N;i++)
{
if(i==start) continue;
if(first) printf(" ");
printf("%d",dis[i]);
first=;
}
printf("\n");
}
}
return ;
} /* 1
2 0
1 */

hdu 5876 Sparse Graph icpc大连站网络赛 1009 补图最短路的更多相关文章

  1. HDU 5875 Function -2016 ICPC 大连赛区网络赛

    题目链接 网络赛的水实在太深,这场居然没出线zzz,差了一点点,看到这道题的的时候就剩半个小时了.上面是官方的题意题解,打完了才知道暴力就可以过,暴力我们当时是想出来了的,如果稍稍再优化一下估计就过了 ...

  2. hdu 5874 Friends and Enemies icpc大连站网络赛 1007 数学

    #include<stdio.h> #include<iostream> #include<algorithm> #include<math.h> #i ...

  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

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

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

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

  6. HDU 5876 Sparse Graph BFS 最短路

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

  7. HDU 5876 Sparse Graph(补图中求最短路)

    http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意: 在补图中求s到其余各个点的最短路. 思路:因为这道题目每条边的距离都是1,所以可以直接用bfs来做 ...

  8. HDU 5876 Sparse Graph(补图上BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意: 有一个 n 个点无向图,再给你 m 对顶点, 代表着这 m 对顶点之间没有边, 除此之外 ...

  9. HDU 5876 Sparse Graph BFS+set删点

    Problem Description In graph theory, the complement of a graph G is a graph H on the same vertices s ...

随机推荐

  1. GIT 代码管理工具 SourceTree

    什么是git? git是一款开源的分布式版本控制工具 在世界上所有的分布式版本控制工具中,git是最快.最简单.最流行的 git的起源 作者是Linux之父:Linus Benedict Torval ...

  2. python3 列表 函数

    python3中list的所有函数 list是有序的,元素个数无限的,元素类型多样的,可变的 增加 # 'append', 增加对象# 'insert', 指定位置增加# 'extend', 增加可迭 ...

  3. ajax客户端请求与服务端响应浅谈

    AJAX,即Asynchronous Javascript And XML,AJAX本质是在HTTP协议的基础上以异步的方式与服务器进行通信. 所谓的异步,是指某段程序执行不会阻塞其他程序执行,其表现 ...

  4. LanSoEditor_advance1.8.0 视频编辑的高级版本

    ------------------------------------------2017年1月11日11:18:33------------------------------------- 我们 ...

  5. canvas画扇形图(本文来自于http://jo2.org/html5-canvas-sector/)

    1.定义画扇形的构造函数: //扇形CanvasRenderingContext2D.prototype.sector = function (x, y, radius, sDeg, eDeg) {/ ...

  6. 面试经典——从输入 URL 到页面加载完的过程中都发生了什么事情?

    想要更加了解http协议,猛戳这里 1)把URL分割成几个部分:协议.网络地址.资源路径.其中网络地址指示该连接网络上哪一台计算机,可以是域名或者IP地址,可以包括端口号:协议是从该计算机获取资源的方 ...

  7. VIP站长大会(北京站)常见问题解答

    功能支持问题 1. react能否和MIP结合使用,如果暂时不能以后是否有考虑?是否会和其他 js 框架(比如angular )结合? 目前暂无计划支持. 2. MIP页是否支持自定义cookie?实 ...

  8. jffs2reset 实现分析

    在脚本 reset2default中看到如下内容: #!/usr/bin/lua local hwm = require "hwm" hwm.myexec("jffs2r ...

  9. Leetcode 解题报告

    347. Top K Frequent Elements Given a non-empty array of integers, return the k most frequent element ...

  10. WPF 限制Textbox输入的内容

    限制文本框TextBox的输入内容,在很多场景都有应用.举个例子,现在文本框中,只能输入0.1.2.3.4.5.6.7.8.9.“|”这11个字符. 限制输入0-9很容易实现,关键是这个“|”符号.它 ...