Pagini recente » Cod sursa (job #396291) | Cod sursa (job #914132) | Cod sursa (job #2217133) | Cod sursa (job #1753494) | Cod sursa (job #1771080)
#include <fstream>
#include <vector>
#include <queue>
#define VAL 50005
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int N, M, i, j;
int a, b, c;
int cost[VAL];
vector<int> v[VAL];
vector<int> p[VAL];
queue<int> q;
void bfs()
{
q.push(1);
while (q.empty()==false)
{
a=q.front();
q.pop();
for (i=0; i<v[a].size(); i++)
{
if (cost[v[a][i]]==-1)
{
cost[v[a][i]]=cost[a]+p[a][i];
q.push(v[a][i]);
}
else
{
if (cost[v[a][i]]>cost[a]+p[a][i])
{
cost[v[a][i]]=cost[a]+p[a][i];
q.push(v[a][i]);
}
}
}
}
}
int main()
{
fin >> N >> M;
for (i=1; i<=M; i++)
{
fin >> a >> b >> c;
if (b!=1)
{
v[a].push_back(b);
p[a].push_back(c);
}
}
for (i=2; i<=N; i++)
cost[i]=-1;
bfs();
for (i=2; i<=N; i++)
{
if (cost[i]==-1)
cost[i]=0;
fout << cost[i] << " ";
}
fin.close();
fout.close();
return 0;
}