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. DevExpress的SpinEdit控件无法输入数字的问题

    今天在发布程序后突然发现了这个问题,刚开始很莫名其妙的,因为在调试时从来没碰到过.然后经过测试发现,这个问题的原因和输入法有很大关系: 当你的输入法是中文状态时,是无法向框中输入数字的,此时只能点击上 ...

  2. 关于wkwebview

    一.引入库 #import <WebKit/WebKit.h> 二.初始化有两种方式 // 默认初始化 - (instancetype)initWithFrame:(CGRect)fram ...

  3. Beyond Compare V3.2.3 Beta 中文版

    软件名称: Beyond Compare V3.2.3 Beta 中文版 软件语言: 简体中文 授权方式: 免费软件 运行环境: Win7 / Vista / Win2003 / WinXP 软件大小 ...

  4. request获取ip

    public static String getIp(HttpServletRequest request) { String ip = request.getHeader("x-forwa ...

  5. 解锁Oracle数据库用户

    Oracle数据库,如何解除数据库用户账户的锁定呢???如何修改口令呢??? 请问各位高手:Oracle数据库,如何解除数据库用户账户的锁定呢???如何修改口令呢???[万分感激各位高手的帮忙] 00 ...

  6. Python学习笔记——基础篇【第五周】——常用模块学习

    模块介绍 本节大纲: 模块介绍 time &datetime模块   (时间模块) random   (随机数模块) os   (系统交互模块) sys shutil   (文件拷贝模块) j ...

  7. php遍历目录下文件,并读取内容

    <?php echo "<h2>遍历目录下文件,并读取内容</h2><br>\n"; function listDir($dir) { i ...

  8. 启动Mysql报错:Another MySQL daemon already running with the same unix socket.

    启动Mysql报错: Another MySQL daemon already running with the same unix socket. 删除如下文件即可解决 /var/lib/mysql ...

  9. php获取url字符串截取路径的文件名和扩展名

    <?php //获取连接里边的id $url = 'http://www.rong123.com/cjbkscbsd/x_dfsdfs/24454_1_1.html'; function get ...

  10. DataGrid 导出数据到 Excel

    Private Sub GridToExl_Click() On Error Resume Next If DataGrid1.Columns.Count = 0 Then MsgBox " ...