题意:题目大意:有N个点,给出从a点到b点的距离,当然a和b是互相可以抵达的,问从1到n的最短距离

poj2387

Description

Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible.

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

* Line 1: Two integers: T and N

* 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

* Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.

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修改自己写的版本)

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #define MAX 9999999
  6. using namespace std ;
  7. int u , v ,n, dis[1111],vis[1111],ma[1111][1111];
  8. void dijk()
  9. {
  10. int k , mini;
  11. for(int i = 1 ; i <=v;i++)
  12. {
  13. dis[i]=ma[1][i];
  14. }
  15. for(int i = 1  ;i<=v;i++)
  16. {
  17. mini=MAX;
  18. for(int j = 1 ; j<=v;j++)
  19. {
  20. if(!vis[j]&&dis[j]<mini)
  21. {
  22. mini=dis[j];
  23. k=j;
  24. }
  25. }
  26. vis[k]=1;
  27. for(int j=1 ;j<=v;j++)
  28. {
  29. if(dis[j]>dis[k]+ma[k][j])
  30. {
  31. dis[j]=dis[k]+ma[k][j];
  32. }
  33. }
  34. }
  35. }
  36. int main()
  37. {
  38. while(cin>>u>>v)
  39. {
  40. n=0;
  41. for(int i = 0 ; i <=v;i++)
  42. {
  43. for(int j = 0 ; j <=v;j++)
  44. {
  45. ma[i][j]=MAX;
  46. }
  47. ma[i][i]=0;
  48. vis[i]=0;
  49. dis[i]=MAX;
  50. }
  51. for(int i = 1 ;i<=u;i++)
  52. {
  53. int a , b , len;
  54. cin>>a>>b>>len;
  55. n=max(max(n,a),b);
  56. if(ma[a][b]>len)
  57. {
  58. ma[a][b]=ma[b][a]=len;
  59. }
  60. }
  61. dijk();
  62. printf("%d\n",dis[v]);
  63. }
  64. return 0 ;
  65. }

解法二(Bellman-Ford)

  1. //*bellman算法:
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <algorithm>
  6. #define N 2010
  7. #define MAX 99999999
  8. using namespace std ;
  9. struct node{
  10. int a , b , w ;
  11. }edge[N];
  12. int n , m ;
  13. void bell()
  14. {
  15. int i , j ;
  16. int  d[N];
  17. for(int i =1 ; i<=n;i++)//*距离初始化为无穷;
  18. {
  19. d[i]=MAX;
  20. }
  21. d[1]=0;//*初始地点为0;
  22. for(i=1;i<=n;i++)
  23. {
  24. for(j=1;j<=m;j++)//*按点-边搜,顺便解决了重边问题;
  25. {
  26. if(d[edge[j].a]>d[edge[j].b]+edge[j].w) d[edge[j].a]= d[edge[j].b]+edge[j].w;
  27. if(d[edge[j].b]>d[edge[j].a]+edge[j].w) d[edge[j].b]= d[edge[j].a]+edge[j].w;
  28. }
  29. }
  30. printf("%d\n",d[n]);
  31. }
  32. int main()
  33. {
  34. int i , a   , b ,c;
  35. while(cin>>m>>n)
  36. {
  37. for(int i =1 ; i<=m;i++)//*结构体存边和权
  38. {
  39. cin>>a>>b>>c;
  40. edge[i].a=a;
  41. edge[i].b=b;
  42. edge[i].w=c;
  43. }
  44. bell();
  45. }
  46. return 0 ;
  47. }

方法三(Floyd-Warshall):虽然过不去数据,因为太大;但是值得一试;

    1. #include <iostream>
    2. #include <stdio.h>
    3. #include <math.h>
    4. #include <algorithm>
    5. #include <cstring>
    6. #define N 2000
    7. #define MAX 99999999
    8. using namespace std ;
    9. int u , v ;
    10. int dis[N][N];
    11. void warsh() {
    12. int i , j , k ;
    13. for(k=1; k<=v; k++) {
    14. for(i=1; i<=v; i++) {
    15. for(j=1; j<=v; j++) {
    16. dis[i][j]=min(dis[i][j],dis[k][j]+dis[i][k]);
    17. }
    18. }
    19. }
    20. }
    21. int main() {
    22. cin>>u>>v ;
    23. int a,  b , c ;
    24. for(int i = 1 ; i <= v ; i++) {
    25. for(int j = 1 ; j <=v; j++) {
    26. dis[i][j]=MAX;
    27. }
    28. }
    29. for(int i = 0 ; i < v ; i++) {
    30. dis[i][i]=0;
    31. }
    32. for(int i = 1 ; i <=u ; i++) {
    33. cin>>a>>b>>c;
    34. dis[a][b]=dis[b][a]=c;
    35. }
    36. warsh();
    37. cout<<dis[1][v]<<endl;
    38. return 0 ;
    39. }

