题目链接:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_A

Single Source Shortest Path

Input

An edge-weighted graph G (VE) and the source r.

|V| |E| r
s0 t0 d0
s1 t1 d1
:
s|E|−1 t|E|−1 d|E|−1

|V| is the number of vertices and |E| is the number of edges in G. The graph vertices are named with the numbers 0, 1,..., |V|−1 respectively. r is the source of the graph.

si and ti represent source and target vertices of i-th edge (directed) and di represents the cost of the i-th edge.

Output

c0
c1
:
c|V|−1

The output consists of |V| lines. Print the cost of the shortest path from the source r to each vertex 0, 1, ... |V|−1 in order. If there is no path from the source to a vertex, print INF.

Constraints

  • 1 ≤ |V| ≤ 100000
  • 0 ≤ di ≤ 10000
  • 0 ≤ |E| ≤ 500000
  • There are no parallel edges
  • There are no self-loops

Sample Input 1

4 5 0
0 1 1
0 2 4
1 2 2
2 3 1
1 3 5

Sample Output 1

0
1
3
4

Sample Input 2

4 6 1
0 1 1
0 2 4
2 0 1
1 2 2
3 1 1
3 2 5

Sample Output 2

3
0
2
INF 这题数据范围比较大,最多有100000个顶点,用邻接矩阵表示的话,空间肯定会超限。
我用Dijiksra的邻接表来解了。 套一个模板就出来了:
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
typedef long long ll;
#define INF 2147483647 struct edge{
int to,cost;
};
int V,E; vector <edge> G[]; multimap <int,int> l; ll d[]; void dijkstra(int s){
fill(d,d+V,INF);
d[s] = ; l.insert(make_pair(,s)); while(l.size() > ){
int p = l.begin()->first;
int v = l.begin()->second;
l.erase(l.begin()); if(d[v] < p) continue; for(int i = ;i < G[v].size(); i++){
edge e = G[v][i];
if(d[e.to] > d[v] + e.cost){
d[e.to] = d[v] + e.cost;
l.insert(make_pair(d[e.to], e.to));
}
}
}
} int main(){ int r;
cin >> V >> E >> r;
for(int i = ;i < E; i++){
edge e;
int from;
cin >> from >> e.to >> e.cost;
G[from].push_back(e);
} dijkstra(r); for(int i = ;i < V; i++){
if(d[i] != INF) cout << d[i] << endl;
else cout << "INF" <<endl;
}
return ;
}
 

AOJ GRL_1_A: Single Source Shortest Path (Dijktra算法求单源最短路径,邻接表)的更多相关文章

  1. 51nod 1445 变色DNA ( Bellman-Ford算法求单源最短路径)

    1445 变色DNA 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 有一只特别的狼,它在每个夜晚会进行变色,研究发现它可以变成N种颜色之一,将这些颜色标号为0,1 ...

  2. AOJ GRL_1_C: All Pairs Shortest Path (Floyd-Warshall算法求任意两点间的最短路径)(Bellman-Ford算法判断负圈)

    题目链接:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_C All Pairs Shortest Path Input ...

  3. Dijkstra算法求单源最短路径

    Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店 ...

  4. SPFA算法与dijkstra算法求单源最短路径的比较

    SPFA是运用队列,把所有的点遍历到没有能更新的,点可以重复入队 如题http://www.cnblogs.com/Annetree/p/5682306.html dijkstra是每次找出离源点最近 ...

  5. AOJ GRL_1_B: Shortest Path - Single Source Shortest Path (Negative Edges) (Bellman-Frod算法求负圈和单源最短路径)

    题目链接: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_B   Single Source Shortest Path ...

  6. 单源最短距离 Single Source Shortest Path

    单源最短距离_示例程序_图模型_用户指南_MaxCompute-阿里云 https://help.aliyun.com/document_detail/27907.html 单源最短距离 更新时间:2 ...

  7. [数据结构与算法-15]单源最短路径(Dijkstra+SPFA)

    单源最短路径 问题描述 分别求出从起点到其他所有点的最短路径,这次主要介绍两种算法,Dijkstra和SPFA.若无负权优先Dijkstra算法,存在负权选择SPFA算法. Dijkstra算法 非负 ...

  8. 【转】Dijkstra算法(单源最短路径)

    原文:http://www.cnblogs.com/dolphin0520/archive/2011/08/26/2155202.html 单源最短路径问题,即在图中求出给定顶点到其它任一顶点的最短路 ...

  9. 【算法】单源最短路径和任意两点最短路径总结(补增:SPFA)

    [Bellman-Ford算法] [算法]Bellman-Ford算法(单源最短路径问题)(判断负圈) 结构: #define MAX_V 10000 #define MAX_E 50000 int ...

随机推荐

  1. adplus 抓取dump

    工具所在路径   C:\Program Files\Windows Kits\10\Debuggers\x64 cmd窗口切换目录倒adplus所在路径下,输入抓取命令.adplus -hang -p ...

  2. 彻底解决降级安装失败无法彻底卸载应用bug

    彻底解决魅族手机无法彻底卸载应用bug使用Flyme系统的同学可能会遇到一个问题:卸载了某些软件(例如通过开发者模式调试安装的应用)后,实际这个应用还残留在系统,当你用低版本或者其他签名的apk覆盖安 ...

  3. 深度学习之入门Pytorch(1)------基础

    目录: Pytorch数据类型:Tensor与Storage 创建张量 tensor与numpy数组之间的转换 索引.连接.切片等 Tensor操作[add,数学运算,转置等] GPU加速 自动求导: ...

  4. HDU 5288 OO’s Sequence [数学]

     HDU 5288 OO’s Sequence http://acm.hdu.edu.cn/showproblem.php?pid=5288 OO has got a array A of size ...

  5. 使用multiprocessing模块操作进程

    1.Process模块介绍 process模块是一个创建进程的模块,借助这个模块,就可以完成进程的创建. Process([group [, target [, name [, args [, kwa ...

  6. 基于LXC的虚拟网络自动部署

    一.问题: 在搭建以LXC为基础的虚拟网络时,网络参数繁多,配置过程繁琐.面临一个新的网络拓扑结构时,通常要花费大量时间来构建网络.因此,如果能通过配置文件,自动生成相对应的网络拓扑,并生成操作指令. ...

  7. centos7下部署FastDFS分布式文件系统

    前言 项目中用到文件服务器,有朋友推荐用FastDFS,所以就了解学习了一番,感觉确实颇为强大,在此再次感谢淘宝资深架构师余庆大神开源了如此优秀的轻量级分布式文件系统,本篇文章就记录一下FastDFS ...

  8. Java基础学习总结(55)——java8新特性:stream

    java作为开发语言中的元老已经度过了很多年,最新的java8为我们带来了一些新特性,这些特性可以在以后的工作中为我们的开发提供更多的便捷,现在就让我们看看最新的函数式编程风格怎么在实际的开发中使用. ...

  9. mysql数据实时同步到Elasticsearch

    业务需要把mysql的数据实时同步到ES,实现低延迟的检索到ES中的数据或者进行其它数据分析处理.本文给出以同步mysql binlog的方式实时同步数据到ES的思路, 实践并验证该方式的可行性,以供 ...

  10. 洛谷 P3133 [USACO16JAN]无线电联系Radio Contact

    P3133 [USACO16JAN]无线电联系Radio Contact 题目描述 Farmer John has lost his favorite cow bell, and Bessie the ...