This commit is contained in:
Zengtudor 2024-11-17 22:01:54 +08:00
parent f737de093d
commit d368c89147
4 changed files with 99 additions and 0 deletions

59
src/P3371/P3371.cpp Normal file
View File

@ -0,0 +1,59 @@
#include <algorithm>
#include <cstdint>
#include <functional>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using ll = int64_t;
using std::cin, std::cout;
const ll maxn = 1e5+5;
ll n, m, s, dist[maxn], inf {(1ll<<31)-1};
std::vector<std::pair<ll, ll>> adj[maxn];
int main(){
std::iostream::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
cin>>n>>m>>s;
std::fill(dist, dist+1+n, inf);
dist[s]=0;
for(ll i{1}; i<=m; i++){
ll u, v, w;
cin>>u>>v>>w;
for(auto& edge: adj[u]){
if(edge.first==v){
edge.second=std::min(edge.second, w);
goto end;
}
}
adj[u].emplace_back(v, w);
end:;
}
std::priority_queue<std::pair<ll, ll>, std::vector<std::pair<ll, ll>>, std::greater<>> pq;
pq.emplace(0, s);
while(pq.size()){
auto [d, u] = pq.top();
pq.pop();
if(d>dist[u])continue;
for(auto& edge: adj[u]){
auto [v, w] {edge};
if(dist[u] + w < dist[v]){
dist[v] = dist[u] + w;
pq.emplace(dist[v], v);
}
}
}
for(ll i{1}; i<=n; i++){
cout<<dist[i]<<' ';
}
cout<<'\n';
}

16
src/P3371/P3371_1.in Normal file
View File

@ -0,0 +1,16 @@
5 15 5
2 5 181
1 5 98
4 2 49
3 2 262
4 3 26
2 4 192
5 1 221
2 2 254
4 4 233
1 5 44
5 4 67
4 2 214
1 1 47
1 1 118
5 4 3

1
src/P3371/P3371_1.out Normal file
View File

@ -0,0 +1 @@
221 52 29 3 0

23
src/P6147/P6147.cpp Normal file
View File

@ -0,0 +1,23 @@
#include <cstdint>
#include <iostream>
#include <vector>
using ll = int64_t;
using std::cin, std::cout;
const ll maxn = 1e5+5;
ll n;
std::vector<ll> adj[maxn];
int main(){
std::iostream::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
cin>>n;
for(ll i{1}; i<n; i++){
ll u, v;
cin>>u>>v;
adj[u].push_back(v);
}
}