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
  • 相关阅读:
    如何快速且深入的学习一门新技术
    为什么说云原生会成为未来企业技术变迁的趋势
    高并发场景下锁的使用技巧
    开箱即用~基于.NET Core的敏捷开发框架规划
    为什么在做微服务设计的时候需要DDD?
    为什么我使用了索引,查询还是慢?
    解读中兴通信在物联网行业如何践行DDD
    服务发现技术是如何演进出来的?
    关于盘点和总结的那点事儿
    文件上传 通过 ServletContext.getRealPath()获取不到路径&war与war exploded的区别
  • 原文地址:https://www.cnblogs.com/caitian/p/5835806.html
Copyright © 2011-2022 走看看