Pagini recente » Cod sursa (job #1930130) | Cod sursa (job #940904) | Cod sursa (job #2142975) | Cod sursa (job #112391) | Cod sursa (job #2567216)
#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 MAXP = 55;
const int INF = 2000000005;
int p, n, m;
vector<pair<int, int> > g[MAXN];
priority_queue<pair<int, int> > q;
int dist[MAXN][MAXN], dest[MAXP];
int cost[MAXP][MAXP][MAXP];
void Dijkstra(int src, int dist[]){
dist[src] = 0;
q.push(make_pair(src, 0));
while(!q.empty()){
pair<int, int> current = q.top();
q.pop();
if(current.second > dist[current.first])
continue;
for(int i = 0; i < g[current.first].size(); ++i){
int newnode = g[current.first][i].first;
int newcost = g[current.first][i].second;
if(current.second + newcost < dist[newnode]){
dist[newnode] = current.second + 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 = 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] << '\n';
return 0;
}