Pagini recente » Cod sursa (job #2009012) | Cod sursa (job #591006) | Cod sursa (job #1794114) | Cod sursa (job #91726) | Cod sursa (job #2169041)
#include <fstream>
#include <queue>
#include <bitset>
#include <vector>
#define N 50003
#define oo 2000000000
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
bitset<N> f;
vector<pair<int,int> > g[N];
int n,m;
int dist[N];
priority_queue<pair<int,int> > q; //-cost,nod;
int x,y,c;
void Dijkstra(int nod)
{
q.push(make_pair(0,nod));
for(int i=1; i<=n; ++i)
dist[i] = oo;
dist[nod] = 0;
int cost,noda;
int costurm,nodurm;
while(!q.empty())
{
noda = q.top().second;
q.pop();
if(f[noda] == 1) continue;
f[noda] = 1;
for(int i=0; i < g[noda].size(); ++i)
{
costurm = g[noda][i].second;
nodurm = g[noda][i].first;
if(dist[nodurm] > dist[noda] + costurm)
{
dist[nodurm] = dist[noda] + costurm;
q.push(make_pair(-dist[nodurm],nodurm));
}
}
}
for(int i=1; i<=n; ++i)
if(dist[i] == oo) dist[i] = 0;
}
int main()
{
fin>>n>>m;
while(m--)
{
fin>>x>>y>>c;
g[x].push_back(make_pair(y,c)); //nod,cost
}
Dijkstra(1);
for(int i=2; i<=n; ++i)
fout<<dist[i]<<" ";
return 0;
}