题目链接: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. Ubuntu16.04系统安装搜狗输入法

    前言:正常双击.deb软件包安装搜狗输入法会有bug,需要按照下面操作进行消除错误. 一.下载搜狗输入法Linux版软件包 下载地址为:http://pinyin.sogou.com/linux/ , ...

  2. SQL Server 汉字转拼音字母

    /* 汉字转拼音首字母 */ create function fun_getPY(@str nvarchar(4000)) returns nvarchar(4000) as begin declar ...

  3. Oracle学习系类篇(二)

    1.Oracle对表的增删改 1.1添加列 1.2修改列 1.3 删除列 1.4 修改表名称 1.5 修改列名称 1.6 删除主键约束 1.7 添加主键约束 1.8 添加外键约束

  4. Book 树状数组 小结

    差不多花了10天学树状数组,是照着这篇博客做的题目,还差几道---------- http://blog.csdn.net/chenguolinblog/article/details/9916229 ...

  5. hdu2614 Beat

    题意: 有n个问题. 给出你解决完第i个问题之后解决j问题所花的时间,花的时间越多表示难度越大,每次只能解决难度大于或等于上个题难度的问题.问你最多能解决多少问题. 他妈的,第一次做想半天想不出来如何 ...

  6. Unity5.X 创建基本的3D游戏场景

    点New(新建懒得写了,反正不是智障应该都会) 创建好的项目会自带一个场景,场景会自带Main Camera (主摄像机),Directional Light (方向光)   系统自带几个可以创建的3 ...

  7. react-redux源码解析(资料)

    资料:https://www.cnblogs.com/hhhyaaon/p/5863408.html 感觉很棒,记录一下

  8. MATLAB解析PFM格式图像

    http://www.p-chao.com/ja/2016-09-27/matlab%E8%A7%A3%E6%9E%90pfm%E6%A0%BC%E5%BC%8F%E5%9B%BE%E5%83%8F/ ...

  9. poj 2288 Islands and Bridges (状压dp+Tsp问题)

    这道题千辛万苦啊! 这道题要涉及到当前点和前面两个点,那就设dp[state][i][j]为当前状态为state,当前点为i,前一个点为j 这个状态表示和之前做炮兵那题很像,就是涉及到三个点时,就多设 ...

  10. Java基础学习总结(57)——Jrebel插件热部署

    JavaEE开发环境下,Tomcat对热布署的支持还是比较弱,致使开发过程中浪费大量时间在重启服务上.胖先生讨厌来来回回的折腾,所以想看看有没有实时的编译,发现Jrebel的插件付费软件,它对热布署的 ...