Pagini recente » Cod sursa (job #3170192) | E | Cod sursa (job #1610144) | Cod sursa (job #796056) | Cod sursa (job #693735)
Cod sursa(job #693735)
#include <fstream>
#include <queue>
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int n,m,k,a,b,c,cost[50005],min;
char prez[50005];
struct nod
{
int nr;
int c;
nod *urm;
} *v[50005],*p;
struct Comp
{
bool operator() (int i, int j)
{
return cost[i]<cost[j];
}
};
priority_queue<int, vector<int>, Comp> q;
void adaug(int a, int b, int c)
{
nod *p;
p=new nod;
p->nr=b;
p->c=c;
p->urm=v[a];
v[a]=p;
}
void afis()
{
int i;
for(i=2;i<=n;i++) g<<cost[i]<<' ';
g<<'\n';
}
void dijkstra(int i)
{
int x;
prez[i]=1;
q.push(i);
while(!q.empty())
{
x=q.top();
q.pop();
p=v[x];
prez[x]=0;
while(p)
{
if(!cost[p->nr]||cost[p->nr]>cost[x]+p->c)
{
cost[p->nr]=cost[x]+p->c;
if(!prez[p->nr]) prez[p->nr]=1,q.push(p->nr);
}
p=p->urm;
}
}
afis();
}
int main()
{
int i;
f>>n>>m;
for(i=1;i<=m;i++)
{
f>>a>>b>>c;
adaug(a,b,c);
}
dijkstra(1);
f.close(); g.close();
return 0;
}