次短路模板,待检查

//#define LOCAL
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mem(a, b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define DNF 0x7f
#define DBG printf("this is a input\n")
#define fi first
#define se second
#define mk(a, b) make_pair(a,b)
#define pb push_back
#define LF putchar('\n')
#define SP putchar(' ')
#define p_queue priority_queue
#define CLOSE ios::sync_with_stdio(0); cin.tie(0)

template<typename T>
void read(T &x) {x = 0;char ch = getchar();ll f = 1;while(!isdigit(ch)){if(ch == '-')f *= -1;ch = getchar();}while(isdigit(ch)){x = x * 10 + ch - 48; ch = getchar();}x *= f;}
template<typename T, typename... Args>
void read(T &first, Args& ... args) {read(first);read(args...);}
template<typename T>
void write(T arg) {T x = arg;if(x < 0) {putchar('-'); x =- x;}if(x > 9) {write(x / 10);}putchar(x % 10 + '0');}
template<typename T, typename ... Ts>
void write(T arg, Ts ... args) {write(arg);if(sizeof...(args) != 0) {putchar(' ');write(args ...);}}
using namespace std;

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

ll lcm(ll a, ll b) {
    return a / gcd(a, b) * b;
}
const int N = 200005;
int n , m;
int head[N] , cnt;
struct node1 {
    int t, next, w;
}edge[N * 2];
void add (int f, int t, int w)
{
    edge[cnt].t = t;
    edge[cnt].w = w;
    edge[cnt].next = head[f];
    head[f] = cnt ++;
}
int dis[N][2] , vis[N][2];
struct node
{
    int v;
    int value;
    int mark;
    node(int a, int b, int c):v(a),value(b),mark(c){};
    bool operator < (const node& no) const {
        return value > no.value;
    }
};

void dijkstra(int s) {
    mem(dis, INF);
    mem(vis, 0);
    p_queue<node> q;
    dis[s][0] = 0;
    dis[s][0] = 0;
    q.push(node(s, 0, 0));
    while (!q.empty()) {
        node no = q.top();
        q.pop();
        int u = no.v;
        int flag = no.mark;
        if (vis[u][flag]) continue;
        vis[u][flag] = 1;
        for (int i = head[u]; i != -1; i = edge[i].next)
        {
            int v = edge[i].t, w = edge[i].w;
            if (dis[v][0] > dis[u][flag] + w)
            {
                if (dis[v][0] != INF)
                {
                    dis[v][1] = dis[v][0];
                    q.push({v, dis[v][1], 1});
                }
                dis[v][0] = dis[u][flag] + w;
                q.push({v, dis[v][0], 0});
            }
            else if (dis[v][1] > dis[u][flag] + w)
            {
                dis[v][1] = dis[u][flag] + w;
                q.push({v, dis[v][1], 1});
            }
        }
    }
}
int main()
{
    read (n, m);
    mem(head,-1);
    for (int i = 1 ; i <= m ; i ++)
    {
        int u , v, w;
        read (u ,v , w);
        add (u , v, w);
        add (v , u, w);
    }
    dijkstra(1);
    for (int i = 2 ; i <= n ; i ++)
        if(dis[i][1] == INF)
            write(-1),LF;
        else
            write(dis[i][1]),LF;
}

/*
10
3 8 96
6 2 57
11 15 58
5 20 98
18 20 91
8 20 75
1 3 33
1 17 69
12 2 46
4 5 31
19 8
9 18
 */

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值