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 2147483647struct edge{int to,cost; }; int V,E; vector <edge> G[100010];multimap <int,int> l;ll d[500010];void dijkstra(int s){fill(d,d+V,INF);d[s] = 0;l.insert(make_pair(0,s));while(l.size() > 0){int p = l.begin()->first;int v = l.begin()->second;l.erase(l.begin());if(d[v] < p) continue;for(int i = 0;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 = 0;i < E; i++){edge e;int from;cin >> from >> e.to >> e.cost;G[from].push_back(e);}dijkstra(r);for(int i = 0;i < V; i++){if(d[i] != INF) cout << d[i] << endl;else cout << "INF" <<endl;}return 0; }
转载于:https://www.cnblogs.com/zhangjiuding/p/7726089.html
AOJ GRL_1_A: Single Source Shortest Path (Dijktra算法求单源最短路径,邻接表)相关推荐
- 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 In ...
- Dijkstra(迪杰斯特拉)算法求单源最短路径问题
Dijkstra(迪杰斯特拉)算法求单源最短路径问题 重要的事情说三遍:代码不是我写的!代码不是我写的!代码不是我写的! 第一个算法是严蔚敏数据结构(C语言版)上写的,第二个算法是王道数据结构上写的, ...
- 51nod 1445 变色DNA ( Bellman-Ford算法求单源最短路径)
1445 变色DNA 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 有一只特别的狼,它在每个夜晚会进行变色,研究发现它可以变成N种颜色之一,将这些颜色标号为0,1 ...
- 【2023王道数据结构】【图】通过C++实现图的BFS(广度优先遍历)算法求单源最短路径问题C、C++完整实现(可直接运行)
~~~笔锋至此又怎能平淡而终,故事开始便不承认普通✌✌✌ ✌ 题目及题解持续更新中 [2023王道数据结构目录]课后算法设计题C.C++代码实现完整版大全 题目: 通过C++实现图的BFS(广度优先遍 ...
- Dijkstra算法求单源最短路径
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 ...
- C语言基本数据结构之三(图的广度及深度遍历,求单源最短路径的Dijkstra算法)
上一篇主要讲了二叉树的先序,中序,后序遍历算法以及深度和节点的算法,这篇就讲一讲图的基本算法. 一.图的基本概念 1.1有向图G1: 有向图G是由两个集合V(G)和E(G)组成的,其中:V(G)是顶点 ...
- 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 ( ...
- 图的基本算法(单源最短路径)
在许多路由问题中,寻找图中一个顶点到另一个顶点的最短路径或最小带权路径是非常重要的提炼过程.正式表述为,给定一个带权有向图G = (V, E) , 顶点s到v中顶点t的最短路径为在边集E中连接s到t代 ...
最新文章
- win7/IE8无法加载QCbin的插件
- TensorRT学习笔记2 - 基础知识
- 直播 | WWW 2021论文解读:生成式板块推荐的变分控制和评估
- webpack 谷歌地图_如何在Webpack中设置可靠且可维护的Google Analytics(分析)
- mysql 主键倒序查询速度慢_一亿条数据order by主键降序速度很慢
- Python中文问题
- 一步一步SharePoint 2007之十五:实现Form认证(5)——更改认证的Provider
- C语言 signal
- 一个经典的多线程同步问题
- 20款开源搜索引擎介绍与比较
- pp助手苹果版_曾联合盘古团队开发越狱工具的PP助手将在月底关停iOS相关业务...
- 使用Feurio刻录音乐CD 无损音乐FLAC
- Life feelings--13--青春不毕业,那些心里念念叨叨难以忘怀的记忆
- hadoop3.3.1搭建过程遇到的坑
- py233基于 python的诚交大学生二手交易平台Django#毕业设计
- 微信开发者工具 Source Map 的使用
- AURIX TC397 CAN MCMCAN
- 5.Flink原理初探\角色分工\执行流程图生成\DataFlow,Operator,Partition,Parallelism,SubTask\OperatorChain和Task\任务槽\槽共享
- Javaweb酒店预约管理系统(框架SpringBoot+Vue)
- ASUS AC1900p 梅林固件编译 asuswrt-merlin
热门文章
- 【Golang 快速入门】项目实战:即时通信系统
- 手把手教你如何导入源码,zookeeper为例
- hibernate入门学习(更新中)
- CTA策略如何过滤部分震荡行情?
- stream().map().collect()用法
- eclipse java读取文件_在eclipse完成对Java_web项目里面资源文件的读取
- linux编译lnx文件命令_Linux命令总结
- java做网页客户端_如何成为 Java web开发者
- mysql workbench中文设置 mac系统,win系统,linux系统
- 人脸识别的Python库