Pagini recente » Cod sursa (job #2583975) | Borderou de evaluare (job #520080) | Cod sursa (job #257783) | Cod sursa (job #1189308) | Cod sursa (job #793300)
Cod sursa(job #793300)
#include<cstdio>
#include<cassert>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
const int kmax = 1000000005;
int verts, edges, source, cost[50005], taken[50005];
vector<pair<int, int> > graph[50005];
void read(){
assert(freopen("dijkstra.in", "r", stdin));
scanf("%d%d", &verts, &edges);
source = 1;
for(int i = 0; i < edges; ++i){
int x, y, c;
scanf("%d%d%d", &x, &y, &c);
graph[x].push_back(make_pair(y, c));
graph[y].push_back(make_pair(x, c));
}
for(int i = 2; i <= verts; ++i)
cost[i] = kmax;
}
class comp{
public:
bool operator() (int x, int y){
if(cost[x] > cost[y])
return 0;
return 1;
}
};
void solve(){
priority_queue<int, vector<int>, comp> h;
h.push(source);
while(!h.empty()){
int current = h.top();
h.pop();
if(taken[current])
;//NOPE
else{
taken[current] = 1;
for(int i = 0; i < graph[current].size(); ++i)
if(cost[current] + graph[current][i].second < cost[graph[current][i].first]){
h.push(graph[current][i].first);
cost[graph[current][i].first] = cost[current] + graph[current][i].second;
}
}
}
}
void write(){
assert(freopen("dijkstra.out", "w", stdout));
for(int i = 2; i <= verts; ++i)
if(cost[i] >= kmax)
printf("0 ");
else
printf("%d ", cost[i]);
}
int main(){
read();
solve();
write();
return 0;
}