Pagini recente » Cod sursa (job #1190230) | Cod sursa (job #2321924) | Cod sursa (job #791664) | Cod sursa (job #539192) | Cod sursa (job #2302836)
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream fin( "dijkstra.in" );
ofstream fout( "dijkstra.out" );
const int NMAX = 102;
const int INF = 0x3f3f3f;
int N, M;
int source;
int d[NMAX];
vector <int> Ad[NMAX];
vector <int> Cost[NMAX];
void Read()
{
fin >> N >> source;
int a, b, d;
while( fin >> a >> b >> d )
{
++M;
Ad[a].push_back( b );
Cost[a].push_back( d );
// Ad[b].push_back( a );
// Cost[b].push_back( d );
}
fin.close();
}
void Do()
{
for( int i = 1; i <= N; ++i )
d[i] = INF;
d[source] = 0;
priority_queue < pair <int, int> > Heap;
Heap.push( make_pair( 0, source ) );
int nod;
int w;
while( ! Heap.empty() )
{
nod = Heap.top().second;
Heap.pop();
for( int i = 0; i < Ad[nod].size(); ++i )
{
w = Ad[nod][i];
if( d[w] > d[nod] + Cost[nod][i] )
{
d[w] = d[nod] + Cost[nod][i];
Heap.push( make_pair( d[w], w ) );
}
}
}
}
void Print()
{
for( int i = 1; i <= N; ++i )
( d[i] == INF ) ? fout << "-1 " : fout << d[i] << ' ';
fout << '\n';
fout.close();
}
int main()
{
Read();
Do();
Print();
return 0;
}