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

    Til the Cows Come Home
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 30865   Accepted: 10432

    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.

    赤裸裸的dij .... 竟然没看清题意.. 先输入边数再输入点数。。

    #include <iostream>
    #include <vector>
    #include <queue>
    #include <cstdio>
    using namespace std;
    typedef long long LL;
    const int N = 1010;
    const int inf = 1e9+7 ;
    int n , m , dis[N];
    
    struct node
    {
        int v , cost ;
        node(){};
        node( int a , int b ) { v = a , cost = b ;}
        bool operator < ( const node &a ) const{
            return cost > a.cost ;
        }
    };
    vector<node>g[N];
    
    int dij()
    {
        priority_queue<node>que;
        que.push( node(n,0));
        for( int i = 1 ; i <= n ; ++i )dis[i] = inf ;
        dis[n] = 0 ;
        while( !que.empty() )
        {
            int u = que.top().v ; que.pop();
            for( int i = 0 ; i < g[u].size(); ++i ){
                int v = g[u][i].v , w = g[u][i].cost;
                if( dis[u] + w >= dis[v] ) continue ;
                dis[v] = dis[u] + w;
                que.push(node(v,dis[v]));
            }
        }
        return dis[1];
    }
    
    void run()
    {
        int u , v , w ;
        for( int i = 0 ; i <= n ; ++i ) g[i].clear();
        while( m -- ){
            cin >> u >> v >> w;
            g[u].push_back(node(v,w));
            g[v].push_back(node(u,w));
        }
        cout << dij() << endl;
    }
    
    int main()
    {
        #ifdef LOCAL
            freopen( "in.txt","r",stdin );
        #endif // LOCAL
        ios::sync_with_stdio(false);
        while( cin >> m >> n ) run();
    }
    View Code
    only strive for your goal , can you make your dream come true ?
  • 相关阅读:
    MySQL性能优化的最佳经验
    18个网站SEO建议
    sql之left join、right join、inner join的区别
    PHP与MYSQL事务处理
    Firefox上Web开发工具库一览
    SphinxSE的安装
    python XML
    python yaml
    C语言文本处理
    Linux strace命令
  • 原文地址:https://www.cnblogs.com/hlmark/p/4069162.html
Copyright © 2011-2022 走看看