Cod sursa(job #272166)

Utilizator andrabAndra B andrab Data 6 martie 2009 15:22:46
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.71 kb
#include<fstream.h>
#define max 50000
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");

long n,m,d[max];
struct nod{int x,y,c;
	   };
 nod g[max];


void bellman()
{int i,j;
 for(i=1;i<n;i++)
 for(j=1;j<=m;j++)
 if(d[g[j].y]>d[g[j].x]+g[j].c)
 d[g[j].y]=d[g[j].x]+g[j].c;
}

void init()
{int i,j;
 fin>>n>>m;
 for(i=1;i<=m;i++)
 fin>>g[i].x>>g[i].y>>g[i].c;

 for(i=1;i<=n;i++)
 d[i]=max;
 }

int main()
{int i;
 init();
 d[1]=0;
 bellman();
 for(i=1;i<=m;i++)
 if(d[g[i].x]+g[i].c<d[g[i].y])
 {fout<<"gr contine cicluri de cost negativ";
 fout<<"\n";
  }
 for(i=1;i<=n;i++)
 if(d[i]<max && d[i]) fout<<d[i]<<" ";
 fout<<"\n";

 fin.close();
 fout.close();
 return 0;
 }