Cod sursa(job #182369)

Utilizator raula_sanChis Raoul raula_san Data 20 aprilie 2008 19:41:04
Problema Team Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.76 kb
#include <cstdio>
#include <vector>
#include <queue>

using namespace std;

#define MAXN 512
#define MAXP 64
#define INFI 0x3f3f3f3f

#define pb push_back
#define mp make_pair

#define ff first
#define ss second

int P, N, M;
int Dest[MAXP], D[MAXN], Dist[MAXN][MAXN], C[MAXP][MAXP][MAXP];

struct Comp {
	bool operator()(int i, int j) {
		return D[i] > D[j];
	};
};

vector < pair <int, int> > G[MAXN];
priority_queue <int, vector <int>, Comp> Pq;

inline int MIN(int i, int j)
{	return i < j ? i : j;
}

void ReadData()
{
	freopen("team.in", "rt", stdin);

	int x, y, z;
	for(scanf("%d %d %d", &P, &N, &M); M; --M) {
		scanf("%d %d %d", &x, &y, &z);

		G[x].pb(mp(y, z));
		G[y].pb(mp(x, z));
	}

	int i;
	for(i=1; i<=P; ++i)
		scanf("%d", Dest+i);

	Dest[++P] = 1;

	fclose(stdin);
}

void Dijkstra(int s)
{
	int i;
	for(i=1; i<=N; ++i)
		D[i] = INFI;

	D[s] = 0;
	Pq.push(s);

	while(Pq.empty() == false) {
		int nd = Pq.top();
		Pq.pop();

		vector < pair <int, int> > :: iterator it;

		for(it=G[nd].begin(); it!=G[nd].end(); ++it)
			if(D[it->ff] > D[nd] + it -> ss) {
				D[it->ff] = D[nd] + it -> ss;
				Pq.push(it->ff);
			}
	}

	for(i=1; i<=N; ++i)
		Dist[s][i] = D[i];
}

void Solve()
{
	int i;
	for(i=1; i<=P; ++i)
		Dijkstra(Dest[i]);

	int j, k, x, ct;
	for(i=2; i<=P; ++i)
		for(j=1; j<=P-i+1; ++j) {
			k = j + i - 1;

			for(ct=1; ct<=N; ++ct) {
				C[j][k][ct] = INFI;

				for(x=j+1; x<k; ++x) {
					C[j][k][ct] = MIN(C[j][k][ct], C[j][x-1][Dest[x]]+C[x+1][k][Dest[x]]+Dist[ct][x]);
				}
			}
		}
}

void Write()
{
	freopen("team.out", "wt", stdout);

	printf("%d", C[1][P][1]);

	fclose(stdout);
}

int main()
{
	ReadData();
	Solve();
	Write();

	return 0;
}