Cod sursa(job #546412)

Utilizator DraStiKDragos Oprica DraStiK Data 4 martie 2011 21:40:29
Problema Team Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.9 kb
//#include <algorithm>
//#include <fstream>
//#include <vector>
//#include <bitset>
//#include <queue>
//using namespace std;
//
//#define pb push_back
//#define mp make_pair
//#define sc second
//#define fs first
//
//#define INF 0x3f3f3f3f
//#define DIM 505
//#define MAX 55
//
//ifstream fin ("team.in");
//ofstream fout ("team.out");
//
//vector <pair <int,int> > g[DIM];
//int bst[MAX][MAX][MAX];
//int D[MAX],dst[DIM];
//int cst[MAX][MAX];
//bitset <DIM> viz;
//int N,M,P;
//
//struct cmp
//{
//    bool operator () (const int &a,const int &b)
//    {
//        return D[a]>D[b];
//    }
//}; priority_queue <int,vector <int>,cmp> q;
//
//inline void dijkstra (int start)
//{
//    vector <pair <int,int> > :: iterator it;
//    int nod;
//
//    memset (dst,INF,sizeof (dst));
//    dst[start]=0;
//
//    for (q.push (start); !q.empty (); )
//    {
//        nod=q.top (); q.pop (); viz[nod]=0;
//        for (it=g[nod].begin (); it!=g[nod].end (); ++it)
//            if (dst[nod]+it->sc<dst[it->fs])
//            {
//                dst[it->fs]=dst[nod]+it->sc;
//                if (!viz[it->fs])
//                {
//                    viz[it->fs]=1;
//                    q.push (it->fs);
//                }
//            }
//    }
//}
//
//inline int calc (int x,int y,int start)
//{
//    int nod;
//
//    if (x>y)
//        return 0;
//
//    if (bst[x][y][start]!=INF)
//        return bst[x][y][start];
//
//    for (nod=x; nod<=y; ++nod)
//        bst[x][y][start]=min (bst[x][y][start],calc (x,nod-1,nod)+calc (nod+1,y,nod)+cst[start][nod]);
//
//    return bst[x][y][start];
//}
//
//int main ()
//{
//    int i,j,x,y,z;
//
//    fin>>P>>N>>M;
//    for (i=1; i<=M; ++i)
//    {
//        fin>>x>>y>>z;
//        g[x].pb (mp (y,z));
//        g[y].pb (mp (x,z));
//    }
//
//    D[0]=1;
//    for (i=1; i<=N; ++i)
//        fin>>D[i];
//
//    for (i=0; i<=P; ++i)
//    {
//        dijkstra (D[i]);
//        for (j=0; j<=P; ++j)
//            cst[i][j]=dst[D[j]];
//    }
//
//    memset (bst,INF,sizeof (bst));
//    fout<<calc (1,P,0);
//
//    return 0;
//}
#include <fstream>
#include <bitset>
#include <vector>
#include <queue>

using namespace std;

ifstream fin ("team.in");
ofstream fout ("team.out");

#define MAX_N 505
#define MAX_P 55
#define INF 0x3f3f3f3f

#define nd first
#define cst second

#define foreach(V) for(typeof (V).begin() it = (V).begin(); it != (V).end(); ++it)

int P, N, M;
vector <pair <int, int> > G[MAX_N];
int D[MAX_P], Dst[MAX_P][MAX_P], Bst[MAX_N];
int Sol[MAX_P][MAX_P][MAX_P];

void citire()
{
	fin >> P >> N >> M;

	for(int i = 1; i <= M; ++i)
	{
		int x, y, c;
		fin >> x >> y >> c;

		G[x].push_back( make_pair(y,c) );
		G[y].push_back( make_pair(x,c) );
	}

	for(int i = 1; i <= P; ++i)
		fin >> D[i];
	D[0] = 1;
}

struct cmp
{
	bool operator()(const int &a, const int &b)
	{
		return Bst[a] > Bst[b];
	}
};

void Dijkstra(int k)
{
	memset(Bst, INF, sizeof Bst);
	Bst[k] = 0;
	priority_queue<int, vector <int>, cmp> Q;
	bitset <MAX_N> inq;
	inq[k] = 1;

	for(Q.push(k); !Q.empty(); Q.pop())
	{
		int nod = Q.top();
		inq[nod] = 0;

		foreach(G[nod])
		{
			int act = it -> nd;
			int cst = it -> cst;

			if(Bst[act] > Bst[nod] + cst)
			{
				Bst[act] = Bst[nod] + cst;
				if(!inq[act])
				{
					Q.push(act);
					inq[act] = 1;
				}
			}
		}
	}
}

int solve(int i, int j, int nod)
{
	if(Sol[i][j][nod] < INF) return Sol[i][j][nod];
	if(i > j) return 0;

	for(int k = i; k <= j; ++k)
		Sol[i][j][nod] = min(Sol[i][j][nod], solve(i, k-1, k) + solve(k+1, j, k) + Dst[nod][k]);

	return Sol[i][j][nod];
}

int main()
{
	citire();

	for(int i = 0; i <= P; ++i)
	{
		Dijkstra(D[i]);

		for(int j = 0; j <= P; ++j)
			Dst[i][j] = Bst[D[j]];
	}

	memset(Sol, INF, sizeof Sol);
	fout << solve(1, P, 0) << "\n";
}