zoukankan      html  css  js  c++  java
  • F

    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

    Hint

    INPUT DETAILS: 

    There are five landmarks. 

    OUTPUT DETAILS: 

    Bessie can get home by following trails 4, 3, 2, and 1.
     
    题目意思:有N个点,有一头牛在N点,求到1点的最短路径;
     
    解法:只是一道标准求最短路的问题,最短路的问题有三种算法:Dijkstra 算法,Bellman_Ford 算法,SPFA 算法
    第三种算法是第二种的优化;有个博客写的不错:http://www.61mon.com/index.php/archives/196/
     
    第一种算法:Dijkstra 算法
     
     1 #include<iostream>
     2 #include<stack>
     3 using namespace std;
     4 
     5 const int MAX = 200000 +100;  //假设权值最大不超过10000
     6 
     7 struct Edge
     8 {
     9     int a,b;
    10     int len;
    11 
    12 };
    13 Edge edge[MAX];
    14 long long dist[MAX];
    15 int T,N;
    16 int source;
    17 
    18 void BellmanFord()
    19 {
    20     dist[N] = 0;
    21     for(int i = 1;i <N;i++)
    22         dist[i] = 1e12;
    23 
    24     for(int i = 1;i <= N;i++)
    25     {
    26         for(int j = 0;j < T+T;j++)
    27         {
    28             if( dist[edge[j].b] > dist[edge[j].a] +edge[j].len )
    29             {
    30                 dist[edge[j].b ] = dist[edge[j].a] +edge[j].len;
    31             }
    32         }
    33     }
    34 
    35 }
    36 
    37 int main()
    38 {
    39     source = 1;
    40     cin>>T>>N;
    41     for(int i = 0;i<T;i++)
    42     {
    43         cin>>edge[i].a>>edge[i].b>>edge[i].len;
    44         edge[i+T].a = edge[i].b;
    45         edge[i+T].b = edge[i].a;
    46         edge[i+T].len = edge[i].len;
    47 
    48     }
    49 
    50 
    51     BellmanFord();
    52 
    53     cout<<dist[1]<<endl;
    54 
    55     return 0;
    56 }

    第二种算法:Bellman_Ford 算法

    #include<iostream>
    #include <string.h>
    using namespace std;
    
    const int MAX = 5000;
    const long long MAX1 = 1e12;
    
    int visit[MAX];
    long long Map[MAX][MAX];
    long long dist[MAX];
    int T,N;
    
    void D()
    {
        for(int i =1;i <=N;i++)
        {
            dist[i] = Map[N][i];
            visit[i] = 0;
        }
        visit[N] = 1;
    
        long long min_cost;
        int min_cost_num;
        for(int i = 1;i < N;i++)
        {
            min_cost = MAX1;
            for(int i = 1;i <=N;i++)
            {
                if(visit[i]==0&&dist[i]<min_cost)
                {
                    min_cost = dist[i];
                    min_cost_num = i;
                }
            }
            visit[min_cost_num] = 1;
    
            for(int i = 1;i <= N;i++)
            {
                if(visit[i]==0&&dist[i]>dist[min_cost_num]+Map[min_cost_num][i])
                {
                    dist[i] = dist[min_cost_num]+Map[min_cost_num][i];
    
                }
            }
    
        }
    }
    
    int main()
    {
        cin>>T>>N;
        for(int j = 1;j<=N;j++)
        for(int i = 1;i <=N;i++)
        {
            if(i != j)
                Map[j][i] = MAX1;
            else
                Map[j][j] = 0;
        }
    
        for(int i =1 ;i <= T;i++)
        {
            int x,y,len;
            cin>>x>>y>>len;
            if(len < Map[x][y])
                Map[x][y] = Map[y][x] = len;
        }
    
        D();
        cout<<dist[1]<<endl;
    
    
        return 0;
    }
     
     
     
     
  • 相关阅读:
    在Vue脚手架里面使用font-awsome
    在webstorm上使用git
    smartGit继续使用的方法
    工作笔记
    “老司机”传授给“小白”的职业经验
    兼容性问题(目前遇到的)
    web前端页面项目经验总结
    jquery中隐藏div的几种方法
    懒加载和预加载
    JS 中的事件绑定、事件监听、事件委托
  • 原文地址:https://www.cnblogs.com/a2985812043/p/7299286.html
Copyright © 2011-2022 走看看