Pagini recente » Cod sursa (job #1552643) | Cod sursa (job #2415214) | Cod sursa (job #490084) | Cod sursa (job #1091379) | Cod sursa (job #2048545)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
const int N = 50005;
const int NMAX = 999999999;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
struct muchie
{
int d;
int nod;
};
class cmp{
public:
bool operator()(muchie a,muchie b)
{
if(a.d>b.d)
return 1;
else
return 0;
}
};
priority_queue<muchie , vector<muchie>,cmp>h;
int n;
int dist[N];
vector <muchie> V[N];
void rezolva()
{
dist[1] = 0;
muchie muc;
muc.d = 0;
muc.nod = 1;
h.push(muc);
muchie x;
while(!h.empty())
{
x = h.top();
h.pop();
muchie y;
if(dist[x.nod] < x.d)
continue;
for(int i = 0; i< V[x.nod].size(); i++)
{
y = V[x.nod][i];
if(dist[y.nod] > dist[x.nod] + y.d)
{
dist[y.nod] = dist[x.nod] + V[x.nod][i].d;
y.d = dist[y.nod];
h.push(y);
}
}
}
}
int main()
{
int m;
f>>n>>m;
for(int i =2 ; i<= n; i++)
dist[i] = NMAX;
for(int i =1 ; i<= m; i++)
{
int ds;
muchie a,b;
f>>a.nod>>b.nod>>ds;
b.d = ds;
V[a.nod].push_back(b);
}
rezolva();
for(int i = 2; i<= n; i++)
{
if(dist[i]==NMAX)
{
g<<0<<" ";
}
else
{
g<<dist[i]<<" ";
}
}
return 0;
}