AOJ GRL_1_A: Single Source Shortest Path (Dijktra算法求单源最短路径,邻接表)
题目链接:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_A
Single Source Shortest Path
Input
An edge-weighted graph G (V, E) 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算法求单源最短路径,邻接表)的更多相关文章
- 51nod 1445 变色DNA ( Bellman-Ford算法求单源最短路径)
1445 变色DNA 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 有一只特别的狼,它在每个夜晚会进行变色,研究发现它可以变成N种颜色之一,将这些颜色标号为0,1 ...
- 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 ...
- Dijkstra算法求单源最短路径
Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店 ...
- SPFA算法与dijkstra算法求单源最短路径的比较
SPFA是运用队列,把所有的点遍历到没有能更新的,点可以重复入队 如题http://www.cnblogs.com/Annetree/p/5682306.html dijkstra是每次找出离源点最近 ...
- 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 ...
- 单源最短距离 Single Source Shortest Path
单源最短距离_示例程序_图模型_用户指南_MaxCompute-阿里云 https://help.aliyun.com/document_detail/27907.html 单源最短距离 更新时间:2 ...
- [数据结构与算法-15]单源最短路径(Dijkstra+SPFA)
单源最短路径 问题描述 分别求出从起点到其他所有点的最短路径,这次主要介绍两种算法,Dijkstra和SPFA.若无负权优先Dijkstra算法,存在负权选择SPFA算法. Dijkstra算法 非负 ...
- 【转】Dijkstra算法(单源最短路径)
原文:http://www.cnblogs.com/dolphin0520/archive/2011/08/26/2155202.html 单源最短路径问题,即在图中求出给定顶点到其它任一顶点的最短路 ...
- 【算法】单源最短路径和任意两点最短路径总结(补增:SPFA)
[Bellman-Ford算法] [算法]Bellman-Ford算法(单源最短路径问题)(判断负圈) 结构: #define MAX_V 10000 #define MAX_E 50000 int ...
随机推荐
- POJ 1471 模拟?
题意:求最大无坏点三角形 思路: 模拟? (为什么我模拟过了...) 有人用 DP,有人用 搜索... // by SiriusRen #include <cstdio> #include ...
- Android开发:ImageView阴影和图层效果
import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import ...
- Rabbit MQ 学习 (二)
接连上一篇 :安装Erlang环境 之后,这篇安装 Rabbit Server 官网下载安装包:http://www.rabbitmq.com/install-windows.html 打开安装一路下 ...
- JavaScript DOM编程艺术(第2版)学习笔记2(4~6章应用实例)
本书的第4章使用第3章学到的操作DOM的方法和属性写了一个展示图片的网页,并在第5,6章对代码进行了优化. 第一版,搭建网页的静态结构,包括一级标题<h1>,无序列表清单<ul> ...
- 使用CocoaPods更新第三方库出错的解决办法
使用CocoaPods更新第三方库出错的解决办法 执行完pod install或pod update之后,控制台抛出以下警告信息: [!] The xx [Debug] target override ...
- javscript中变量的作用域和提升
示例: var a = 1; function foo() { if (!a) { var a = 10; } alert(a); }; foo(); 上面这段代码在运行时会产生 ...
- 洛谷T47092 作业_简单状压动归
只要注意一下细节就毫无难点了,简简单单状态压缩即可. Code: #include<cstdio> #include<algorithm> using namespace st ...
- css鼠标滑过出现文字效果
模仿淘宝上鼠标移动到商品图片时,出现的文字效果. 1.效果图 鼠标移动到粉红色的区域,则出现黄色部分. 2.代码 <!DOCTYPE ...
- HDU 4725 The Shortest Path in Nya Graph [构造 + 最短路]
HDU - 4725 The Shortest Path in Nya Graph http://acm.hdu.edu.cn/showproblem.php?pid=4725 This is a v ...
- 搭建hadoop java开发环境
package hadoopDemo; import java.io.IOException; import java.net.URI; import java.net.URISyntaxExcept ...