Pagini recente » Cod sursa (job #1217816) | Cod sursa (job #2896398) | Cod sursa (job #1752855) | Cod sursa (job #1137208) | Cod sursa (job #2377191)
#include <cstdio>
#include <set>
#include <vector>
#include <bitset>
#define NMAX 50005
#define INF 1<<30
using namespace std;
struct muche
{
int catre;
int cost;
bool const operator<(const muche A)const
{
if(cost!=A.cost)
return cost<A.cost;
return catre<A.catre;
}
};
vector<muche> G[NMAX];
int dist[NMAX];
int n,m;
void citire()
{
scanf("%d %d\n",&n,&m);
for(int i=1;i<=n;i++)
{
int a,b,c;
scanf("%d %d %d\n",&a,&b,&c);
muche aux;
aux.catre=b;
aux.cost=c;
G[a].push_back(aux);
}
}
set<muche> Q;
int Dijkstra()
{
muche aux;
aux.catre=1;
aux.cost=0;
Q.insert(aux);
while(!Q.empty())
{
int nod=(*Q.begin()).catre;
Q.erase(*Q.begin());
for(auto it:G[nod])
{
int catre=it.catre;
int cost=it.cost;
if( dist[catre] > dist[nod] + cost)
{
if(dist[catre]!=INF)
{
muche aux;
aux.catre=catre;
aux.cost=dist[catre];
Q.erase(*Q.find(aux));
}
dist[catre]=dist[nod]+cost;
muche aux;
aux.catre=catre;
aux.cost=dist[catre];
Q.insert(aux);
}
}
}
for(int i=2;i<=n;i++)
{
if(dist[i]==INF)
dist[i]=0;
printf("%d ",dist[i]);
}
}
int main()
{
freopen("dijkstra.in","r",stdin);
freopen("dijkstra.out","w",stdout);
citire();
for(int i=1;i<=n;i++)
dist[i]=INF;
dist[1]=0;
Dijkstra();
return 0;
}