Pagini recente » Arhiva de probleme | Istoria paginii runda/your_11th_nightmare | Arhiva de probleme | Arhiva de probleme | Cod sursa (job #1088047)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
struct point {
vector<int> a, b;
};
vector<int> d;
vector<point> g;
int t=1;
struct compare {
bool operator()(const int &t1, const int &t2) const
{
return d[t1]<d[t2]?0:1;
}
};
priority_queue<int, vector<int>, compare> index;
void dj(int i) {
d[i] = 0;
while(!index.empty()) {
int current = index.top();
for(int k=0;k<(int)g[current].a.size();k++) {
if(d[g[current].a[k]]>d[current] + g[current].b[k]) {
d[g[current].a[k]] = d[current] + g[current].b[k];
//index.push(g[current].a[k]);
}
}
index.pop();
}
}
int main()
{
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
int n, m;
in>>n>>m;
g.resize(n+1);
d.resize(n+1, 1001);
d[1]=0;
while(m-->0) {
int x, y, z;
in>>x>>y>>z;
g[x].a.push_back(y);
g[y].a.push_back(x);
g[x].b.push_back(z);
g[y].b.push_back(z);
}
in.close();
for(int k=1;k<(int)d.size();k++) index.push(k);
dj(1);
for(int k=2;k<(int)d.size();k++) {
if(d[k]==1001) out<<0; else out<<d[k];
out<<" ";
}
out.close();
return 0;
}