Pagini recente » Cod sursa (job #2129098) | Istoria paginii utilizator/butnarugiulia | Cod sursa (job #1034174) | Cod sursa (job #1136080) | Cod sursa (job #222787)
Cod sursa(job #222787)
#include <cstdio>
#include <cstdlib>
#include <set>
#include <vector>
#include <algorithm>
#define oo 1<<30
#define N 100000
using namespace std;
set <pair <int,int> > S;
vector<int> cine[N],cost[N];
int dist[N];
int main(){
int i,a,b,c,n,m;
pair<int,int> now;
freopen("dijkstra.in","r",stdin);
freopen("dijkstra.out","w",stdout);
scanf("%d%d",&n,&m);
while (m--){
scanf("%d%d%d",&a,&b,&c);
cine[a].push_back(b);
cost[a].push_back(c);
}
for (i = 2; i <= n; ++i)
dist[i] = oo;
S.insert(make_pair(0,1));
while (!S.empty()){
now = *(S.begin());
S.erase(S.begin());
for (i = 0; i < cine[now.second].size(); ++i){
if (dist[cine[now.second][i]] > dist[now.second] + cost[now.second][i]){
dist[cine[now.second][i]] = dist[now.second] + cost[now.second][i];
S.insert(make_pair(now.first + cost[now.second][i], cine[now.second][i]));
}
}
}
for (i = 2; i <= n; ++i)
printf("%d ",dist[i]==oo?0:dist[i]);
}