Mai intai trebuie sa te autentifici.
Cod sursa(job #217487)
| Utilizator | Data | 28 octombrie 2008 19:20:17 | |
|---|---|---|---|
| Problema | Algoritmul lui Dijkstra | Scor | 0 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.82 kb |
#include<stdio.h>
#include<vector>
#define nmax 50000
#define mmax 250000
using namespace std;
vector < int > v[nmax];
vector < long long > cost[nmax];
long long w[nmax];
int dimensiune[nmax];
void dijkstra(int x)
{
for(int i=0;i<dimensiune[x];i++)
if(w[x]+cost[x][i]<w[ v[x][i] ] )
{
w[ v[x][i] ]=w[x]+cost[x][i];
dijkstra(v[x][i]);
}
}
int main()
{
int x,y,c,i,n,m;
freopen("dijkstra.in","r",stdin);
freopen("dijkstra.out","w",stdout);
scanf("%d %d",&n,&m);
for(i=1;i<=m;i++)
{
scanf("%d %d %d",&x,&y,&c);
v[x].push_back(y);
dimensiune[x]++;
cost[x].push_back(c);
v[y].push_back(x);
dimensiune[y]++;
cost[y].push_back(c);
}
for(i=2;i<=n;i++)
w[i]=mmax;
dijkstra(1);
for(i=2;i<=n;i++)
printf("%lld ",w[i]);
return 0;
}
