Pagini recente » Cod sursa (job #414286) | Cod sursa (job #2320750) | Cod sursa (job #998245) | Cod sursa (job #1031210) | Cod sursa (job #1413774)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
const int maxn = 50010;
const int inf = 2e9;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n, m, d[maxn];
vector <pair <int, int> > g[maxn];
queue <int> q;
bool inq[maxn];
void dijkstra()
{
for (int i = 2; i <= n; i++)
d[i] = inf;
q.push(1);
while (!q.empty())
{
int node = q.front();
q.pop();
inq[node] = 0;
for (vector <pair <int, int> > :: iterator it = g[node].begin(); it != g[node].end(); ++it)
{
int v = it -> first;
int c = it -> second;
if (d[v] > d[node] + c)
{
d[v] = d[node] + c;
if (!inq[v])
{
inq[v] = 1;
q.push(v);
}
}
}
}
for (int i = 2; i <= n; i++)
{
if (d[i] != inf) fout << d[i] << " ";
else fout << "-1 ";
}
}
int main()
{
fin >> n >> m;
for (int a, b, c; m; --m)
{
fin >> a >> b >> c;
g[a].push_back(make_pair(b, c));
}
dijkstra();
return 0;
}