Pagini recente » Diferente pentru teorema-chineza-a-resturilor intre reviziile 89 si 32 | Cod sursa (job #3153676) | Cod sursa (job #1513360) | Cod sursa (job #201688) | Cod sursa (job #1738183)
#include <fstream>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <iterator>
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int dist[50010];
int n,m;
const int INF=1<<30;
vector <pair <int,int> > graf[50010];
priority_queue <pair <int,int>,vector<pair <int,int> > , greater<pair <int,int> > > pq;
void read()
{
f >> n >>m;
int a,b,c;
for(int i=0;i<m;i++){
f >> a >> b >>c;
graf[a].push_back(make_pair(c,b));
}
}
void dijkstra()
{
for(int i=2;i<=n;i++)
dist[i]=INF;
dist[1]=0;
pq.push(make_pair(0,1));
int w,now,next,cost;
while(!pq.empty())
{
w=pq.top().first;
now=pq.top().second;
pq.pop();
for(vector<pair<int,int> >::iterator it=graf[now].begin();it!=graf[now].end();it++)
{
cost=it->first;
next=it->second;
if(dist[next]>w+cost)
{
dist[next]=w+cost;
pq.push(make_pair(dist[next],next));
}
}
}
}
int main()
{
read();
dijkstra();
for(int i=2;i<=n;i++)
if(dist[i]!=INF)
g << dist[i] << " ";
else g << 0 << " ";
return 0;
}