zoukankan      html  css  js  c++  java
  • POJ 2387 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.
     
    最短路问题。
    dijkstra算法:
     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <algorithm>
     4 #define INF 0x3f3f3f3f
     5 using namespace std;
     6 const int N = 1010;
     7 int G[N][N], vis[N], d[N];
     8 int t, n;
     9 void dijkstra() {
    10     for(int i = 1; i <= n; i ++) d[i] = INF, vis[i] = 0;
    11     d[1] = 0;
    12     while(true) {
    13         int v = -1;
    14         for(int i = 1; i <= n; i ++) {
    15             if(!vis[i] && (v == -1 || d[v] > d[i])) v = i;
    16         }
    17         if(v == -1) break;
    18         vis[v] = 1;
    19         for(int i = 1; i <= n; i ++)
    20         d[i] = min(d[i],d[v]+G[i][v]);
    21     }
    22 }
    23 int main() {
    24     while(scanf("%d%d",&t,&n) != EOF) {
    25         for(int i = 1; i <= n; i ++) {
    26             for(int j = 1; j <= n; j ++)
    27             G[i][j] = (i==j ? 0: INF);
    28         }
    29         for(int i = 1; i <= t; i ++) {
    30             int u, v, w;
    31             scanf("%d %d %d", &u, &v, &w);
    32             if(G[u][v] > w) {
    33                 G[u][v] = G[v][u] = w;
    34             }
    35         }
    36         dijkstra();
    37         printf("%d
    ",d[n]);
    38     }
    39     return 0;
    40 }

    优化的dijksta算法:

     1 #include <iostream>
     2 #include <queue>
     3 #include <string.h>
     4 #include <stdio.h>
     5 #include <vector>
     6 #include <queue>
     7 #define INF 0x3f3f3f3f
     8 using namespace std;
     9 const int N = 2010;
    10 struct edge {
    11     int to, cost;
    12 };
    13 vector<edge> G[N];
    14 int d[N], t, n;
    15 typedef pair<int,int> P;
    16 void dijkstra() {
    17     priority_queue<P, vector<P>, greater<P> > que;
    18     for(int i = 1; i <= n; i ++) d[i] = INF;
    19     d[1] = 0;
    20     que.push(P(0,1));
    21     while(!que.empty()) {
    22         P p = que.top(); que.pop();
    23         int v = p.second;
    24         if(d[v] < p.first) continue;
    25         for(int i = 0; i < G[v].size(); i ++) {
    26             edge e = G[v][i];
    27             if(d[e.to] > d[v] + e.cost) {
    28                 d[e.to] = d[v] + e.cost;
    29                 que.push(P(d[e.to], e.to));
    30             }
    31         }
    32     }
    33 }
    34 int main() {
    35     while(scanf("%d%d",&t,&n)!=EOF) {
    36         for(int i = 1; i <= t; i ++) {
    37             int u, v, w;
    38             scanf("%d %d %d",&u, &v, &w);
    39             G[u].push_back((edge){v,w});
    40             G[v].push_back((edge){u,w});
    41         }
    42         dijkstra();
    43         printf("%d
    ",d[n]);
    44     }
    45     return 0;
    46 }
  • 相关阅读:
    background-position 使用方法具体介绍
    Android平台上直接物理内存读写漏洞的那些事
    自己编写高负荷測试的工具
    String,StringBuffer与StringBuilder的差别??
    shell之here文档
    心跳检测的思路及代码
    高可用架构篇--MyCat在MySQL主从复制基础上实现读写分离
    MySQL主从复制之Mycat简单配置和高可用
    Mycat 读写分离+分库分表
    MyCat:对MySQL数据库进行分库分表
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/7276826.html
Copyright © 2011-2022 走看看