zoukankan      html  css  js  c++  java
  • Poj2387--Til the Cows Come Home(Spfa)

    Til the Cows Come Home

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 35672   Accepted: 12102

    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

    #include <queue>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    using namespace std;
    const int INF = 0x3f3f3f3f;
    int map[1010][1010], vis[1010], dis[1010];
    int t, n;
    void Spfa(int src)
    {
        memset(vis, 0, sizeof(vis));
        for(int i = 1; i <= n; i++)
            dis[i] = INF;
        dis[src] = 0;
        vis[src] = 1;
        queue<int> q;
        q.push(src);
        while(!q.empty())
        {
            int temp = q.front();
            q.pop();
            vis[temp] = 0;
            for(int i = 1; i <= n; i++)   //遍历每个节点 ; 
            {
                if(dis[i] > dis[temp] + map[temp][i])
                {
                    dis[i] = dis[temp] + map[temp][i];
                    if(!vis[i])
                    {
                        vis[i] = 1;
                        q.push(i);
                    }
                }
            }
        }
    }
    int main()
    {
        while(~scanf("%d %d", &t, &n))
        {
            int a, b, c, i, j;
            for(i = 1; i <= n; i++)
                for(j = 1; j <= n; j++)
                    map[i][j]=(i==j?0:INF);
            for(i = 1; i <= t; i++)
            {
                scanf("%d %d %d", &a, &b, &c);
                if(map[a][b] > c)
                    map[a][b] = map[b][a] =c;
            }
            Spfa(1);
            printf("%d
    ", dis[n]);
        }
        return 0;    
    }  
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 using namespace std;
     5 const int INF = 0x3f3f3f3f; 
     6 int dis[1010], vis[1010], map[1010][1010];
     7 int n, t;
     8 void Dijkstra(int src)
     9 {
    10     int i, j;
    11     memset(vis, 0, sizeof(vis));
    12     for(i = 1; i <= n; i++)
    13         dis[i] = map[src][i];
    14     vis[src] = 1;
    15     for(i = 1; i < n; i++)
    16     {
    17         int temp, min = INF;
    18         for(j = 1; j <= n; j++)
    19         {
    20             if(dis[j] < min && !vis[j])   //未标记的通过循环; 
    21             {
    22                 temp = j;
    23                 min = dis[j]; 
    24             } 
    25         }
    26         vis[temp] = 1;
    27         for(j = 1; j <= n; j++)          //松弛操作; 
    28         {
    29             if(dis[j] > dis[temp] + map[temp][j])
    30                 dis[j] = dis[temp] + map[temp][j]; 
    31         }
    32     }
    33 }
    34 int main()
    35 {
    36     while(~scanf("%d %d", &t, &n))
    37     {
    38         int a, b, c;
    39         for(int i = 1; i <= n; i++)
    40             for(int j = 1; j <= n; j++)
    41                 map[i][j]=(i==j?0:INF);
    42         for(int i = 1; i <= t; i++)
    43         {
    44             scanf("%d %d %d", &a, &b, &c);
    45                 if(map[a][b] > c)
    46                     map[a][b] = map[b][a] = c;
    47         }
    48         Dijkstra(1);
    49         printf("%d
    ", dis[n]);
    50     }
    51     return 0;
    52 } 
    Dijkstra
  • 相关阅读:
    POJ3159 Candies —— 差分约束 spfa
    POJ1511 Invitation Cards —— 最短路spfa
    POJ1860 Currency Exchange —— spfa求正环
    POJ3259 Wormholes —— spfa求负环
    POJ3660 Cow Contest —— Floyd 传递闭包
    POJ3268 Silver Cow Party —— 最短路
    POJ1797 Heavy Transportation —— 最短路变形
    POJ2253 Frogger —— 最短路变形
    POJ1759 Garland —— 二分
    POJ3685 Matrix —— 二分
  • 原文地址:https://www.cnblogs.com/soTired/p/4717731.html
Copyright © 2011-2022 走看看