Pagini recente » Cod sursa (job #2085565) | Cod sursa (job #1128838) | Cod sursa (job #611667) | Cod sursa (job #1222241) | Cod sursa (job #1923315)
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
class Compare
{
public:
bool operator () (pair<int,int> &x,pair<int,int> &y)
{
return x.second<y.second;
}
};
vector < pair <int,int > > v[50001];
int dist[50001];
bool viz[50001];
priority_queue < pair<int,int> ,vector< pair <int,int> >, Compare > H;
void Dijkstra (int S)
{
dist[S]=0;
H.push(make_pair(S,0));
int j;
while (!H.empty())
{
pair<int,int> k;
k=H.top();
H.pop();
if (viz[k.first]==0)
{
for (j=0; j<v[k.first].size(); j++)
{
if (dist[k.first]+v[k.first][j].second<dist[v[k.first][j].first])
{
dist[v[k.first][j].first]=dist[k.first]+v[k.first][j].second;
H.push(make_pair(v[k.first][j].first,dist[v[k.first][j].first]));
}
}
viz[k.first]=1;
}
}
}
int main()
{
freopen ("dijkstra.in","r",stdin);
freopen ("dijkstra.out","w",stdout);
int n,m,i;
int A,B,C;
scanf ("%d %d",&n,&m);
for (i=1; i<=n; i++)
{
dist[i]=2000000000;
}
for (i=1; i<=m; i++)
{
scanf ("%d %d %d",&A,&B,&C);
v[A].push_back(make_pair(B,C));
}
Dijkstra(1);
for (i=2; i<=n; i++)
{
if (dist[i]==2000000000)
{
printf ("0 ");
}
else
{
printf ("%d ",dist[i]);
}
}
printf ("\n");
return 0;
}