Pagini recente » Cod sursa (job #3222367) | Cod sursa (job #1359296) | Cod sursa (job #1368105) | Cod sursa (job #2464646) | Cod sursa (job #1311061)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
const int NMAX=200000,inf=1<<30;
int d[NMAX],N,M;
vector <pair<int,int> > v[NMAX];
priority_queue <pair<int,int> > Q;
void citire()
{
int x,y,z;
ifstream fin("dijkstra.in");
fin>>N>>M;
while(M--)
{
fin>>x>>y>>z;
v[x].push_back(pair<int,int>(y,z));
}
}
void dijkstra_quece()
{
for(int i=2;i<=N;++i)
d[i]=inf;
Q.push( pair<int,int> (1,0) );
while( !Q.empty() )
{
pair<int,int> top;
top.first=Q.top().first;
top.second=Q.top().second;
Q.pop();
if(top.second<=d[top.first])
for(vector<pair<int,int> >::iterator it=v[top.first].begin();it!=v[top.first].end();++it)
if( d[it->first]> top.second + (it->second) )
{
d[it->first] = top.second + (it->second);
Q.push(pair<int,int>(it->first,it->second));
}
}
}
void afisare()
{
ofstream fout("dijkstra.out");
for(int i=2;i<=N;++i)
if(d[i]!=inf)
fout<<d[i]<<" ";
else
fout<<0;
}
int main()
{
citire();
dijkstra_quece();
afisare();
return 0;
}