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

    个人心得:模板题,不过还是找到了很多问题,真的是头痛,为什么用dijkstra算法book【1】=1就错了.....

    纠结中....

    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<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<iomanip>
     6 #include<algorithm>
     7 using namespace std;
     8 #define inf 1<<29
     9 int t,n;
    10 int cow[1005][1005];
    11 int dis[1005];
    12 int book[1005];
    13 void dijkstra()
    14 {
    15     memset(book,0,sizeof(book));
    16     for(int i=1;i<=n;i++) dis[i]=cow[1][i];
    17     dis[1]=0;
    18     book[1]=1;
    19     for(int i=1;i<=n;i++)
    20     {
    21         int mina=inf;
    22         int k;
    23         for(int j=1;j<=n;j++)
    24         {
    25             if(book[j]==0&&mina>dis[j])
    26             {
    27                 mina=dis[j];
    28                 k=j;
    29                 }
    30         }
    31         book[k]=1;
    32         for(int j=1;j<=n;j++)
    33         {
    34                 if(book[j]==0&&dis[j]>dis[k]+cow[k][j])
    35                     dis[j]=dis[k]+cow[k][j];
    36             }
    37     }
    38 }
    39 int main()
    40 {
    41     while(cin>>t>>n){
    42     int x,y,z;
    43     for(int i=1;i<=n;i++)
    44          for(int j=1;j<=n;j++)
    45               if(i==j) cow[i][j]=0;
    46        else cow[i][j]=inf;
    47     for(int i=1;i<=t;i++){
    48         cin>>x>>y>>z;
    49         if(cow[x][y]>z)
    50        cow[x][y]=cow[y][x]=z;
    51 
    52     }
    53     dijkstra();
    54     cout<<dis[n]<<endl;}
    55         return 0;
    56 }
    View Code

    Bellman-Ford算法

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<iomanip>
     6 #include<algorithm>
     7 using namespace std;
     8 #define inf 1<<29
     9 int t,n;
    10 int u[2005],v[2005],w[2005];
    11 int dis[1005];
    12 int book[1005];
    13 int main()
    14 {
    15     while(cin>>t>>n)
    16     {
    17         for(int i=1;i<=t;i++)
    18             cin>>u[i]>>v[i]>>w[i];
    19         for(int i=1;i<=n;i++)
    20             dis[i]=inf;
    21         dis[1]=0;
    22         for(int i=1;i<=n;i++)
    23             for(int j=1;j<=t;j++){
    24                if(dis[v[j]]>dis[u[j]]+w[j])
    25                   dis[v[j]]=dis[u[j]]+w[j];
    26                   if(dis[u[j]]>dis[v[j]]+w[j])
    27                   dis[u[j]]=dis[v[j]]+w[j];
    28 
    29             }
    30         cout<<dis[n]<<endl;
    31 
    32     }
    33     return 0;
    34 }
    View Code

    Queue

     1 /*
     2 spfa
     3 Memory 256K
     4 Time   32MS
     5 */
     6 #include <iostream>
     7 #include <queue>
     8 using namespace std;
     9 #define inf 1<<29
    10 #define MAXM 4005
    11 #define MAXV 1005
    12 
    13 typedef struct{
    14     int a,b,w,next;
    15 }Edge;
    16 
    17 Edge edge[MAXM];
    18 int n,m,headlist[MAXV];
    19 
    20 void spfa(){
    21     int i,d[MAXV],v,vis[MAXV];
    22     queue <int>q;
    23     for(i=2;i<=n;i++){
    24         d[i]=inf;
    25         vis[i]=0;
    26     }
    27     d[1]=0;
    28     vis[1]=1;
    29     q.push(1);
    30     while(!q.empty()){
    31         v=q.front();q.pop();
    32         vis[v]=0;
    33 
    34         for(i=headlist[v];i!=-1;i=edge[i].next)
    35             if(d[v]+edge[i].w<d[edge[i].b]){
    36                 d[edge[i].b]=d[v]+edge[i].w;
    37                 if(!vis[edge[i].b]){
    38                     vis[edge[i].b]=1;
    39                     q.push(edge[i].b);
    40                 }
    41             }
    42     }
    43     printf("%d
    ",d[n]);
    44 }
    45 
    46 int main(){
    47     int i,a,b,c;
    48     while(~scanf("%d%d",&m,&n)){
    49         for(i=1;i<=n;i++) headlist[i]=-1;
    50         for(i=1;i<=2*m;i+=2){
    51             scanf("%d%d%d",&a,&b,&c);
    52             edge[i].a=a;
    53             edge[i].b=b;
    54             edge[i].w=c;
    55             edge[i].next=headlist[a];
    56             headlist[a]=i;
    57             edge[i+1].a=b;
    58             edge[i+1].b=a;
    59             edge[i+1].w=c;
    60             edge[i+1].next=headlist[b];
    61             headlist[b]=i+1;
    62         }
    63         spfa();
    64     }
    65     return 0;
    66 }
    View Code
  • 相关阅读:
    清除浮动的三种方式
    js控制滚动条默认在底部
    npm 基础命令
    package.json中^,~的区别
    mac 使用iTerm2快捷登录远程服务器
    iterm 分屏切换快捷键与配色设置
    git tag — 标签相关操作
    gulp iconfont
    webpack 3 升级 webpack4 个人笔记
    import * as x from 'xx' 和 import x from 'xx'
  • 原文地址:https://www.cnblogs.com/blvt/p/7307800.html
Copyright © 2011-2022 走看看