zoukankan      html  css  js  c++  java
  • PKU2387Til the Cows Come Home(SPFA+邻接表)

    这道题还是蛮经典的图论题。

    题意:

    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.
           由bidirectional(双向)可知此图是无向图。

           要求你输入ne(边数)和nv(顶点数),然后是每条路径s到t的权值w.

           然后小女孩从最后一个节点回到起点,求最短路。

    解题思路:

                 点的个数为1000,不算很大,所以邻接矩阵跟邻接表都可以。下面我用邻接表。

    代码:

    #include<iostream> 
     
    using namespace std; 
     
    #define MAXE 10000 
    #define MAXV 1001 
    int inf = 1000000000
     
    struct edge_t 

        int s, t; 
        int w; 
        int next; 
    }; 
     
    edge_t arrEdge[MAXE]; //arrEdge[1...szEdge]保存szEdge条边. 
    int szEdge;//主函数中方便记录边的位置 
    int arrHead[MAXV];//用来记录边的位置 
     
    int nv; 
    int arrDis[MAXV]; 
    int Que[MAXE*10]; //队列 
    char inQue[MAXV]; //点v是否在队列中 
     
    void SPFA(int s) 

        int i, ie, v, head, tail; 
        edge_t e; 
     
        for(i = 1; i <= nv; i++) 
            arrDis[i] = inf; 
     
        arrDis[s] = 0
     
        //初始化队列 
        memset(Que, 0sizeof(Que)); 
        memset(inQue, 0sizeof(inQue)); 
        head = 0, tail = 1
        Que[head] = s; 
        inQue[s] = 1
        while(head < tail) 
        { 
            v = Que[head++]; 
            inQue[v] = 0
     
            for(ie = arrHead[v]; ie!=-1; ie = arrEdge[ie].next) 
            { 
                e = arrEdge[ie]; 
                if(arrDis[e.t] > arrDis[v] + e.w)//当这里不能在松弛时,不用入队,即算法结束 
                { 
                    arrDis[e.t] = arrDis[v] + e.w; 
                    if(!inQue[e.t]) 
                    { 
                        inQue[e.t] = 1
                        Que[tail++] = e.t; 
                    } 
                } 
            } 
        } 
        cout<<arrDis[1]<<endl; 
        return ; 

     
    int main() 

        int ne, i, s, t, w, p; 
     
        memset(arrHead, -1sizeof(arrHead)); 
        memset(arrEdge, 0sizeof(arrEdge)); 
        szEdge = 0
     
        scanf("%d %d", &ne, &nv); 
        for(i = 1; i <= ne; i++) 
        { 
            scanf("%d %d %d", &s, &t, &w); 
            szEdge++; 
            arrEdge[szEdge].s = s; 
            arrEdge[szEdge].t = t; 
            arrEdge[szEdge].w = w; 
            arrEdge[szEdge].next = arrHead[s]; 
            arrHead[s] = szEdge; 
     
            szEdge++; 
            arrEdge[szEdge].s = t; 
            arrEdge[szEdge].t = s; 
            arrEdge[szEdge].w = w; 
            arrEdge[szEdge].next = arrHead[t]; 
            arrHead[t] = szEdge; 
        } 
     
        SPFA(nv); 
     
        return 0

  • 相关阅读:
    MyBatis中传入参数parameterType类型详解
    mybatis逆向工程无法生成带有_表明的解决办法。
    sql server 正则匹配查询数据
    Kibana按时间(小时,分钟,天)统计数据 ,
    AutoCAD 多重引线 文本 左右对齐的问题
    ObjectARXWizard2022安装
    综合练习:词频统计
    powerDesigner设计表自动生成sql语句,设置携带注释
    打jar包后,无法获取resources目录下的word模板文件
    solr新建core,并配置schema
  • 原文地址:https://www.cnblogs.com/cchun/p/2520123.html
Copyright © 2011-2022 走看看