Pagini recente » Cod sursa (job #387189) | Cod sursa (job #196647) | Cod sursa (job #3285597) | Cod sursa (job #2805643) | Cod sursa (job #325648)
Cod sursa(job #325648)
#include <stdio.h>
#include <vector>
#define abs(x) ((x>0)?(x):(-(x)))
using namespace std;
#define NMAX 50001
vector<int> u,v,c;
long d[NMAX],m,n,inf;
int C[2*NMAX],k;
FILE *f=fopen("dijkstra.in","r");
FILE *g=fopen("dijkstra.out","w");
void read()
{
long a,b,cs;
fscanf(f,"%ld %ld",&n,&m);
for(int i=1;i<=m;i++)
{
fscanf(f,"%d %d %d",&a,&b,&cs);
inf+=abs(cs);
u.push_back(a);
v.push_back(b);
c.push_back(cs);
}
}
void init()
{
d[1]=0;
for(int i=2;i<=n;i++)
{
d[i]=inf;
}
}
void solve()
{
init();
C[++k]=1;
for(int i=1;i<=k;i++)
for(int j=0;j<u.size();j++)
{
if(u[j]==C[i]&&(d[C[i]]+c[j]<d[v[j]]))
{
d[v[j]]=d[C[i]]+c[j];
C[++k]=v[j];
}
}
}
void show()
{
for(int i=2;i<=n;i++)
fprintf(g,"%ld ",d[i]);
}
int main()
{
read();
solve();
show();
return 0;
}