zoukankan      html  css  js  c++  java
  • Til the Cows Come Home(最短路模板题)

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

    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.

     

     

    //题目意思是,第一行有两个整数n,m,说明有n个边,m个点,接下来n行,每行有三个整数,a,b,c,说明从 a 到 b 距离是多少,输出从1- n 的最小路程

    //显然,这是一道水题,dijstra算法 4116kb 110ms

     1 //dijkstra 算法
     2 #include <iostream>
     3 #include <cstdio>
     4 #include <string.h>
     5 using namespace std;
     6 #define INF 0x3f3f3f3f
     7 #define MX 1005
     8 
     9 int n,m;
    10 int mp[MX][MX];
    11 
    12 int dis[MX];
    13 bool vis[MX];
    14 
    15 void dijkstra()
    16 {
    17     memset(vis,0,sizeof(vis));
    18     memset(dis,0x3f,sizeof(dis));
    19     dis[1]=0;
    20 
    21     for(int i=1;i<=n;i++)
    22     {
    23         int mim=INF,v;
    24         for(int j=1;j<=n;j++)
    25             if(!vis[j] && dis[j]<mim)
    26             {
    27                 v=j;
    28                 mim=dis[j];
    29             }
    30         vis[v]=1;
    31         for(int j=1;j<=n;j++)
    32             if(!vis[j] && dis[j]>mp[v][j]+dis[v])
    33                 dis[j]=mp[v][j]+dis[v];
    34     }
    35     printf("%d
    ",dis[n]);
    36 }
    37 
    38 int main()
    39 {
    40     while(~scanf("%d%d",&m,&n))
    41     {
    42         memset(mp,0x3f,sizeof(mp));
    43         for(int i=0;i<m;i++)
    44         {
    45             int a,b,c;
    46             scanf("%d%d%d",&a,&b,&c);
    47             if(mp[a][b]>c) mp[a][b]=mp[b][a]=c;//只记最小的
    48         }
    49         dijkstra();
    50     }
    51     return 0;
    52 }
    View Code

    //spfa 算法,很牛逼 260kb 0ms过了

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <queue>
     5 using namespace std;
     6 #define INF 0x3f3f3f3f
     7 #define MXN 1005
     8 #define MXM 4010
     9 
    10 struct Edge{
    11     int to;
    12     int w;
    13     int nex;
    14 }edge[MXM];
    15 
    16 int n,m,r_m;
    17 int headlist[MXN];
    18 int dis[MXN];
    19 int vis[MXN];
    20 
    21 void spfa()
    22 {
    23     queue <int> Q;
    24     memset(vis,0,sizeof(vis));
    25     memset(dis,0x3f,sizeof(dis));
    26     dis[1]=0;
    27     vis[1]=1;
    28     Q.push(1);
    29     while(!Q.empty())
    30     {
    31         int u =Q.front();Q.pop();
    32         vis[u]=0;
    33         for(int i=headlist[u];i!=-1;i=edge[i].nex)
    34         {
    35             int to=edge[i].to, w=edge[i].w;
    36             if(dis[u]+w<dis[to])
    37             {
    38                 dis[to]=dis[u]+w;
    39                 if(!vis[to])
    40                 {
    41                     vis[to]=1;
    42                     Q.push(to);
    43                 }
    44             }
    45         }
    46     }
    47     printf("%d
    ",dis[n]);
    48 }
    49 
    50 int main(){
    51     int i,a,b,c;
    52     while(~scanf("%d%d",&m,&n))
    53     {
    54         for(i=1;i<=n;i++) headlist[i]=-1;
    55         r_m=0;
    56         for(i=0;i<m;i++)
    57         {
    58             scanf("%d%d%d",&a,&b,&c);
    59             edge[r_m]=(Edge){b,c,headlist[a]};
    60             headlist[a]=r_m;
    61             edge[r_m+1]=(Edge){a,c,headlist[b]};
    62             headlist[b]=r_m+1;
    63             r_m+=2;
    64         }
    65         spfa();
    66     }
    67     return 0;
    68 }
    View Code

     

     

     

  • 相关阅读:
    采用[ICONIX] 方法实践BLOG设计之一 [问题域建模]
    关于“三国众谋士”之IT从业可行性报告
    采用[ICONIX] 方法实践BLOG设计之二 [用例建模]
    NET框架中的 Decorator 和 Strategy 模式
    域模型向左走(充血),向右走(贫血)
    采用[ICONIX] 方法实践BLOG设计之五 [初步设计复核]
    Discuz!NT 缓存设计简析 [原创]
    Discuz!NT控件剖析 之 Tab 属性页 [原创: 附源码]
    没有银弹,但可以"扯蛋"
    Discuz!NT控件剖析 之 Button [原创: 附源码]
  • 原文地址:https://www.cnblogs.com/haoabcd2010/p/5709737.html
Copyright © 2011-2022 走看看