zoukankan      html  css  js  c++  java
  • dijstra模板

    #include <iostream>
    #include<queue>
    #include<cstdio>
    #include<cstring>
    #include<vector>
    using namespace std;
    const int inf=0x3f3f3f3f;
    const int Max=1e4+10;
    typedef struct P
    {
        int from,cost;//from为父亲节点
        friend operator <(P a,P b){return a.cost<b.cost;}//重载一个判断,使优先队列先取出价值小的//
    }P;
    typedef struct edge
    {
        int to,cost;//to为子节点
    }edge;
    vector<edge> G[Max];//记录节点之间的关系
    int d[105];
    int n,m;
    void dijk()
    {
        fill(d+1,d+n+1,inf);
        priority_queue<P> que;
        d[1]=0;
        P p;
        p.from=1;
        p.cost=0;
        que.push(p);
        while(!que.empty()){
            p=que.top();
            que.pop();
            int v=p.from;
            for(int i=0;i<G[v].size();i++){
                edge e=G[v][i];
                if(d[e.to]>d[v]+e.cost){
                    d[e.to]=d[v]+e.cost;
                    p.from=e.to;
                    p.cost=d[e.to];
                    que.push(p);
                }
            }
        }
    }
    int main()
    {
        int u,v,w;
        edge e;
        while(~scanf("%d%d",&n,&m)&&(n||m)){
            for(int i=0;i<1010;++i) G[i].clear();
            memset(d,0,sizeof d);
            for(int i=0;i<m;i++){
                scanf("%d%d%d",&u,&v,&w);
                e.to=v,e.cost=w;
                G[u].push_back(e);
                e.to=u;
                G[v].push_back(e);
            }
            dijk();
            if(d[n]==inf)
                printf("-10086
    ");
            else
                printf("%d
    ",d[n]);
        }
        return 0;
    }
    
    

  • 相关阅读:
    DBA操作规范
    MySQL高可用之MHA
    Get MySQL这5个优化技巧,你将如虎添翼
    数据库的那些事
    Kubernetes
    nginx错误分析 `104: Connection reset by peer`
    kubernets资源预留
    kubernetes Pod亲和性
    kubernetes cpu限制参数说明
    zabbix 面板graph图上没有数据显示
  • 原文地址:https://www.cnblogs.com/Levi-0514/p/9042493.html
Copyright © 2011-2022 走看看