Pagini recente » Cod sursa (job #1423450) | Cod sursa (job #2932650) | Cod sursa (job #2185438) | Cod sursa (job #394298) | Cod sursa (job #520797)
Cod sursa(job #520797)
#include <fstream>
#include <queue>
#define nmax 50001
#define oo 0x3f3f3f
using namespace std;
queue <int> Q;
int n,m;
bool viz[nmax];
struct nod{
int inf,cost;
nod *urm;
} *g[nmax];
int dist[nmax];
void add(int x,int y,int z)
{
nod *aux = new nod;
aux->inf = y;
aux->cost = z;
aux->urm = g[x];
g[x] = aux;
}
int main()
{
ifstream f("dijkstra.in");
ofstream gg("dijkstra.out");
f>>n>>m;
int x,y,z;
for(int i=1;i<=m;++i)
{
f>>x>>y>>z;
add(x,y,z);
}
Q.push(1);
viz[1] = true;
memset(dist,oo,sizeof(dist));
dist[1] = 0;
while( !Q.empty()){
int nd = Q.front();
Q.pop();
viz[nd]=false;
for(nod *p=g[nd];p;p=p->urm)
{
if(dist[nd] + p->cost < dist[p->inf])
{
dist[p->inf] = p->cost + dist[nd];
if(!viz[p->inf])
{
Q.push(p->inf);
viz[p->inf]=true;
}
}
}
}
for(int i=2;i<=n;++i)
if(dist[i] == oo)
gg<<0<<" ";
else
gg<<dist[i]<< " ";
return 0;
}