Dijkstra算法与Bellman - Ford算法示例(源自网上大牛的博客)【图论】
题意:题目大意:有N个点,给出从a点到b点的距离,当然a和b是互相可以抵达的,问从1到n的最短距离
poj2387
Description
Farmer John's field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1..N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.
Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.
Input
* Lines 2..T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1..100.
Output
Sample Input
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
Sample Output
90
解法一:(dijkstra算法)(PS:2016.3.22修改自己写的版本)
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #define MAX 9999999
- using namespace std ;
- int u , v ,n, dis[1111],vis[1111],ma[1111][1111];
- void dijk()
- {
- int k , mini;
- for(int i = 1 ; i <=v;i++)
- {
- dis[i]=ma[1][i];
- }
- for(int i = 1 ;i<=v;i++)
- {
- mini=MAX;
- for(int j = 1 ; j<=v;j++)
- {
- if(!vis[j]&&dis[j]<mini)
- {
- mini=dis[j];
- k=j;
- }
- }
- vis[k]=1;
- for(int j=1 ;j<=v;j++)
- {
- if(dis[j]>dis[k]+ma[k][j])
- {
- dis[j]=dis[k]+ma[k][j];
- }
- }
- }
- }
- int main()
- {
- while(cin>>u>>v)
- {
- n=0;
- for(int i = 0 ; i <=v;i++)
- {
- for(int j = 0 ; j <=v;j++)
- {
- ma[i][j]=MAX;
- }
- ma[i][i]=0;
- vis[i]=0;
- dis[i]=MAX;
- }
- for(int i = 1 ;i<=u;i++)
- {
- int a , b , len;
- cin>>a>>b>>len;
- n=max(max(n,a),b);
- if(ma[a][b]>len)
- {
- ma[a][b]=ma[b][a]=len;
- }
- }
- dijk();
- printf("%d\n",dis[v]);
- }
- return 0 ;
- }
解法二(Bellman-Ford)
- //*bellman算法:
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #define N 2010
- #define MAX 99999999
- using namespace std ;
- struct node{
- int a , b , w ;
- }edge[N];
- int n , m ;
- void bell()
- {
- int i , j ;
- int d[N];
- for(int i =1 ; i<=n;i++)//*距离初始化为无穷;
- {
- d[i]=MAX;
- }
- d[1]=0;//*初始地点为0;
- for(i=1;i<=n;i++)
- {
- for(j=1;j<=m;j++)//*按点-边搜,顺便解决了重边问题;
- {
- if(d[edge[j].a]>d[edge[j].b]+edge[j].w) d[edge[j].a]= d[edge[j].b]+edge[j].w;
- if(d[edge[j].b]>d[edge[j].a]+edge[j].w) d[edge[j].b]= d[edge[j].a]+edge[j].w;
- }
- }
- printf("%d\n",d[n]);
- }
- int main()
- {
- int i , a , b ,c;
- while(cin>>m>>n)
- {
- for(int i =1 ; i<=m;i++)//*结构体存边和权
- {
- cin>>a>>b>>c;
- edge[i].a=a;
- edge[i].b=b;
- edge[i].w=c;
- }
- bell();
- }
- return 0 ;
- }
方法三(Floyd-Warshall):虽然过不去数据,因为太大;但是值得一试;
- #include <iostream>
- #include <stdio.h>
- #include <math.h>
- #include <algorithm>
- #include <cstring>
- #define N 2000
- #define MAX 99999999
- using namespace std ;
- int u , v ;
- int dis[N][N];
- void warsh() {
- int i , j , k ;
- for(k=1; k<=v; k++) {
- for(i=1; i<=v; i++) {
- for(j=1; j<=v; j++) {
- dis[i][j]=min(dis[i][j],dis[k][j]+dis[i][k]);
- }
- }
- }
- }
- int main() {
- cin>>u>>v ;
- int a, b , c ;
- for(int i = 1 ; i <= v ; i++) {
- for(int j = 1 ; j <=v; j++) {
- dis[i][j]=MAX;
- }
- }
- for(int i = 0 ; i < v ; i++) {
- dis[i][i]=0;
- }
- for(int i = 1 ; i <=u ; i++) {
- cin>>a>>b>>c;
- dis[a][b]=dis[b][a]=c;
- }
- warsh();
- cout<<dis[1][v]<<endl;
- return 0 ;
- }
Dijkstra算法与Bellman - Ford算法示例(源自网上大牛的博客)【图论】的更多相关文章
- Bellman - Ford 算法解决最短路径问题
Bellman - Ford 算法: 一:基本算法 对于单源最短路径问题,上一篇文章中介绍了 Dijkstra 算法,但是由于 Dijkstra 算法局限于解决非负权的最短路径问题,对于带负权的图就力 ...
- Bellman—Ford算法思想
---恢复内容开始--- Bellman—Ford算法能在更普遍的情况下(存在负权边)解决单源点最短路径问题.对于给定的带权(有向或无向)图G=(V,E),其源点为s,加权函数w是边集E的映射.对图G ...
- random_shuffle (stl算法)打乱顺序 - 飞不会的日志 - 网易博客
random_shuffle (stl算法)打乱顺序 - 飞不会的日志 - 网易博客 random_shuffle (stl算法)打乱顺序 2012-03-31 10:39:11| 分类: 算法 | ...
- 图论——最短路:Floyd,Dijkstra,Bellman-Ford,SPFA算法及最小环问题
一.Floyd算法 用于计算任意两个节点之间的最短路径. 参考了five20的博客 Floyd算法的基本思想如下:从任意节点A到任意节点B的最短路径不外乎2种可能,1是直接从A到B,2是从A经过若干个 ...
- poj1860 bellman—ford队列优化 Currency Exchange
Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 22123 Accepted: 799 ...
- uva 558 - Wormholes(Bellman Ford判断负环)
题目链接:558 - Wormholes 题目大意:给出n和m,表示有n个点,然后给出m条边,然后判断给出的有向图中是否存在负环. 解题思路:利用Bellman Ford算法,若进行第n次松弛时,还能 ...
- 算法笔记_070:BellmanFord算法简单介绍(Java)
目录 1 问题描述 2 解决方案 2.1 具体编码 1 问题描述 何为BellmanFord算法? BellmanFord算法功能:给定一个加权连通图,选取一个顶点,称为起点,求取起点到其它所有顶 ...
- 排序算法系列:插入排序算法JAVA版(靠谱、清晰、真实、可用、不罗嗦版)
在网上搜索算法的博客,发现一个比较悲剧的现象非常普遍: 原理讲不清,混乱 啰嗦 图和文对不上 不可用,甚至代码还出错 我总结一个清晰不罗嗦版: 原理: 和选择排序类似的是也分成“已排序”部分,和“未排 ...
- 排序算法系列:快速排序算法JAVA版(靠谱、清晰、真实、可用、不罗嗦版)
在网上搜索算法的博客,发现一个比较悲剧的现象非常普遍: 原理讲不清,混乱 啰嗦 图和文对不上 不可用,甚至代码还出错 为了不误人子弟耽误时间,推荐看一些靠谱的资源,如[啊哈!算法]系列: https: ...
随机推荐
- 如何解决Linux 系统下 ifconfig 命令无网络接口 ens33
今天我在做Redis的哨兵集群模式的时候,以前都是好的,也不知道从什么时候开始就无法连接Redis服务器了,就是运行如下命令,没有效果:redis-server redis.conf,然后在通过命令查 ...
- POJ 2888 Magic Bracelet [Polya 矩阵乘法]
传送门 题意:竟然扯到哈利波特了.... 和上一题差不多,但颜色数很少,给出不能相邻的颜色对 可以相邻的连边建图矩阵乘法求回路个数就得到$f(i)$了.... 感觉这样的环上有限制问题挺套路的...旋 ...
- BZOJ 1951: [Sdoi2010]古代猪文 [Lucas定理 中国剩余定理]
1951: [Sdoi2010]古代猪文 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 2194 Solved: 919[Submit][Status] ...
- Spring MVC的配置和使用
Spring MVC的配置和使用 笔记仓库:https://github.com/nnngu/LearningNotes Spring MVC需要的jar包 文章中 Spring MVC 使用的版本是 ...
- 发放春节福利,ASP.NET Core断点续传
ASP.NET Core断点续传 在ASP.NET WebAPi写过完整的断点续传文章,目前我对ASP.NET Core仅止于整体上会用,对于原理还未去深入学习,由于有园友想看断点续传在ASP.NET ...
- ES6中let和闭包
在开始本文之前我们先来看一段代码 for(var i=0;i<10;i++){ arr[i]=function(){ return i; } } console.log(arr[3]());// ...
- 优雅使用 illuminate/database 包中的 Collection
优雅使用 illuminate/database 包中的 Collection 或许你很抵抗使用 Laravel , 但是你没有理由不喜欢使用 illuminate/database.这是一个 ORM ...
- cloud9 ide
https://github.com/tekacs/cloud9 http://www.pjhome.net/article/Javascript/nodeJS_IDE_cloud9.html htt ...
- phpstorm使用之——常用快捷键
phpstorm使用之--常用快捷键 使用IDE的根本所在乃是为了提高工作效率. windows下phpstorm的快捷键 ctrl+shift+n查找文件 ctrl+shift+f 在一个目录里查找 ...
- Nginx 静态资源缓存设置
在开发调试web的时候,经常会碰到因浏览器缓存(cache)而经常要去清空缓存或者强制刷新来测试的烦恼,提供下apache不缓存配置和nginx不缓存配置的设置.在常用的缓存设置里面有两种方式,都是使 ...