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.

    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

    理解dijkstra,对于所有未标号的点中选取一个d[x]最小的点,记为点x。将x标记
    对于所有x出发的边(x,y),更新d[y]=min(d[y],d[x]+mapp[x][y])
    其实本质上是一个用优先队列优化的bfs(优先访问最短边的终点),在bfs中加上松弛操作就行了,同时保证访问过的点集之间的最短路是知道的
    每次访问一个没有访问过的新点时,我们是要把加入访问过的点集中去的对吧!加入的前提就是与和它相连的访问过的所有点
    做一次松弛操作,更新下状态。那么与相邻但是没有访问过的点,我们加入优先队列。
    代码如下:
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <queue>
     5 #include <algorithm>
     6 #include <vector>
     7 using namespace std;
     8 #define inf 0x3f3f3f3f
     9 const int maxn =1010;
    10 vector <int>G[maxn];//存每个点连的边的编号
    11 bool done[maxn];//标记某点是否访问过
    12 int p[maxn];//最短路中某个点的上一条路是几号
    13 int d[maxn];//原点到某点的距离
    14 int t,n;
    15 struct Edge{    //
    16     int from,to,dis;
    17     Edge(int u,int v,int d):from(u),to(v),dis(d){}
    18 };
    19 vector <Edge>edges;
    20 struct HeapNode {//堆点,用于优化
    21     int d,u;
    22     bool operator <(const HeapNode& x) const {//优先考虑距离最小
    23         return d>x.d;
    24     }
    25 };
    26 void addEdge(int u,int v,int dis){//加边函数
    27     edges.push_back(Edge(u,v,dis));
    28     int m=edges.size();
    29     G[u].push_back(m-1);
    30 }
    31 void dijkstra (int s) {
    32     priority_queue<HeapNode> q;
    33     memset(d,inf,sizeof d);
    34     d[s]=0;
    35     memset(done,false,sizeof done);
    36     q.push(HeapNode{0,s});
    37     while (!q.empty()){
    38         HeapNode x=q.top();
    39         q.pop();
    40         int u=x.u;
    41         if (done[u])
    42             continue;
    43         done[u]=true;
    44         for (int i=0;i<G[u].size();++i){
    45             Edge& e=edges[G[u][i]];
    46             if (d[e.to]>d[u]+e.dis){//当e.to这个点是done过的点时,功能是得到
    47                 d[e.to]=d[u]+e.dis;
    48                 p[e.to]=G[u][i];
    49                 q.push((HeapNode){d[e.to],e.to});
    50             }
    51         }
    52     }
    53 }
    54 void init(){
    55     for (int i=0;i<n;++i)
    56     G[i].clear();
    57     edges.clear();
    58 }
    59 int main()
    60 {
    61     //freopen("de.txt","r",stdin);
    62     while (~scanf("%d%d",&t,&n)){
    63         init();
    64         for (int i=0;i<t;++i){
    65             int x,y,z;
    66             scanf("%d%d%d",&x,&y,&z);
    67             addEdge(x,y,z);
    68             addEdge(y,x,z);
    69         }
    70         dijkstra(1);
    71         printf("%d
    ",d[n]);
    72     }
    73     return 0;
    74 }
    
    
    
    
    
  • 相关阅读:
    maven解决“Could not calculate build plan”问题
    HTTP中的重定向和请求转发的区别
    (转)《杂 文》 之 教你制作一份属于自己的简历
    (转)《SSO CAS单点系列》之 实现一个SSO认证服务器是这样的!
    (转)《SSO CAS单点系列》之 15分钟让你了解SSO技术到底是个什么鬼!
    (转)帮你深入理解OAuth2.0协议
    opengl库区分:glut、freeglut、glfw、glew、gl3w、glad
    OpenGL中的光照技术(翻译)
    [转]gluProject 和 gluUnproject 的详解
    英文版Ubuntu18.10安装搜狗输入法过程(图文并茂,亲自尝试!)
  • 原文地址:https://www.cnblogs.com/agenthtb/p/6072164.html
Copyright © 2011-2022 走看看