Pagini recente » Cod sursa (job #2719583) | Cod sursa (job #1480952) | Cod sursa (job #2099177) | Cod sursa (job #1560227) | Cod sursa (job #1893955)
#include <fstream>
#include <queue>
#include <vector>
#define inf 1<<30
#define nmax 50005
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int n,m,d[nmax];
vector <pair <int,int > > v[nmax];
vector <pair <int,int > > :: iterator it;
priority_queue<pair <int, int>, vector <pair <int,int > > , greater <pair <int ,int > > > heap;
int main()
{
int i,j,x,y,z;
f>>n>>m;
for (i=1;i<=m;i++) {
f>>x>>y>>z;
v[x].push_back(make_pair(y,z));
}
d[1]=0;
for (i=2;i<=n;i++)
d[i]=inf;
heap.push(make_pair(0,1));
while (!heap.empty()) {
x=heap.top().second;
heap.pop();
for (it=v[x].begin();it!=v[x].end();it++) {
y=it->first;
z=it->second;
if (d[y]>d[x]+z) {
d[y]=d[x]+z;
heap.push(make_pair(d[y],y));
}
}
}
for (i=2;i<=n;i++)
if (d[i]!=inf)
g<<d[i]<<' ';
else
g<<"0 ";
return 0;
}