Pagini recente » Cod sursa (job #2187617) | Cod sursa (job #1921937) | Cod sursa (job #1594672) | Cod sursa (job #1387970) | Cod sursa (job #3224749)
#include <fstream>
#include <climits>
#include <vector>
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
const long long nmax = 50005;
const long long inf = LONG_MAX;
long long fr[nmax], n, m, cost[nmax];
vector< pair<long long, long long> > a[nmax];
void Dij(long long nod)
{
fr[nod] = 1;
while(a[nod].size())
{
pair<long long, long long> k = a[nod].back();
a[nod].pop_back();
cost[k.first] = min(cost[k.first], k.second + cost[nod]);
if(!fr[k.first])
Dij(k.first);
}
}
int main()
{
f >> n >> m;
for(long long i = 1; i <= n; i ++)
{
long long x, y, c;
f >> x >> y >> c;
a[x].push_back({y, c});
}
for(long long i = 2; i <= n; i ++)
cost[i] = inf;
Dij(1);
for(long long i = 2; i <= n; i ++)
if(cost[i] == inf)
g << 0 << " ";
else
g << cost[i] << " ";
return 0;
}