Cod sursa(job #679210)

Utilizator alexapoApostol Alexandru Ionut alexapo Data 12 februarie 2012 21:37:12
Problema Ubuntzei Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.76 kb
#include <fstream>
using namespace std;
ifstream f("ubuntzei.in");
ofstream g("ubuntzei.out");
unsigned m,n,a,b,c,i,negr[2005],alb[2005],loc[2005],k;
struct nod{
	unsigned x,cost;
	nod *urm;
}*v[50005];
nod *p;
int main()
{
	f>>n>>m;
	f>>k;
	for(i=1;i<=k;i++)
		f>>loc[i];
	for(i=1;i<=m;i++)
	{
		p=new nod;
		f>>a>>b>>c;
		p->x=b;
		p->cost=c;
		p->urm=v[a];
		v[a]=p;
	}
	bool ok=true;
	while (ok)
	{
		ok=false;
		for(i=1;i<=n;i++)
			if((i==1||(alb[i]))&&!negr[i])
			{
				negr[i]=1;
				p=v[i];
				
				while(p)
				{
					if((alb[p->x]==0&&((p->x)!=1))||alb[p->x]>(p->cost+alb[i]))
						alb[p->x]=alb[i]+p->cost,negr[p->x]=0;
					p=p->urm;
				}
				ok=true;
			}
	}
	g<<alb[n]<<'\n';
	
	f.close();
	g.close();
	return 0;
}