Pagini recente » Cod sursa (job #2266633) | Cod sursa (job #596166) | Cod sursa (job #131054) | Cod sursa (job #1073116) | Cod sursa (job #3263114)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");
#define pi pair <int,int>
#define int long long
const int MAXN=250010;
const int INF=2e18+10;
vector <pi> g[MAXN];
int n,d[MAXN];
pair <int,int> a[MAXN];
priority_queue <pi,vector <pi>, greater <pi>> pq;
void solvefor (int root){
for (int i=1;i<=n;++i){
d[i]=INF;
}
d[root]=0;
pq.push ({0,root});
while (!pq.empty ()){
int node=pq.top ().second,cost=pq.top ().first;
pq.pop ();
if (cost!=d[node]) continue;
for (auto x:g[node]){
if (cost+x.first<d[x.second]){
d[x.second]=cost+x.first;
pq.push ({d[x.second],x.second});
}
}
}
}
void solve (){
int rez=0;
for (int i=1;i<=n;++i){
solvefor (i);
for (int j=1;j<=n;++j){
if (i==j or d[j]==INF) continue;
rez=max (rez,d[j]);
}
}
cout <<rez;
}
void add_edge (int x, int y, int z){
g[x].push_back ({z,y});
//g[y].push_back ({z,x});
}
void read_data (){
int m;
fin >>n>>m;
for (int i=1;i<=m;++i){
int x,y,z;
fin >>x>>y>>z;
add_edge (x,y,z);
//cin >>a[i].first>>a[i].second;
}
}
void make_graph (){
for (int i=1;i<=n;++i){
for (int j=i+1;j<=n;++j){
if (a[i].first==a[j].first){
add_edge (i,j,abs (a[i].second-a[j].second));
}
else{
if (a[i].second==a[j].second){
add_edge (i,j,abs (a[i].first-a[j].first));
}
}
}
}
}
signed main()
{
ios_base::sync_with_stdio (false);
cin.tie (nullptr);
read_data ();
//make_graph ();
solvefor (1);
for (int i=2;i<=n;++i){
if (d[i]==INF) fout <<0<<' ';
else
fout <<d[i]<<' ';
}
return 0;
}