Pagini recente » Cod sursa (job #2715217) | Cod sursa (job #613402) | Cod sursa (job #2898191) | Cod sursa (job #906638) | Cod sursa (job #505021)
Cod sursa(job #505021)
#include <cstdio>
#include <fstream>
#include <queue>
#include <vector>
#define nmax 50010
#define inf 1000000
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
//FILE *fin=fopen("dijkstra.in", "r");
//FILE *fout=fopen("dijkstra.out", "w");
int d[nmax]; //distantele
int uz[nmax]; //blocat 0 sau 1
int n, m, x, y, c;
vector< pair <int, int> > v[nmax];
vector< pair <int, int> >::iterator it, sf;
class cmp
{
public:
inline bool operator()(const int &x, const int &y) { return d[x]>d[y]; }
};
priority_queue< int, vector<int>, cmp > h; //heapul
int main()
{
int i;
fin >> n >> m;
for(i=1; i<=m; ++i)
{
fin >> x >> y >> c;
v[x].push_back(make_pair(y,c));
}
fill(d+2,d+n+1,inf);
h.push(1);
for(; !h.empty(); h.pop())
{
x = h.top();
uz[x]=0;
for(it=v[x].begin(), sf=v[x].end(); it<sf; ++it)
{
y=it->first;
c=it->second;
if(d[y]>d[x]+c)
{
d[y]=d[x]+c;
if(!uz[y])
{
h.push(y);
uz[y]=1;
}
}
}
}
for(i=2; i<=n; ++i)
if(d[i]!=inf)
fout << d[i] << " ";
else
fout << 0 << " ";
}