zoukankan      html  css  js  c++  java
  • POJ2387(KB4-A)

    Til the Cows Come Home

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 54716   Accepted: 18560

    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.

    Source

     
    dijkstra,坑点:有重边,取边权最小
     1 //2017-07-18
     2 #include <iostream>
     3 #include <cstdio>
     4 #include <cstring>
     5 
     6 using namespace std;
     7 
     8 const int N = 1010;
     9 const int inf = 0x3f3f3f3f;
    10 int t, n, G[N][N], dis[N], vis[N];
    11 
    12 void dijkstra(int s, int d)
    13 {
    14     for(int i = 1; i <= n; i++)
    15         dis[i] = G[s][i];
    16     dis[s] = 0;
    17     vis[s] = 1;
    18     int mindis, u;
    19     for(int i = 1; i <= n; i++)
    20     {
    21         mindis = inf;
    22         for(int j = 1; j <= n; j++)
    23             if(!vis[j] && dis[j] < mindis)
    24             {
    25                 mindis = dis[j];
    26                 u = j;
    27             }
    28         vis[u] = 1;
    29         for(int v = 1; v <= n; v++)
    30         {
    31             if(dis[v] > dis[u]+G[u][v]){
    32                 dis[v] = dis[u]+G[u][v];
    33             }
    34         }
    35     }
    36 }
    37 
    38 int main()
    39 {
    40     int s, d, u, v, w;
    41     while(cin>>t>>n)
    42     {
    43         for(int i = 1; i <= n; i++)
    44         {
    45             for(int j = 1; j <= n; j++)
    46                   G[i][j] = inf;
    47             dis[i] = inf;
    48             vis[i] = 0;
    49         }
    50         for(int i = 0; i < t; i++)
    51         {
    52             cin>>u>>v>>w;
    53             if(G[u][v] > w)
    54                 G[u][v] = G[v][u] = w;
    55         }
    56         dijkstra(n, 1);
    57         cout<<dis[1]<<endl;
    58     }
    59 
    60     return 0;
    61 }
     1 import java.util.*; // 2018-03-28
     2     
     3 public class Main {
     4     static final int INF = 0x3f3f3f3f;
     5     static Graph graph;
     6     static int [] dist;
     7     
     8     static boolean spfa(int s, int n) {
     9         boolean [] vis = new boolean[n+1];
    10         int [] cnt = new int[n+1];
    11         for(int i = 1; i <= n; i++) {
    12             dist[i] = INF;
    13             vis[i] = false;
    14         }
    15         Queue<Integer> que = new LinkedList<Integer>();
    16         que.offer(s);
    17         dist[s] = 0;
    18         vis[s] = true;
    19         while(!que.isEmpty()) {
    20             int u = que.poll();
    21             vis[u] = false;
    22             for(int i = graph.head[u]; i != -1; i = graph.edges[i].next) {
    23                 int v = graph.edges[i].v;
    24                 int w = graph.edges[i].w;
    25                 if(dist[v] > dist[u] + w) {
    26                     dist[v] = dist[u] + w;
    27                     if(!vis[v]) {
    28                         vis[v] = true;
    29                         que.offer(v);
    30                         if(++cnt[v] > n)return false;
    31                     }
    32                 }
    33             }
    34         }
    35         return true;
    36     }
    37     
    38     public static void main(String[] args) {
    39         Scanner cin = new Scanner(System.in);
    40         
    41         int n, m;
    42         while(cin.hasNext()) {
    43             m = cin.nextInt();
    44             n = cin.nextInt();
    45             graph = new Graph(n, 2*m);
    46             int u, v, w;
    47             for(int i = 0; i < m; i++) {
    48                 u = cin.nextInt();
    49                 v = cin.nextInt();
    50                 w = cin.nextInt();
    51                 graph.addEdge(u, v, w);
    52                 graph.addEdge(v, u, w);
    53             }
    54             dist = new int[n+1];
    55             if(spfa(1, n)) {
    56                 System.out.println(dist[n]);
    57             }
    58         }
    59     }
    60 }
    61     
    62 class Graph{
    63     static class Edge{
    64         int v, w, next;
    65         
    66         Edge(int _v, int _w, int _next){
    67             this.v = _v;
    68             this.w = _w;
    69             this.next = _next;
    70         }
    71     }
    72     
    73     int n, m, tot;
    74     int [] head;
    75     Edge [] edges;
    76     
    77     Graph(int _n, int _m){
    78         this.n = _n;
    79         this.m = _m;
    80         tot = 0;
    81         head = new int[n+1];
    82         edges = new Edge[m+1];
    83         for(int i = 0; i <= n; i++)
    84             head[i] = -1;
    85     }
    86     
    87     void addEdge(int u, int v, int w) {
    88         edges[tot] = new Edge(v, w, head[u]);
    89         head[u] = tot++;
    90     }
    91 }
  • 相关阅读:
    JavaScript 的 Promise
    MacOS copy图标shell脚本
    ExtJS 修改load paging时的参数
    JSONP
    8种跨域解决方案
    Ext Store Proxy Ajax
    ExtJS 自定义组件
    MacOS Apache配置
    xshell 上传 下载文件
    shell 内网主机存活探测器
  • 原文地址:https://www.cnblogs.com/Penn000/p/7202695.html
Copyright © 2011-2022 走看看