hdu 5876 Sparse Graph icpc大连站网络赛 1009 补图最短路
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 补图最短路的更多相关文章
- HDU 5875 Function -2016 ICPC 大连赛区网络赛
题目链接 网络赛的水实在太深,这场居然没出线zzz,差了一点点,看到这道题的的时候就剩半个小时了.上面是官方的题意题解,打完了才知道暴力就可以过,暴力我们当时是想出来了的,如果稍稍再优化一下估计就过了 ...
- hdu 5874 Friends and Enemies icpc大连站网络赛 1007 数学
#include<stdio.h> #include<iostream> #include<algorithm> #include<math.h> #i ...
- 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
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...
- hdu 5876 Sparse Graph 无权图bfs求最短路
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) P ...
- 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(补图中求最短路)
http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意: 在补图中求s到其余各个点的最短路. 思路:因为这道题目每条边的距离都是1,所以可以直接用bfs来做 ...
- HDU 5876 Sparse Graph(补图上BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意: 有一个 n 个点无向图,再给你 m 对顶点, 代表着这 m 对顶点之间没有边, 除此之外 ...
- 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 ...
随机推荐
- Apache 2.4.16、PHP5.6.11安装教程
以前我写过Apache2.4和php5.5的安装教程,但是后来我自己跟着自己写的东西做时发现有很多问题,这里把这些问题重新修正,再写一个教程,供大家参考. 注意:WinXP系统请选择旧版本Apache ...
- Java NIO Channel之FileChannel [ 转载 ]
Java NIO Channel之FileChannel [ 转载 ] @author zachary.guo 对于文件 I/O,最强大之处在于异步 I/O(asynchronous I/O),它允许 ...
- [Q]系统环境改变导致“未注册”的解决方法
据用户反映设置账户开机密码后显示未注册, 具体表现: 1. 重装试用版,重新获取注册申请码,发现注册申请码跟原来没有发生变化. 2. 重新使用原来的授权文件注册,但打开后显示未注册. 3. 发现“** ...
- Ubuntu火狐、Chromium等浏览器安装flash插件
1.打开系统设置->软件和更新->其他软件,勾选Canonical合作伙伴,输入密码,重新载入更新 2.打开终端,按装插件 sudo apt install adobe-flashplug ...
- haproxy(1)
参考文档: http://cbonte.github.io/haproxy-dconv/1.5/configuration.html 一.Haproxy 软件负载均衡一般通过两种方式来实现:基于操作系 ...
- C# 语言规范_版本5.0 (第11章 结构)
1. 结构 结构与类的相似之处在于,它们都表示可以包含数据成员和函数成员的数据结构.但是,与类不同,结构是一种值类型,并且不需要堆分配.结构类型的变量直接包含了该结构的数据,而类类型的变量所包含的只是 ...
- jsp日期插件My97DatePicker 强大的日期控件 使用方便简单
本文属转载(希望对编程爱好者有所帮助)详情请访问官方网站 http://www.my97.net/dp/index.asp 一. 简介 1. 简介 目前的版本是:4.7 2. 注意事项 My97Dat ...
- linux 启动 关闭 防火墙
开启防火墙: systemctl start firewalld 关闭防火墙: systemctl stop firewalld
- JavaScript Date对象更进一步
总结分享这个近期开发解决的一个Bug. Javascript的Date对象具有容错性,会自动根据当年的日期根据设置的属性值转换,也就是说Date对象的setDate会影响setMonth,month会 ...
- 树:BST、AVL、红黑树、B树、B+树
我们这个专题介绍的动态查找树主要有: 二叉查找树(BST),平衡二叉查找树(AVL),红黑树(RBT),B~/B+树(B-tree).这四种树都具备下面几个优势: (1) 都是动态结构.在删除,插入操 ...