Pagini recente » Cod sursa (job #3245655) | Cod sursa (job #2562401) | Cod sursa (job #82871) | Cod sursa (job #1316701) | Cod sursa (job #3336063)
// #include <bits/stdc++.h>
// using namespace std;
// ifstream fin ("date.in");
// #define rep(a,b) for(int i=a;i<=a;i++)
// int n,m,k,s;
// struct muchie{int a,b,cost;};
// vector<muchie> M;
// vector<int>control;
// int main() {
// fin>>n>>m;
// rep(1,m) {
// int x,y,z;
// fin>>x>>y>>z;
// M.push_back({x,y,z});
// M.push_back({y,x,z});
// }
// fin>>k;
// rep(1,k) {
// int x;
// fin>>x;
// control.push_back(x);
// }
// fin>>s;
//
//
//
// return 0;
// }
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");
typedef long long ll;
const ll INF = 1e18;
#define rep(a,b) for(int i=a;i<=b;i++)
vector<vector<pair<int,int>>> M;
vector<ll> d;
int n,m;
void djikstra(int s) {
d[s] = 0;
priority_queue<pair<int,int>, vector<pair<int,int>>,greater<pair<int,int>>> Q;
Q.push({d[s],s});
while (!Q.empty()) {
auto [dist_u, u] = Q.top();
Q.pop();
if (dist_u > d[u]) continue;
for (auto [v, cost] : M[u])
if (d[u] + cost < d[v]) {
d[v] = d[u] + cost;
Q.push({d[v],v});
}
}
}
int main() {
fin>>n>>m;
M.resize(n+1);
d.assign(n+1,INF);
rep(1,m) {
int x,y,z;
fin>>x>>y>>z;
M[x].push_back({y,z}); // v cost
}
djikstra(1);
rep(2,n) {
if (d[i] == INF) fout<<0<<' ';
else fout<<d[i]<<' ';
}
return 0;
}