zoukankan      html  css  js  c++  java
  • 洛谷P4779 【模板】单源最短路径(标准版)

    P4779 【模板】单源最短路径(标准版)

     

    题目背景

    2018 年 7 月 19 日,某位同学在 NOI Day 1 T1 归程 一题里非常熟练地使用了一个广为人知的算法求最短路。

    然后呢?

    100 ightarrow 6010060;

    Ag ightarrow CuAgCu;

    最终,他因此没能与理想的大学达成契约。

    小 F 衷心祝愿大家不再重蹈覆辙。

    题目描述

    给定一个 NN 个点,MM 条有向边的带非负权图,请你计算从 SS 出发,到每个点的距离。

    数据保证你能从 SS 出发到任意点。

    输入输出格式

    输入格式:

    第一行为三个正整数 N, M, SN,M,S。 第二行起 MM 行,每行三个非负整数 u_i, v_i, w_iui,vi,wi,表示从 u_iui 到 v_ivi 有一条权值为 w_iwi 的边。

    输出格式:

    输出一行 NN 个空格分隔的非负整数,表示 SS 到每个点的距离。

    输入输出样例

    输入样例#1: 复制
    4 6 1
    1 2 2
    2 3 2
    2 4 1
    1 3 5
    3 4 3
    1 4 4
    输出样例#1: 复制
    0 2 4 3

    说明

    样例解释请参考 数据随机的模板题

    1 leq N leq 1000001N100000;

    1 leq M leq 2000001M200000;

    S = 1S=1;

    1 leq u_i, v_ileq N1ui,viN;

    0 leq w_i leq 10 ^ 90wi109,

    0 leq sum w_i leq 10 ^ 90wi109。

    本题数据可能会持续更新,但不会重测,望周知。

    2018.09.04 数据更新 from @zzq

    /*
        还记得一年前某人说SLF优化或者LLL优化的SPFA不会被卡,结果今天的32分怎么解释??
    */
    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<cstring>
    using namespace std;
    deque<int>q;
    bool vis[100010];
    int head[100010],dis[100010],cnt,n,m,s;
    struct node{
        int to,v,pre;
    }e[200010];
    void Insert(int from,int to,int v){
        e[++cnt].to=to;
        e[cnt].v=v;
        e[cnt].pre=head[from];
        head[from]=cnt;
    }
    int main(){
        scanf("%d%d%d",&n,&m,&s);
        int x,y,z;
        for(int i=1;i<=m;i++){
            scanf("%d%d%d",&x,&y,&z);
            Insert(x,y,z);
        }
        memset(dis,0x3f,sizeof(dis));
        q.push_back(s);vis[s]=1;dis[s]=0;
        while(!q.empty()){
            int now=q.front();q.pop_front();vis[now]=0;
            for(int i=head[now];i;i=e[i].pre){
                int to=e[i].to;
                if(dis[to]>dis[now]+e[i].v){
                    dis[to]=dis[now]+e[i].v;
                    if(!vis[to]){
                        vis[to]=1;
                        if(!q.empty()&&dis[to]<=dis[q.front()])q.push_front(to);
                        else q.push_back(to);
                    }
                }
            }
        }
        for(int i=1;i<=n;i++)printf("%d ",dis[i]);
        return 0;
    }
    32分 SLF优化spfa
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    using namespace std;
    struct edge{
        int to,v,pre;
    }e[200010];
    int cnt,n,m,s,head[100010],dis[100010];
    bool vis[100010];
    struct node{
        int id,dist;
        bool operator < (const node b)const{
            return dist>b.dist;
        }
    }a[100010];
    priority_queue<node>q;
    void Insert(int from,int to,int v){
        e[++cnt].to=to;
        e[cnt].v=v;
        e[cnt].pre=head[from];
        head[from]=cnt;
    }
    node make_node(int id,int dist){
        node ret;
        ret.id=id;ret.dist=dist;
        return ret;
    }
    void Dij(){
        q.push(make_node(s,0));dis[s]=0;
        while(!q.empty()){
            node cur=q.top();q.pop();int now=cur.id;
            if(vis[now])continue;
            vis[now]=1;
            for(int i=head[now];i;i=e[i].pre){
                int to=e[i].to;
                if(dis[to]>dis[now]+e[i].v){
                    dis[to]=dis[now]+e[i].v;
                    q.push(make_node(to,dis[to]));
                }
            }
        }
    }
    int main(){
        scanf("%d%d%d",&n,&m,&s);
        int x,y,z;
        for(int i=1;i<=m;i++){
            scanf("%d%d%d",&x,&y,&z);
            Insert(x,y,z);
        }
        memset(dis,0x3f,sizeof(dis));
        Dij();
        for(int i=1;i<=n;i++)printf("%d ",dis[i]);
        return 0;
    }
    100分 堆优化Dij
  • 相关阅读:
    什么是垃圾回收
    Oracle GoldenGate学习之Goldengate介绍
    JVM虚拟机选项:Xms Xmx PermSize MaxPermSize区别
    查看linux系统版本命令
    Case when 的用法,简单Case函数
    case when then else end
    ORACLE视图添加备注
    修改 tomcat 内存
    Linux 内存机制详解宝典
    oracle正则表达式regexp_like的用法详解
  • 原文地址:https://www.cnblogs.com/thmyl/p/11026750.html
Copyright © 2011-2022 走看看