Cod sursa(job #2567175)

Utilizator rusu.ralucaRusu Raluca rusu.raluca Data 3 martie 2020 15:38:58
Problema Team Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.87 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
#include <math.h>

using namespace std;

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

const int MAXN = 505;
const int MAXD = 1005;
const int inf = 1000000005;

int p, n, m;
vector<pair<int, int> > g[MAXN];
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q;

int drum[MAXN][MAXN], dest[MAXN];
int cost[MAXN][MAXN][MAXN];

void Dijkstra(int src, int dist[]){
	for(int i=1; i<=n; ++i){
		dist[i] = inf;
	}

	dist[src] = 0;
	q.push(make_pair(src, 0));

	while(!q.empty()){
		int nodcurrent = q.top().first;
		int costcurrent = q.top().second;
		q.pop();

		if(costcurrent > dist[nodcurrent])
			continue;

		for(int i = 0; i < g[nodcurrent].size(); ++i){
			int newnode = g[nodcurrent][i].first;
			int newcost = g[nodcurrent][i].second;
			if(costcurrent + newcost < dist[newnode]){
				dist[newnode] = costcurrent + newcost;
				q.push(make_pair(newnode, dist[newnode])); 
			}

		}
	}
} 

int main(){
	in >> p >> n >> m;
	for(int i = 0; i < m; ++i){
		int x, y, c;
		in >> 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){
		in >> dest[i];
	}
	dest[0] = 1;
	for(int i = 0; i <= p; ++i){
		Dijkstra(dest[i], drum[i]);
	}

  for(int i = 0; i <= p; ++i)
    for(int j = 0; j <= p; ++j)
      if(i <= j) 
        for(int k = 0; k <= p; ++k)
          cost[i][j][k] = inf;
 
  for(int len = 1; len <= p; ++len)
    for(int i = 1; (i + len - 1) <= p; ++i)
      for(int j = 0; j <= p; ++j){
        if(len == 1 && i == j){
          cost[i][i][i] = 0;
            continue;
        }
        for(int k = i; k <= (i + len - 1); ++k)
          cost[i][i + len - 1][j] = min((cost[i][i+len-1][j]), (cost[i][k-1][k]+cost[k+1][i+len-1][k]+drum[j][dest[k]]));
  		}
  out << cost[1][p][0] << '\n';
  return 0;

}