Cod sursa(job #968520)

Utilizator Kira96Denis Mita Kira96 Data 2 iulie 2013 11:23:39
Problema Ubuntzei Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.98 kb
#include<fstream>
#define INF 999999999
#define NM 2010
#define KM 20
#define exp 132000
#include<vector>
using namespace std;
ifstream f("ubuntzei.in");
ofstream g("ubuntzei.out");
struct el{int des,cos;};
int n,m,k,i,c[KM],x,t,dp[KM][exp],D[KM][NM],H[NM],P[NM],L,nod,dest,cost;
vector<el> v[NM];
el a;
int calc(int x,int s);
void Dij();
void urca(int po);
void coboara(int po);
int main ()
{
	f>>n>>m>>k;
	for(i=1;i<=k;++i)
		f>>c[i];
	for(t=1;t<=m;++t)
	{
		f>>x>>a.des>>a.cos;
		v[x].push_back(a);
		swap(x,a.des);
		v[x].push_back(a);
	}
	c[0]=1;
	c[k+1]=n;
	x=0;
	Dij();
	if(!k)
	{
		g<<D[0][n];
		return 0;
	}
	for(t=1;t<=k;++t)
	{
		x=t;
		Dij();
	}
	x=t;
	Dij();
	t=(1<<k);
	t--;
	g<<calc(k+1,t);
	return 0;
}
int calc(int x,int s)
{
	int aux,var;
	if(dp[x][s])
		return dp[x][s];
	if(s==0)
		return D[0][c[x]];
	dp[x][s]=INF;
	var=1;
	for(int i=1;i<=k;++i)
	{
		if(((s&var)==var)&&(i!=x))
		{
			aux=calc(i,s^var)+D[x][c[i]];
			if(aux<dp[x][s])
				dp[x][s]=aux;
		}
		var<<=1;
	}
	return dp[x][s];
}
void urca(int po)
{
    while(po&&D[x][H[po]]<D[x][H[po>>1]])
    {
        swap(H[po],H[po>>1]);
        swap(P[H[po]],P[H[po>>1]]);
        po>>=1;
    }
}
void coboara(int po){
    int y=0;
    while(po!=y){
        y=po;
        if((y<<1)<=L&&D[x][H[y<<1]]<D[x][H[po]])
            po=y<<1;
        if((y<<1)+1<=L&&D[x][H[(y<<1)+1]]<D[x][H[po]])
            po=(y<<1)+1;
        swap(H[po],H[y]);
        swap(P[H[po]],P[H[y]]);
    }
}
void Dij()
{
	for(i=1;i<=n;++i)
	{
		D[x][i]=INF;
		P[i]=0;
		H[i]=0;
	}
	D[x][c[x]]=0;
	H[++L]=c[x];
	P[c[x]]=1;
	while(L)
	{
		nod=H[1];
		H[1]=H[L--];
		P[H[1]]=1;
		coboara(1);
		for(i=0;i<v[nod].size();++i)
		{
			dest=v[nod][i].des;
			cost=v[nod][i].cos;
			if(D[x][dest]>D[x][nod]+cost)
			{
				D[x][dest]=D[x][nod]+cost;
				if(!P[dest])
				{
					H[++L]=dest;
					P[dest]=L;
					urca(L);
				}
				else
					urca(P[dest]);
			}
		}
	}
}