zoukankan      html  css  js  c++  java
  • (简单) POJ 2387 Til the Cows Come Home,Dijkstra。

      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.
     
      最短路模板题。。。
     
    代码如下:
    #include<iostream>
    #include<cstring>
    #include<queue>
    
    using namespace std;
    
    /////////////////////////////////////////////////////////////////
    
    const int MaxN=1010;
    const int INF=10e8;
    
    struct Node
    {
        int v,val;
    
        Node(int _v=0,int _val=0):v(_v),val(_val) {}
        bool operator < (const Node &a) const
        {
            return val>a.val;
        }
    };
    
    struct Edge
    {
        int v,cost;
    
        Edge(int _v=0,int _cost=0):v(_v),cost(_cost) {}
    };
    
    vector <Edge> E[MaxN];
    bool vis[MaxN];
    
    void Dijkstra(int lowcost[],int n,int start)
    {
        priority_queue <Node> que;
        Node qtemp;
        int len;
        int u,v,cost;
    
        for(int i=1;i<=n;++i)
        {
            lowcost[i]=INF;
            vis[i]=0;
        }
        lowcost[start]=0;
    
        que.push(Node(start,0));
    
        while(!que.empty())
        {
            qtemp=que.top();
            que.pop();
    
            u=qtemp.v;
    
            if(vis[u])
                continue;
    
            vis[u]=1;
    
            len=E[u].size();
    
            for(int i=0;i<len;++i)
            {
                v=E[u][i].v;
                cost=E[u][i].cost;
    
                if(!vis[v] && lowcost[v]>lowcost[u]+cost)
                {
                    lowcost[v]=lowcost[u]+cost;
                    que.push(Node(v,lowcost[v]));
                }
            }
        }
    }
    
    inline void addEdge(int u,int v,int c)
    {
        E[u].push_back(Edge(v,c));
    }
    
    /////////////////////////////////////////////////////////////////
    
    int ans[1010];
    
    int main()
    {
        ios::sync_with_stdio(false);
    
        int N,T;
        int a,b,c;
    
        cin>>T>>N;
    
        for(int i=1;i<=T;++i)
        {
            cin>>a>>b>>c;
    
            addEdge(a,b,c);
            addEdge(b,a,c);
        }
    
        Dijkstra(ans,N,N);
    
        cout<<ans[1]<<endl;
    
        return 0;    
    }
    View Code
  • 相关阅读:
    composer设置国内镜像
    mac安装composer
    composer安装laravel
    How to use MySQL 5.6 with MAMP 3 and MAMP PRO 3
    视觉惯性里程计:IMU预积分
    通过安卓NDK调用opencv4android 并通过adb shell 测试生成的二进制文件
    ubuntu: aptget update的时候遇到“Hash Sum mismatch”错误
    卡尔曼滤波 (Kalman Filter)的一个简单实现: 恒定加速度模型
    双目相机与IMU camera IMU 联合标定工具箱使用方法——Kalibr
    HKUST VINSMONO :香港科大开源VINSSLAM算法 part 2
  • 原文地址:https://www.cnblogs.com/whywhy/p/4338585.html
Copyright © 2011-2022 走看看