Pagini recente » Cod sursa (job #3237585) | Cod sursa (job #3135760) | Cod sursa (job #618248) | Cod sursa (job #3042156) | Cod sursa (job #2567233)
#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");
int n, m, p, dest[55];
const int INF = 2000000000;
bool f[505];
vector <pair <int, int> > g[505];
priority_queue <pair <int, int> > q;
int dist[505][505];
int cost[55][55][55];
inline void dijkstra(int nod, int d[]){
memset(f, 0, sizeof(f));
q.push(make_pair(0, nod));
d[nod] = 0;
while(!q.empty()){
pair <int, int> aux = q.top();
q.pop();
if(!f[aux.second])
for(int i = 0; i < g[aux.second].size() ; ++i){
int newnode = g[aux.second][i].first;
int newcost = g[aux.second][i].second;
if(d[newnode] > d[aux.second] + newcost){
d[newnode] = d[aux.second] + newcost;
q.push(make_pair(-d[newnode], newnode));
}
f[aux.second] = 1;
}
}
}
int main(){
in >> p >> n >> m;
int x, y, c;
for(int i = 1; i <= m ; ++i){
in >> x >> y >> c;
g[x].push_back(make_pair(y, c));
g[y].push_back(make_pair(x, c));
}
for(int i = 0; i <= n ; ++i)
for(int j = 0; j <= n ; ++j)
dist[i][j] = INF;
for(int i = 1; i <= p ; ++i)
in >> dest[i];
dest[0] = 1;
g[0].push_back(make_pair(1, 0));
for(int i = 0; i <= p ; ++i)
dijkstra(dest[i], dist[i]);
for(int st = p; st >= 1 ; --st)
for(int dr = st; dr <= p ; ++dr)
for(int k = 0; k <= p ; ++k){
cost[st][dr][k] = INF;
for(int nod = st; nod <= dr ; ++nod)
cost[st][dr][k] = min(cost[st][dr][k], cost[st][nod - 1][nod] + cost[nod + 1][dr][nod] + dist[k][dest[nod]]);
}
out << cost[1][p][0];
return 0;
}