【图论--Dijkstra】POJ 2387 Til the Cows Come Home

本文介绍了一道经典的图论问题——牛回家问题,并提供了详细的Dijkstra算法实现。该问题要求找到从任意节点回到牛棚的最短路径。


 POJ 2387 Til the Cows Come Home


Til the Cows Come Home
题目链接->http://poj.org/problem?id=2387
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 64593 Accepted: 21779

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.

Problem Idea

 【题意】
     题目要求概述一下则是:输入一个节点数为n,边数为m的无向图的各条边,即m条边的信息。
  要求你输出从 第1号节点到N号节点的距离的最大值,即 DJ.d[n-1]
 【类型】
  Dijkstra算法求单源最短路径
 【分析】
  模板题,入门
 【时间复杂度&&优化】
  O(nlog n)

Source Code

#include <iostream>
#include <queue>
#include <vector>
#include <cstring>
#include <cstdio>

const int nmax=100+5;
#define INF 1e8
int n,m;
using namespace std;

struct HeapNode{
    int d,u;//d为s到各个节点的距离,u为起点
    HeapNode(){}
    HeapNode(int d,int u):d(d),u(u){}
    bool operator <(const HeapNode &rhs)const{//自定义greater算子,优先输出d小的节点
        return d>rhs.d;
    }
};
struct Edge{
    int from,to,dist;
    Edge(){}
    Edge(int f,int t,int d):from(f),to(t),dist(d){}
};
struct Dijkstra{
    int n,m; //n为点数,m为边数
    vector<Edge> edges;//边列表,存储各边的编号
    vector<int>G[nmax];//邻接表,存储每个节点出发的边编号(从0开始编号),G[u][i]为起点u到节点i的边的编号
    bool done[nmax];//是否已永久标号
    int d[nmax];//源点s到各个节点的距离,id[i]为源点s到节点i的距离
    int p[nmax];//最短路中的上一条弧,p[i]为源点s到节点i的最短路中的最后一条边的编号
    Dijkstra(){} //记得写个空的构造函数,要不然DJ会报错

    void init(int n){
        this->n=n;
        for(int i=0;i<n;i++){
            G[i].clear();//清空邻接表
        }
        edges.clear();//清空边列表
    }

    void AddEdge(int from,int to,int dist){//如果是无向图,每条无向边调用两次AddEdge
       edges.push_back(Edge(from,to,dist));//边列表增加一条边
        m=edges.size();//边列表中有几条边
        G[from].push_back(m-1);//邻接表中的节点数目(下标从0开始)
    }

    void dijkstra(){//求源点0到其他节点的最短路径
        for(int i=0;i<n;i++) d[i]=INF;
        //d[s]=0;//记得源点从0开始,不是1开始
        d[0]=0; //本题中,源点为节点0
        memset(done,0, sizeof(done));

        priority_queue<HeapNode>q;
        q.push(HeapNode(d[0],0));//本题中优先队列初始化的节点为HeapNode(d[0],0)

        while(!q.empty()){
            //在所有未标号的节点中,选出d值最小的节点
            HeapNode x=q.top();//找到d值最大的节点
            q.pop();//弹出队头
            int u=x.u;//得到d值最大的节点的起点
            //给节点x标记
            if(done[u]) continue; //如果当前节点已经标号,则continue
            done[u]=1; //若未标号,则标记
            //遍历从x出发的所有边(x,y),更新d[y]=min{d[y],d[x]+w[x,y]}
            for(int i=0;i<G[u].size();i++){
                Edge& e=edges[G[u][i]];//得到从u起点出发到i节点的一条边e
                if(d[e.to]>d[u]+e.dist){
                    d[e.to]=d[u]+e.dist;
                    p[e.to]=G[u][i];
                    q.push(HeapNode(d[e.to],e.to));//添加节点y
                }
            }
        }
    }
}DJ;
int main() {
    while(cin>>n>>m){//输入节点数

        DJ.init(n);//切记要初始化,清空边列表和邻接表
        while(m--) {
            int u,v,d;
            scanf("%d%d%d",&u,&v,&d);//输入节点是从1开始的
            u--;v--;//记得把输入节点调整成0开始的
            DJ.AddEdge(u,v,d);
            DJ.AddEdge(v,u,d);
        }

        DJ.dijkstra();//得到节点0到其他节点的最短路径长度

        cout<<DJ.d[n-1]<<endl;//求节点0到节点n-1的最短距离

    }
    return 0;
}



内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值