Pagini recente » Cod sursa (job #1527202) | Cod sursa (job #1913326) | Cod sursa (job #166333) | Cod sursa (job #2822073) | Cod sursa (job #1114111)
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
#include <string.h>
using namespace std;
#define _ 50005
const int INF=0x3f3f33f;
int N,M;
vector< pair<int,int > > G[_];
vector< pair<int,int > >::iterator it;
bool inQueue[_];
queue<int> Q;
int DMIN[_];
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int main()
{
f>>N>>M;
++M;
int a,b,c;
while( --M ){
f>>a>>b>>c;
G[a].push_back( make_pair(b,c) );
}
f.close();
Q.push(1);
memset( DMIN, INF, sizeof(DMIN) );
inQueue[1]=true;
DMIN[1]=0;
for( ; !Q.empty(); Q.pop()){
int nod=Q.front();
inQueue[nod]=false;
for(it=G[nod].begin(); it!=G[nod].end() ; ++it ){
if( DMIN[nod] + it->second < DMIN[it->first] ){
DMIN[it->first]=DMIN[nod] + it->second;
if( !inQueue[it->first] ){
Q.push(it->first);
inQueue[it->first] = true;
}
}
}
}
for(int i=2;i<=N;i++) g<<(DMIN[i] < INF ? DMIN[i] : 0) << " ";
g.close();
return 0;
}