mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-11-09 17:18:49 +00:00
1128. 信使
https://www.acwing.com/problem/content/submission/code_detail/7846048/
This commit is contained in:
parent
69089b2dc5
commit
03a003fbea
42
AcWing/1128/1128.cpp
Normal file
42
AcWing/1128/1128.cpp
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int n, m, u, v, w, dist[105], ans;
|
||||||
|
vector<pair<int, int>> g[105];
|
||||||
|
bool vis[105];
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
cin >> n >> m;
|
||||||
|
for (int i = 0; i < m; i++) {
|
||||||
|
cin >> u >> v >> w;
|
||||||
|
g[u].push_back(make_pair(v, w));
|
||||||
|
g[v].push_back(make_pair(u, w));
|
||||||
|
}
|
||||||
|
memset(dist, 0x3f, sizeof(dist));
|
||||||
|
dist[1] = 0;
|
||||||
|
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
|
||||||
|
q.push(make_pair(0, 1));
|
||||||
|
while (!q.empty()) {
|
||||||
|
auto t = q.top();
|
||||||
|
q.pop();
|
||||||
|
if (vis[t.second]) continue;
|
||||||
|
for (auto i : g[t.second]) {
|
||||||
|
if (dist[i.first] > t.first + i.second) {
|
||||||
|
dist[i.first] = t.first + i.second;
|
||||||
|
q.push(make_pair(dist[i.first], i.first));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
vis[t.second] = true;
|
||||||
|
}
|
||||||
|
for (int i = 1; i <= n; i++) {
|
||||||
|
if (dist[i] == 0x3f3f3f3f) {
|
||||||
|
cout << -1 << endl;
|
||||||
|
exit(0);
|
||||||
|
} else {
|
||||||
|
ans = max(ans, dist[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cout << ans << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user