zoukankan      html  css  js  c++  java
  • poj2387- Til the Cows Come Home(最短路板子题)

    题目描述

    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

    Hint

    INPUT DETAILS: 

    There are five landmarks. 

    OUTPUT DETAILS: 

    Bessie can get home by following trails 4, 3, 2, and 1.
     
     
    大意就是求1到n之间的最短路径,由于点的个数只有1000,O(N^2)dijkstra的算法即可解决。
    代码如下
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    #include<string>
    #include<vector>
    #include<stack>
    #include<bitset>
    #include<cstdlib>
    #include<cmath>
    #include<set>
    #include<list>
    #include<deque>
    #include<map>
    #include<queue>
    using namespace std;
    
    const int INF=2e9; 
    int V,E;
    int d[2005];
    int cost[2005][2005];
    int used[2005];
    void dijkstra(int s)
    {
    
        for(int i=1;i<=V;i++)
        {
            used[i]=0;
            d[i]=INF;
         } 
         d[s]=0;
         int minn,v;
        for(int i=1;i<=V;i++)
        {
            minn=INF;
            v=-1;
            for(int j=1;j<=V;j++)
            {
                if(!used[j]&&d[j]<minn)
                {
                    minn=d[j];
                    v=j;
                }
            }
            used[v]=1;
            for(int j=1;j<=V;j++)
            {
                if((!used[j])&&(d[j]>cost[v][j]+d[v]))
                {
                    d[j]=cost[v][j]+d[v];
                }
            }    
    }
    }
    int main()
    {
        cin>>E>>V;
        for(int i=1;i<=2001;i++)
        {
            for(int j=1;j<=2001;j++)
            {
                cost[i][j]=INF;
                if(i==j)
                cost[i][j]=0;
             } 
        }
        for(int i=1;i<=E;i++)
        {
            int s,t,cos;
            cin>>s>>t>>cos;
            if(cos<cost[s][t])
            {
                cost[s][t]=cos;
                cost[t][s]=cos;
            }    
        }
        dijkstra(1);
        printf("%d
    ",d[V]);
        return 0;
    }
  • 相关阅读:
    Python网络爬虫——bs4基本用法
    Python网络爬虫——requests模块(1)
    yii gii配置ip限制使用gii
    openfire连接数据库mysql
    js 提示条
    jquery滚动条平滑滑动
    yii2.0 添加组件baidu ueditor
    yii添加验证码 和重复密码
    css图标库 font-awesome.min.css
    yii配置访问路由权限配置
  • 原文地址:https://www.cnblogs.com/hh13579/p/11232625.html
Copyright © 2011-2022 走看看