洛谷P1339 Heat Wave G

题目描述

有一个 nn 个点 mm 条边的无向图,请求出从 ss 到 tt 的最短路长度。

输入格式

第一行四个正整数 n,m,s,tn,m,s,t。 接下来 mm 行,每行三个正整数 u,v,wu,v,w,表示一条连接 u,vu,v,长为 ww 的边。

输出格式

输出一行一个整数,表示答案。

输入输出样例

输入 #1复制

7 11 5 4
2 4 2
1 4 3
7 2 2
3 4 3
5 7 5
7 3 3
6 1 1
6 3 4
2 4 3
5 6 3
7 2 1

输出 #1复制

7

说明/提示

【数据范围】
对于 100\%100% 的数据,1\le n \le 25001≤n≤2500,1\le m \le 62001≤m≤6200,1\le w \le 10001≤w≤1000。

【样例说明】
5 \to 6 \to 1 \to 45→6→1→4 为最短路,长度为 3+1+3 = 73+1+3=7。

上代码:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define go(i, j, n, k) for (int i = j; i <= n; i += k)
#define fo(i, j, n, k) for (int i = j; i >= n; i -= k)
#define rep(i, x) for (int i = h[x]; i; i = e[i].nxt)
#define mn 100010
#define mm 200020
#define inf 2147483647
#define ll long long
#define ld long double
#define fi first
#define se second
#define root 1, n, 1
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define bson l, r, rt
inline int read(){
    int f = 1, x = 0;char ch = getchar();
    while (ch > '9' || ch < '0'){if (ch == '-')f = -f;ch = getchar();}
    while (ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
    return x * f;
}
inline void write(int x){
    if (x < 0)putchar('-'),x = -x;
    if (x > 9)write(x / 10);
    putchar(x % 10 + '0');
}
//This is AC head above...
struct node{
    int v, nxt, w;
} e[mm << 1];
int h[mn], p;
inline void add(int a,int b,int c){
    e[++p].nxt = h[a];
    h[a] = p;
    e[p].v = b;
    e[p].w = c;
}
int dis[mn];
int n, m, s, t;
struct tree{
    int minw, minv;
};
struct SegmentTree{
    tree z[mn << 2];
    inline void update(int rt){
        z[rt].minw = min(z[rt << 1].minw, z[rt << 1 | 1].minw);//维护区间最小值
        z[rt].minv = (z[rt << 1].minw < z[rt << 1 | 1].minw) ? z[rt << 1].minv : z[rt << 1 | 1].minv;//维护区间最小值位置
    }
    inline void build(int l,int r,int rt){//建树
        if(l==r){
            z[rt].minw = l == s ? 0 : inf;//我们可以直接建树时把s的点设置为0
            z[rt].minv = l;//记录最小值位置,方便修改
            return;
        }
        int m = (l + r) >> 1;
        build(lson);
        build(rson);
        update(rt);
    }
    inline void modify(int l,int r,int rt,int now,int v){//单点修改
        if(l==r){
            z[rt].minw = v;
            return;
        }
        int m = (l + r) >> 1;
        if(now<=m)
            modify(lson, now, v);
        else
            modify(rson, now, v);
        update(rt);
    }
} tr;
inline void Dij(){//Dijkstra的核心部分
    go(i,1,n,1){
        dis[i] = inf;
    }//初始化dis
    dis[s] = 0;
    while(tr.z[1].minw < inf){//这里就是判断是否为空
        int x = tr.z[1].minv;//取整个线段树中最小的点
        tr.modify(root, x, inf);//单点修改最小的点为inf
        rep(i,x){
            int v = e[i].v;
            if(dis[v] > dis[x] + e[i].w){
                dis[v] = dis[x] + e[i].w;
                tr.modify(root, v, dis[x] + e[i].w);//这里就是类似入队操作
            }
        }
    }
}
int main(){
    n = read(), m = read(), s = read(), t=read();
    go(i,1,m,1){
        int x = read(), y = read(), v = read();
        add(x, y, v);
        add(y, x, v);//这个一定记住,无向图要正反两条边QAQ
    }
    tr.build(root);//建树
    Dij();//Dijkstra
    cout << dis[t];
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值