Cod sursa(job #681626)

Utilizator Ast09Stoica Anca Ast09 Data 17 februarie 2012 16:04:26
Problema Ubuntzei Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.84 kb
#include<fstream>
using namespace std;

ifstream f("ubuntzei.in");
ofstream g("ubuntzei.out");

int m,n,a,b,cost[2005],viz[2005],x,c,A[2005][2005],j,k,h[2005];

int chestie( int p )
{
	int minim,i;
	minim=0;
	p=0;
	for(i=2;i<=n;i++)
	{
		if(viz[i]==0 && cost[i]&&(minim>=cost[i]||minim==0)) minim=cost[i],p=i;
	}
	return p;
}

void prel(int y)
{
	viz[y]=1;
	for(int i=1;i<=n;i++)
	{
		if(viz[i]==0&&A[y][i]!=0)
			if(cost[y]+A[y][i]<cost[i]||cost[i]==0) cost[i]=cost[y]+A[y][i];
	}
	
}

int main()
{
	f>>n>>m;
	viz[1]=1;
	
	f>>k;
	for(j=0;j<k;j++) f>>h[j];
	
	for(j=1;j<=m;j++)
	{
		f>>a>>b>>c;
		if(a==1) cost[b]=c;
		//else if(b==1) cost[a]=c;
		A[a][b]=c;
	}
	
	x=chestie(1);
	while(x!=0)
	{
		//g<<x<<'*';
		prel(x);
		x=chestie(1);
	}
	
	g<<cost[n]<<' ';
	
	f.close();
	g.close();
	return 0;
}