Dijkstra算法与Bellman - Ford算法示例(源自网上大牛的博客)【图论】的更多相关文章

  1. Bellman - Ford 算法解决最短路径问题

    Bellman - Ford 算法: 一:基本算法 对于单源最短路径问题,上一篇文章中介绍了 Dijkstra 算法,但是由于 Dijkstra 算法局限于解决非负权的最短路径问题,对于带负权的图就力 ...

  2. Bellman—Ford算法思想

    ---恢复内容开始--- Bellman—Ford算法能在更普遍的情况下(存在负权边)解决单源点最短路径问题.对于给定的带权(有向或无向)图G=(V,E),其源点为s,加权函数w是边集E的映射.对图G ...

  3. random_shuffle (stl算法)打乱顺序 - 飞不会的日志 - 网易博客

    random_shuffle (stl算法)打乱顺序 - 飞不会的日志 - 网易博客 random_shuffle (stl算法)打乱顺序 2012-03-31 10:39:11|  分类: 算法 | ...

  4. 图论——最短路:Floyd,Dijkstra,Bellman-Ford,SPFA算法及最小环问题

    一.Floyd算法 用于计算任意两个节点之间的最短路径. 参考了five20的博客 Floyd算法的基本思想如下:从任意节点A到任意节点B的最短路径不外乎2种可能,1是直接从A到B,2是从A经过若干个 ...

  5. poj1860 bellman—ford队列优化 Currency Exchange

    Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22123   Accepted: 799 ...

  6. uva 558 - Wormholes(Bellman Ford判断负环)

    题目链接:558 - Wormholes 题目大意:给出n和m,表示有n个点,然后给出m条边,然后判断给出的有向图中是否存在负环. 解题思路:利用Bellman Ford算法,若进行第n次松弛时,还能 ...

  7. 算法笔记_070:BellmanFord算法简单介绍(Java)

    目录 1 问题描述 2 解决方案 2.1 具体编码   1 问题描述 何为BellmanFord算法? BellmanFord算法功能:给定一个加权连通图,选取一个顶点,称为起点,求取起点到其它所有顶 ...

  8. 排序算法系列:插入排序算法JAVA版(靠谱、清晰、真实、可用、不罗嗦版)

    在网上搜索算法的博客,发现一个比较悲剧的现象非常普遍: 原理讲不清,混乱 啰嗦 图和文对不上 不可用,甚至代码还出错 我总结一个清晰不罗嗦版: 原理: 和选择排序类似的是也分成“已排序”部分,和“未排 ...

  9. 排序算法系列:快速排序算法JAVA版(靠谱、清晰、真实、可用、不罗嗦版)

    在网上搜索算法的博客,发现一个比较悲剧的现象非常普遍: 原理讲不清,混乱 啰嗦 图和文对不上 不可用,甚至代码还出错 为了不误人子弟耽误时间,推荐看一些靠谱的资源,如[啊哈!算法]系列: https: ...

随机推荐

  1. 安装php的memcached模块和扩展支持sasl

    memcached的1.2.4及以上增加了CAS(Check and Set)协议,对于同一key的多进行程的并发处理问题.这种情况其实根数据库很像,如果同时有几个进程对同一个表的同一数据进行更新的话 ...

  2. Node.js 基础介绍(一)

    Node.js 学习笔记一) 简单介绍--名称 Node.js,平时听到有好几种叫法,node .Node.js.nodejs ,但是比较正式的称呼还是"Node.js",由于它是 ...

  3. go入门

    1.hello world 小程序 package main import "fmt" func main() { fmt.println("hello,世界" ...

  4. xBIM WeXplorer xViewer的导航,相机、剖切、隐藏 等操作

    目录 基础 xBIM WeXplorer 简要介绍 xBIM WeXplorer xViewer 基本应用 xBIM WeXplorer xViewer 浏览器检查 xBIM WeXplorer xV ...

  5. Android app性能测试小结(7个性能指标)

    1.性能测试的几个指标:       2.性能测试环境准备: 3.启动时间 3.1,监控值的获取方法 启动分为冷启动和热启动,冷启动:应用程序首次启动,进程首次创建并加载资源的过程:热启动:应用程序启 ...

  6. [原创]Oracle 12c的备份和恢复策略

    Oracle 12c的备份和恢复策略(RMAN备份[开启归档/控制文件/数据文件/归档日志]): 备份策略: * 每半年做一个数据库的全备份(包括所有的数据和只读表空间) * 每周做一次零级备份 * ...

  7. Java SocketChannel 读取ByteBuffer字节的处理模型

    在JAVA中的流分为字节流或字符流,一般来说采用字符流处理起来更加方便.字节流处理起来相对麻烦,SocketChannel中将数据读取到ByteBuffer中,如何取出完整的一行数据(使用CRLF分隔 ...

  8. Egret学习笔记 (Egret打飞机-7.实现敌机工厂)

    在游戏过程之,敌机是源源不断的冲屏幕上方往下飞,如果我们每一架敌机都直接new的话,在飞机很多的情况下,也许有性能问题. 就像前面子弹对象池一样,我们也要实现一个飞机对象池,也就是标题说的敌机工厂(之 ...

  9. acm水题3个:1.求最大公约数;2.水仙花数;3.判断完数

    //7.求两个整数的最大公约数#include<stdio.h>//用穷举法求出最大公约数int gcd1(int m,int n){ int min = m > n ? n : m ...

  10. Caffe可视化之VisualDL

    Visual DL是由 PaddlePaddle 和 ECharts 合作推出的一款深度学习可视化工具,其能够可视化scalar.参数分布.模型结构.图像等.底层采用C++编写,上层SDK以pytho ...