Pagini recente » Cod sursa (job #1794164) | Cod sursa (job #2619645) | Cod sursa (job #2464557) | Cod sursa (job #2133965) | Cod sursa (job #505020)
Cod sursa(job #505020)
#include <cstdio>
#include <fstream>
#include <queue>
#include <vector>
#define nmax 50010
#define inf 1000000
//ifstream fin("dijkstra.in");
//ofstream fout("dijkstra.out");
using namespace std;
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;
fscanf(fin, "%d%d", &n, &m);
for(i=1; i<=m; ++i)
{
fscanf(fin, "%d%d%d", &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)
fprintf(fout, "%d ", d[i]);
else
fprintf(fout, "0 ");
}