zoukankan      html  css  js  c++  java
  • POJ 2387 链式前向星下的SPFA

    (POJ)[http://poj.org/problem?id=2387] Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 69789 Accepted: 23386 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

    USACO 2004 November

    #include <stdio.h>
    #include <string.h>
    #include <string>
    #include <iostream>
    #include <stack>
    #include <queue>
    #include <vector>
    
    using namespace std;
    
    const int maxn = 1e5;
    const int maxm = 1050;
    const int inf = 0x3f3f3f3f;
    int n,m;
    
    struct node
    {
        int to,w,next;
    }e[maxn];
    int k;
    int head[maxn];
    int dis[maxn],vis[maxn];
    void add(int u,int v,int w)
    {
        e[k].to=v;
        e[k].next=head[u];
        e[k].w=w;
        head[u]=k++;
    }
    queue<int> q;
    void spfa(int st)
    {
        for(int i=1;i<=n;i++) dis[i]=inf;
        dis[st]=0;
        vis[st]=1;
        q.push(st);
        while(!q.empty())
        {
            int now = q.front();
            q.pop();
            vis[now]=0;
            for(int i=head[now]; i; i=e[i].next)
            {
                int ed = e[i].to;
                if(dis[ed] > dis[now] + e[i].w)
                {
                    dis[ed] = dis[now]+e[i].w;
                    if(!vis[ed])
                    {
                        q.push(ed);
                        vis[ed]=1;
                    }
                }
            }
        }
    }
    //79MS
    int main()
    {
        int x,y,z;
        while(cin>>m>>n)
        {
            memset(dis,inf,sizeof(dis));
            memset(vis,0,sizeof(vis));
            memset(head,-1,sizeof(head));
            k=1;
            for(int i=1;i<=m;i++)
            {
                cin>>x>>y>>z;
                add(x,y,z);
                add(y,x,z);
            }
            spfa(1);
            printf("%d
    ",dis[n]);
        }
    
    }
    /*
    给了一个无向图,输入t和n,
    t代表几个顶点,n代表问的是从第一个顶点到第n的顶点的最短距离,
    各种最短路算法的模板题
    Sample Input
    5 5
    1 2 20
    2 3 30
    3 4 20
    4 5 20
    1 5 100
    Sample Output
    90
    */
    
    

  • 相关阅读:
    makeBackronym
    Regular Ball Super Ball
    Svn忽略配置
    How does it work in C#?
    Counting sheep...
    Convert boolean values to strings 'Yes' or 'No'.
    codeforces 236A . Boy or Girl(串水问题)
    cocos2d-x3.0 解释具体的新的物理引擎setCategoryBitmask()、setContactTestBitmask()、setCollisionBitmask()
    ACM字符串处理算法经典:字符串搜索
    MessageFormat类别:快速格式化字符串
  • 原文地址:https://www.cnblogs.com/Roni-i/p/9292214.html
Copyright © 2011-2022 走看看