Pagini recente » Cod sursa (job #1016030) | Cod sursa (job #2598520) | Cod sursa (job #919474) | Cod sursa (job #3251027) | Cod sursa (job #614923)
Cod sursa(job #614923)
#include<cstdio>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
const int maxN=1<<16;
int n,m,lg[maxN];
bool proc[maxN];
vector<pair<int,int> > G[maxN];
priority_queue<pair<int,int> > H;
void read()
{
freopen("dijkstra.in","r",stdin);
freopen("dijkstra.out","w",stdout);
scanf("%d%d",&n,&m);
int x,y,c;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&c);
G[x].push_back(make_pair(c,y));
}
H.push(make_pair(0,1));
}
void work()
{
while(!H.empty())
{
pair<int,int> nod;
nod=H.top();
H.pop();
if(!proc[nod.second])
{
lg[nod.second]=nod.first;
proc[nod.second]=true;
for(vector<pair<int,int> >::iterator it=G[nod.second].begin();it!=G[nod.second].end();it++)
if(!proc[it->second])
H.push(make_pair(nod.first-it->first,it->second));
}
}
for(int i=2;i<=n;i++)
printf("%d ",-lg[i]);
}
int main()
{
read();
work();
return 0;
}