zoukankan      html  css  js  c++  java
  • poj 2387 最短路模板题

    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

    O(n^2)
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 #include <algorithm>
     5 #include <vector>
     6 #include <queue>
     7 #include <set>
     8 #include <map>
     9 #include <string>
    10 #include <cmath>
    11 #include <stdlib.h>
    12 #define MAXSIZE 1005
    13 using namespace std;
    14 
    15 int t,n;
    16 int m[MAXSIZE][MAXSIZE],dis[MAXSIZE],vis[MAXSIZE];
    17 
    18 void dij(int s)
    19 {
    20     int i,j,k;
    21     for(i = 0;i<n;i++)
    22     {
    23         dis[i] = m[s][i];
    24         vis[i] = 0;
    25     }
    26     dis[s] = 0;
    27     vis[s] = 1;
    28     for(i = 1;i<=n;i++)
    29     {
    30         k = 0;
    31         int mn = 1000000;
    32         for(j = 1;j<=n;j++)
    33             if(!vis[j]&&dis[j]<mn)
    34             {
    35                 mn = dis[j];
    36                 k = j;
    37             }
    38         vis[k] = 1;
    39         for(j = 1;j<=n;j++)
    40             if(!vis[j]&&dis[j]>dis[k]+m[k][j])
    41                 dis[j] = dis[k]+m[k][j];
    42     }
    43 }
    44 
    45 int main()
    46 {
    47     freopen("caicai.txt","r",stdin);
    48     cin>>t>>n;
    49     int i,j,a,b,w;
    50     for(i = 0;i<=n;i++)
    51         for(j = 0;j<=n;j++)
    52             m[i][j] = 1000;
    53     for(i = 0;i<t;i++)
    54     {
    55         scanf("%d%d%d",&a,&b,&w);
    56         if(m[a][b]>w)
    57         {
    58             m[a][b] = w;
    59             m[b][a] = w;
    60         }
    61     }
    62     dij(n);
    63     cout<<dis[1]<<endl;
    64     return 0;
    65 }
    View Code
  • 相关阅读:
    Feature的开发和部署
    MOSS 2007应用如何修改上传文件大小及类型的限制
    学习Ajax的优秀网站
    Office SharePoint 权限开发
    Asp.net操作Excel汇总
    如何取到MOSS列表中item的链接
    解决MOSS、SharePoint的未知错误
    Ajax 之 XMLHttpRequest
    C#中从资源文件里加载文件
    linux 技巧:使用 screen 管理你的远程会话 [linux]
  • 原文地址:https://www.cnblogs.com/caitian/p/5835806.html
Copyright © 2011-2022 走看看