蓝桥王国(Dijkstra)

2.蓝桥王国 - 蓝桥云课

 

问题描述

小明是蓝桥王国的王子,今天是他登基之日。

在即将成为国王之前,老国王给他出了道题,他想要考验小明是否有能力管理国家。

题目的内容如下:

蓝桥王国一共有 NN 个建筑和 MM 条单向道路,每条道路都连接着两个建筑,每个建筑都有自己编号,分别为 1∼N1∼N 。(其中皇宫的编号为 11)

国王想让小明回答从皇宫到每个建筑的最短路径是多少,但紧张的小明此时已经无法思考,请你编写程序帮助小明回答国王的考核。

输入描述

输入第一行包含两个正整数 N,MN,M。

第 22 到 M+1M+1 行每行包含三个正整数 u,v,wu,v,w,表示 u→vu→v 之间存在一条距离为 ww 的路。

1≤N≤3×1051≤N≤3×105,1≤m≤1061≤m≤106,1≤ui,vi≤N1≤ui​,vi​≤N,0≤wi≤1090≤wi​≤109。

输出描述

输出仅一行,共 NN 个数,分别表示从皇宫到编号为 1∼N1∼N 建筑的最短距离,两两之间用空格隔开。(如果无法到达则输出 −1−1)

输入输出样例

示例 1

输入

3 3 
1 2 1
1 3 5
2 3 2

输出

0 1 3

 

​
// Problem: 1. 蓝桥王国
// Contest: Lanqiao
// URL:
// https://www.lanqiao.cn/problems/1122/learning/?page=1&first_category_id=1&second_category_id=8&status=1
// Memory Limit: 512 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define int long long
using u32 = unsigned;
using i64 = long long;
using u64 = unsigned long long;
using PII = pair<int, int>;

const int N = 1e5 + 5, INF = 0x3f3f3f3f3f3f3f3f;

void dijkstra(vector<vector<PII>> graph, int n) {
  vector<int> dist(n + 1, INF);

  dist[1] = 0;
  priority_queue<PII, vector<PII>, greater<PII>> pq;

  pq.push({0, 1});
  while (!pq.empty()) {
    auto top = pq.top();
    pq.pop();
    int d = top.first, u = top.second;

    if (d > dist[u]) continue;

    for (auto i : graph[u]) {
      int v = i.first, w = i.second;

      if (dist[v] > dist[u] + w) {
        dist[v] = dist[u] + w;
        pq.push({dist[v], v});
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    if (dist[i] == INF)
      cout << -1 << ' ';
    else
      cout << dist[i] << ' ';
  }
}

void solve() {
  int n, m;
  cin >> n >> m;

  vector<vector<PII>> graph(n + 1);
  int u, v, w;
  for (int i = 0; i < m; i++) {
    cin >> u >> v >> w;
    graph[u].push_back({v, w});
  }
  dijkstra(graph, n);
}

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  solve();

  return 0;
}

/*
 *                        _oo0oo_
 *                       o8888888o
 *                       88" . "88
 *                       (| -_- |)
 *                       0\  =  /0
 *                     ___/`---'\___
 *                   .' \\|     |// '.
 *                  / \\|||  :  |||// \
 *                 / _||||| -:- |||||- \
 *                |   | \\\  - /// |   |
 *                | \_|  ''\---/''  |_/ |
 *                \  .-\__  '-'  ___/-. /
 *              ___'. .'  /--.--\  `. .'___
 *           ."" '<  `.___\_<|>_/___.' >' "".
 *          | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *          \  \ `_.   \_ __\ /__ _/   .-` /  /
 *      =====`-.____`.___ \_____/___.-`___.-'=====
 *                        `=---='
 *
 *
 *      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *            佛祖保佑       永不宕机     永无BUG
 */

​

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值