Pagini recente » Cod sursa (job #720063) | Cod sursa (job #3000910) | Cod sursa (job #397781) | Cod sursa (job #350935) | Cod sursa (job #1752419)
#include <fstream>
#include <vector>
#include <climits>
#include <algorithm>
#include <map>
#define NMAX 50005
#define INF INT_MAX
using namespace std;
int n,m,d[NMAX];
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
map<int,int> coada; //vom folosi un map sub forma unei cozi de prioritati
vector< pair <int,int> > G[NMAX];
void citire(){
f>>n>>m;
int i,x,y,z;
for(i=1;i<=m;i++){
f>>x>>y>>z;
G[x].push_back(make_pair(y,z));
}
}
void dijkstra(){
vector< pair <int,int> >::iterator it;
d[1]=0;
int i,j;
for(it=G[1].begin();it!=G[1].end();it++){
d[it->first]=it->second;
coada.insert(pair<int,int>(it->second,it->first));
}
for(i=2;i<=n;i++)
if(d[i]==0)
d[i]=INF;
map<int,int>::iterator parcurg=coada.begin();
while(!coada.empty()){
int x=coada.begin()->second;
coada.erase(coada.begin());
for(it=G[x].begin();it!=G[x].end();it++){
if(d[it->first]>d[x]+it->second){
if(d[it->first]==INF)
coada.insert(pair<int,int>(d[x]+it->second,it->first));
d[it->first]=d[x]+it->second;
}
}
}
for(i=2;i<=n;i++)
g<<d[i]<<' ';
}
int main(){
citire();
dijkstra();
return 0;
}