Pagini recente » Cod sursa (job #995776) | Cod sursa (job #2881743) | Cod sursa (job #148952) | Cod sursa (job #436694) | Cod sursa (job #748751)
Cod sursa(job #748751)
#include <fstream>
#include <iostream>
#include <functional>
#include <vector>
#include <queue>
#define INFINIT 32000
using namespace std;
vector<pair<int,int> > G[50002];
int main()
{
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > pq;
int N,M,i,x,y,z,d[50002],curent,viz[50002];
f>>N>>M;
for(i=1;i<=M;i++)
{
f>>x>>y>>z;
G[x].push_back(make_pair(y,z));
G[y].push_back(make_pair(x,z));
}
d[1]=0;
for(i=2;i<=N;i++)
d[i]=INFINIT;
pq.push(make_pair(1,0));
while(!pq.empty())
{
curent=pq.top().first;
pq.pop();
if(!viz[curent])
{
for(vector<pair<int,int> >::iterator it=G[curent].begin();it!=G[curent].end();it++)
{
if ( d[it->first] > (d[curent] + it->second) )
{
d[it->first] = (d[curent] + it->second);
pq.push(make_pair(it->first,it->second));
}
}
viz[curent]=1;
}
}
for(i=2;i<=N;i++) g<<d[i]<<" ";
f.close();
g.close();
return 0;
}