Pagini recente » infoarena - te ajutam sa devii olimpic! | Cod sursa (job #2324837) | Cod sursa (job #2738639) | Cod sursa (job #2125895) | Cod sursa (job #1367206)
//#include <iostream>
#include<fstream>
#include<vector>
#include<queue>
#define mx 500005
//#define co 1001
#define inf 0x3f3f3f3f
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n,m,dist[mx];
vector<int> t[mx], v[mx];
struct cmp{
bool operator() (pair<int,int> a,pair<int, int> b){
return (a.first>b.first);
}
};
priority_queue <pair<int, int>, vector<pair<int, int> >,cmp> q;
void citire()
{
int i,x,y,c;
fin>>n>>m;
for(i=1;i<=m;i++)
{
fin>>x>>y>>c;
t[x].push_back(y);
v[x].push_back(c);
}
for(i=2;i<=n;i++)
dist[i]=inf;
dist[1]=0;
}
void dj()
{
q.push(make_pair(0,1));
while(!q.empty())
{
int nod=q.top().second;
int cost=q.top().first;
q.pop();
for(unsigned int i=0;i<t[nod].size();++i)
if(cost+v[nod][i]<dist[t[nod][i]])
{
dist[t[nod][i]]=cost+v[nod][i];
q.push(make_pair(dist[t[nod][i]],t[nod][i]));
}
}
}
int main()
{
citire();
dj();
for(int i=2;i<=n;i++)
if(dist[i]==inf)
fout<<"0 ";
else fout<<dist[i]<<" ";
return 0;
}