zoukankan      html  css  js  c++  java
  • Til the Cows Come Home(最短路)

    题目:
    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.
     
     
    题意:
    求一个点到点1的最小距离,很明显是个球最短路的模板题了,我们可以用Dijkstra算法,Floyd算法,对于Bell—man算法我还不太理解,基本上没怎么用过Bell—man算法,但是在求含负权图的单源路径的时候就只能用Bell—man算法,还有书上没讲的spaf算法,在开学前之前争取找时间把这个算法弄懂(其实线段树,字典树什么的我还压根不会),要补的实在是太多!!!
     
    Dijkstra算法:
    AC代码:
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    const int N = 1005;
    const int MAX = 1000000;
    int t,n;
    int cost[N][N];
    void init()
    {
        for (int i=1;i<=n;i++)
            for (int j=i;j<=n;j++)
          {
            if (i==j)
               cost[i][j]=cost[j][i]=0;
            else
               cost[i][j]=cost[j][i]=MAX;
          }
    }
    void Dijkstra()
    {
        int mini,k;
    int lowcost[N];
    int fina[N];
        for (int i=1;i<=n;i++)
        {
              lowcost[i]=cost[1][i];
              fina[i]=0;
        }
           fina[1]=1;
        for (int i=2;i<=n;i++)
        {
            mini=MAX;
            k=0;
            for (int j=1;j<=n;j++)
                if (!fina[j]&&lowcost[j]<mini)
                {
                    k=j;
                    mini=lowcost[j];
                }
            fina[k]=1;
            for (int j=1;j<=n;j++)
                if (!fina[j]&&mini+cost[k][j]<lowcost[j])
                    lowcost[j]=cost[k][j]+mini;
        }
        cout << lowcost[n] << endl;
    }
    int main()
    {
    
        int x,y,z;
       while (cin>>t>>n)
       {
           init();
           for (int i=1;i<=t;i++)
          {
            cin>>x>>y>>z;
            if (cost[x][y]>z)
            cost[x][y]=cost[y][x]=z;
          }
            Dijkstra();
       }
    
        return 0;
    }
    Floyd算法:
    超时代码:
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    const int N = 1005;
    const int MAX = 1000000;
    int t,n;
    int cost[N][N];
    void init()
    {
        for (int i=1;i<=n;i++)
            for (int j=i;j<=n;j++)
          {
            if (i==j)
               cost[i][j]=cost[j][i]=0;
            else
               cost[i][j]=cost[j][i]=MAX;
          }
    }
    void Floyd()
    {
       for (int k=1;k<=n;k++)
           for (int i=1;i<=n;i++)
               for (int j=1;j<=n;j++)
               cost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j]);
      int mini=0;
     for (int i=2;i<=n;i++)
        if (mini<cost[1][i])
        mini=cost[1][i];
      cout << mini << endl;
    }
    int main()
    {
    
        int x,y,z;
       while (cin>>t>>n)
       {
           init();
           for (int i=1;i<=t;i++)
          {
            cin>>x>>y>>z;
            if (cost[x][y]>z)
            cost[x][y]=cost[y][x]=z;
          }
           Floyd();
       }
    
        return 0;
    }
  • 相关阅读:
    .NET Core HttpClient调用腾讯云对象存储Web API的"ERROR_CGI_PARAM_NO_SUCH_OP"问题
    .NET Core 控制台程序读 appsettings.json 、注依赖、配日志、设 IOptions
    腾讯云短信服务使用记录与.NET Core C#代码分享
    .net core中使用Type.GetType()从字符串获取类型遇到的问题
    阿里云不同账号之间相同地域的VPC网络互访
    ASP.NET Core 实现用户登录验证的最低配置
    体验 ASP.NET Core 中的多语言支持(Localization)
    .NET Core 2.0 单元测试中初识 IOptionsMonitor<T>
    体验 PHP under .NET Core
    docker swarm:执行 service update 过程中服务短暂不能访问的问题
  • 原文地址:https://www.cnblogs.com/lisijie/p/7424927.html
Copyright © 2011-2022 走看看