This commit is contained in:
1 parent
095dcf1eac
commit
c89b0d349f
2 files changed
+102
No files matched your search
@@ -0,0 +1,32 @@
|
|||||||
|
#include<bits/stdc++.h>
|
||||||
|
#define int long long
|
||||||
|
using namespace std;
|
||||||
|
int n, m, s;
|
||||||
|
int d[100005];
|
||||||
|
vector<pair<int, int>> fri[100005];
|
||||||
|
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
|
||||||
|
signed main() {
|
||||||
|
scanf("%lld%lld%lld", &n, &m, &s);
|
||||||
|
memset(d, 0x7f, sizeof d);
|
||||||
|
d[s] = 0;
|
||||||
|
for(int i = 1, u, v, w; i <= m; ++i) {
|
||||||
|
scanf("%lld%lld%lld", &u, &v, &w);
|
||||||
|
fri[u].push_back({w, v});
|
||||||
|
}
|
||||||
|
q.push({0, s});
|
||||||
|
while(!q.empty()) {
|
||||||
|
pair<int, int> now = q.top();
|
||||||
|
q.pop();
|
||||||
|
if (now.first != d[now.second]) continue;
|
||||||
|
for(pair<int, int> to:fri[now.second]) {
|
||||||
|
if(d[to.second] > d[now.second] + to.first) {
|
||||||
|
d[to.second] = d[now.second] + to.first;
|
||||||
|
q.push({d[to.second], to.second});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(int i = 1; i <= n; ++i) {
|
||||||
|
printf("%lld ", d[i]);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
# P4779 【模板】单源最短路径(标准版)
|
||||||
|
|
||||||
|
## 题目背景
|
||||||
|
|
||||||
|
2018 年 7 月 19 日,某位同学在 [NOI Day 1 T1 归程](https://www.luogu.com.cn/problem/P4768) 一题里非常熟练地使用了一个广为人知的算法求最短路。
|
||||||
|
|
||||||
|
然后呢?
|
||||||
|
|
||||||
|
$100 \rightarrow 60$;
|
||||||
|
|
||||||
|
$\text{Ag} \rightarrow \text{Cu}$;
|
||||||
|
|
||||||
|
最终,他因此没能与理想的大学达成契约。
|
||||||
|
|
||||||
|
小 F 衷心祝愿大家不再重蹈覆辙。
|
||||||
|
|
||||||
|
## 题目描述
|
||||||
|
|
||||||
|
给定一个 $n$ 个点,$m$ 条有向边的带非负权图,请你计算从 $s$ 出发,到每个点的距离。
|
||||||
|
|
||||||
|
数据保证你能从 $s$ 出发到任意点。
|
||||||
|
|
||||||
|
## 输入格式
|
||||||
|
|
||||||
|
第一行为三个正整数 $n, m, s$。
|
||||||
|
第二行起 $m$ 行,每行三个非负整数 $u_i, v_i, w_i$,表示从 $u_i$ 到 $v_i$ 有一条权值为 $w_i$ 的有向边。
|
||||||
|
|
||||||
|
## 输出格式
|
||||||
|
|
||||||
|
输出一行 $n$ 个空格分隔的非负整数,表示 $s$ 到每个点的距离。
|
||||||
|
|
||||||
|
## 输入输出样例 #1
|
||||||
|
|
||||||
|
### 输入 #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
|
||||||
|
```
|
||||||
|
|
||||||
|
## 说明/提示
|
||||||
|
|
||||||
|
样例解释请参考 [数据随机的模板题](https://www.luogu.com.cn/problem/P3371)。
|
||||||
|
|
||||||
|
$1 \leq n \leq 10^5$;
|
||||||
|
|
||||||
|
$1 \leq m \leq 2\times 10^5$;
|
||||||
|
|
||||||
|
$s = 1$;
|
||||||
|
|
||||||
|
$1 \leq u_i, v_i\leq n$;
|
||||||
|
|
||||||
|
$0 \leq w_i \leq 10 ^ 9$,
|
||||||
|
|
||||||
|
$0 \leq \sum w_i \leq 10 ^ 9$。
|
||||||
|
|
||||||
|
本题数据可能会持续更新,但不会重测,望周知。
|
||||||
|
|
||||||
|
2018.09.04 数据更新 from @zzq
|
||||||
Reference in new issue
Block a user