zoukankan      html  css  js  c++  java
  • Dijkstra算法与Bellman

    题意:题目大意:有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修改自己写的版本)

    [cpp] view plain copy
     
    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. }  
    37. int main()  
    38. {  
    39.     while(cin>>u>>v)  
    40.     {  
    41.         n=0;  
    42.         for(int i = 0 ; i <=v;i++)  
    43.         {  
    44.             for(int j = 0 ; j <=v;j++)  
    45.             {  
    46.                 ma[i][j]=MAX;  
    47.             }  
    48.             ma[i][i]=0;  
    49.             vis[i]=0;  
    50.             dis[i]=MAX;  
    51.         }  
    52.         for(int i = 1 ;i<=u;i++)  
    53.         {  
    54.             int a , b , len;  
    55.             cin>>a>>b>>len;  
    56.             n=max(max(n,a),b);  
    57.             if(ma[a][b]>len)  
    58.             {  
    59.                 ma[a][b]=ma[b][a]=len;  
    60.             }  
    61.         }  
    62.         dijk();  
    63.         printf("%d ",dis[v]);  
    64.     }  
    65.     return 0 ;  
    66. }  


    解法二(Bellman-Ford)

    [cpp] view plain copy
     
    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 ",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):虽然过不去数据,因为太大;但是值得一试;

    [cpp] view plain copy
     
      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.   
      23.     cin>>u>>v ;  
      24.     int a,  b , c ;  
      25.     for(int i = 1 ; i <= v ; i++) {  
      26.         for(int j = 1 ; j <=v; j++) {  
      27.             dis[i][j]=MAX;  
      28.         }  
      29.     }  
      30.     for(int i = 0 ; i < v ; i++) {  
      31.         dis[i][i]=0;  
      32.     }  
      33.     for(int i = 1 ; i <=u ; i++) {  
      34.         cin>>a>>b>>c;  
      35.         dis[a][b]=dis[b][a]=c;  
      36.     }  
      37.     warsh();  
      38.     cout<<dis[1][v]<<endl;  
      39.   
      40.     return 0 ;  
      41. }  
  • 相关阅读:
    我的安卓开始
    OLAP的一些知识——接下去的项目需要的背景
    关于Java接口
    Hexo+Github/Coding免费搭建个人博客网站
    手机自带输入法emoji表情的输入,提交及显示——前端解决方案
    改变函数中的 this 指向——神奇的call,apply和bind及其应用
    什么是jsonp?——使用jsonp解决跨域请求问题
    玩转angularJs——通过自定义ng-model,不仅仅只是input可以实现双向数据绑定
    利用jquery mobiscroll插件选择日期、select、treeList的具体运用
    转载:中年程序猿的迷茫,你还在深究技术吗?
  • 原文地址:https://www.cnblogs.com/cglongge/p/8484639.html
Copyright © 2011-2022 走看看