Cod sursa(job #703131)

Utilizator giuliastefGiulia Stef giuliastef Data 2 martie 2012 11:06:06
Problema Ubuntzei Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.07 kb
#include <cstdio>
#include <vector>
#include <cstring>
#define NMAX 2011
#define DMAX 20
#define CMAX 262150
#define INF 1<<30
using namespace std;
vector <pair <int,int> > v[NMAX];
vector <int> g[DMAX];

int k,S,D,l,n,h[NMAX],d[NMAX],pos[NMAX],t[NMAX],cost[DMAX][DMAX],c[CMAX][DMAX];

void upheap(int poz)
{
	int aux;
	while(poz>=1 && d[h[poz]]<d[h[poz/2]])
	{
		aux=pos[h[poz]];
		pos[h[poz]]=pos[h[poz/2]];
		pos[h[poz/2]]=aux;
		
		aux=h[poz];
		h[poz]=h[poz/2];
		h[poz/2]=aux;
		poz=poz/2;
	}
}
void downheap(int poz)
{
	int aux;
	while(poz*2+1<=l && (d[h[poz]]>d[h[poz*2]] || d[h[poz]]>d[h[poz*2+1]]))
	{
		if(poz*2+1<=l && d[h[poz*2]]>d[h[poz*2+1]])
		{
			aux=pos[h[poz]];
			pos[h[poz]]=pos[h[poz*2+1]];
			pos[h[poz*2+1]]=aux;
			
			aux=h[poz];
			h[poz]=h[poz*2+1];
			h[poz*2+1]=aux;
			poz=poz*2+1;
		}
		else
		{
			aux=pos[h[poz]];
			pos[h[poz]]=pos[h[poz*2]];
			pos[h[poz*2]]=aux;
			
			aux=h[poz];
			h[poz]=h[poz*2];
			h[poz*2]=aux;
			poz=poz*2;
		}
	}
}
void dijkstra(int S){
    int i,nod,costt,min,l;
    memset(h,0,sizeof(h));
    memset(pos,-1,sizeof(pos));
    for(i=1;i<=n;i++)
     d[i]=INF;
    d[S]=0;
	l=1;
	h[l]=S;
	pos[h[1]]=1;
	while(l)
	{
		min=h[1];
		h[1]=h[l];
		pos[h[1]]=1;
		l--;
		downheap(1);
		for(i=0;i<v[min].size();i++)
		{
		  nod=v[min][i].first;
		  costt=v[min][i].second;
		  if(d[nod]>d[min]+costt)
		  {
	 		d[nod]=d[min]+costt;
			t[nod]=min;
 			if(pos[nod]==-1)
 			{
			 l++;
			 h[l]=nod;
			 pos[h[l]]=l;
			 upheap(pos[nod]);
			}
			else
				upheap(pos[nod]);
		  }
		}
	}
}
int calc(int conf, int last){
    if(c[conf][last]==-1){
     c[conf][last]=INF;
     for(vector<int>::iterator it=g[last].begin();it!=g[last].end();it++)
      if(conf & (1<<*it)){
       if(*it==D && conf!=(1<<last)+1) continue;
       c[conf][last]=min(c[conf][last], calc(conf ^ (1<<last),*it) + cost[*it][last]);
     }
    }
    return c[conf][last];
}
int ciclu(){
    int sol,i;
    sol=INF;
    memset(c, -1, sizeof(c));
    c[1][0]=0;
    for(vector<int>::iterator it=g[D].begin();it!=g[D].end(); it++)
     sol=min(sol, calc((1<<(k+2))-1,*it)+cost[*it][D]);
    return sol;
}
int main ()
{
	int i,j,nod,min,m,u[20],x,y,z;
	freopen("ubuntzei.in","r",stdin);
	freopen("ubuntzei.out","w",stdout);
	scanf("%d%d%d",&n,&m,&k);
	for(i=1;i<=k && i<=15;i++)
		scanf("%d",&u[i]);
	for(i=1;i<=m;i++)
	{
		scanf("%d%d%d",&x,&y,&z);
		v[x].push_back(make_pair(y,z));
		v[y].push_back(make_pair(x,z));
	}
	dijkstra(1);
	if(k)
	{
     S=k+1;
     D=0;              
     for(i=0;i<=k+1;i++)
      for(j=0;j<=k+1;j++)
        cost[i][j]=INF;
	 for(i=1;i<=k;i++){
	  g[i].push_back(S);
	  cost[S][i]=d[u[i]];
     }
     g[S].push_back(D);
     cost[D][S]=0;
     for(i=1;i<=k;i++)
     {
      dijkstra(u[i]);
      for(j=1;j<=k;j++)
      if(j!=i)
      {
       g[j].push_back(i);
       cost[i][j]=d[u[j]];
      }
      g[D].push_back(i);
      cost[i][D]=d[n];
     }
     printf("%d\n",ciclu());
	}
	else printf("%d",d[n]);
	return 0;
}