Pagini recente » Cod sursa (job #3244434) | Cod sursa (job #611774) | Cod sursa (job #949858) | Cod sursa (job #419477) | Cod sursa (job #2778515)
#include <bits/stdc++.h>
#define PII pair <int, int>
#define INF 0x3f3f3f3f
#define NMAX 505
#define PMAX 55
using namespace std;
ifstream f("team.in");
ofstream g("team.out");
int p, n, m, dp[PMAX][PMAX][PMAX], dest[PMAX], d[PMAX][NMAX];
vector <PII> edges[NMAX];
priority_queue <PII, vector <PII>, greater <PII>> pq;
void dijkstra(int nod, int dist[])
{
for(int i = 1; i <= n; i++)
dist[i] = INF;
dist[nod] = 0;
pq.push(make_pair(0, nod));
while(!pq.empty())
{
PII nod = pq.top();
pq.pop();
if(nod.first != dist[nod.second])
continue;
for(auto child : edges[nod.second])
if(dist[child.first] > nod.first + child.second)
{
dist[child.first] = nod.first + child.second;
pq.push(make_pair(dist[child.first], child.first));
}
}
}
int query(int st, int dr, int home)
{
if(st > dr)
return 0;
if(dp[st][dr][home] != 0)
return dp[st][dr][home];
if(st == dr && dr == home)
return 0;
dp[st][dr][home] = INF;
for(int i = st; i <= dr; i++)
dp[st][dr][home] = min(dp[st][dr][home], query(st, i - 1, i) + query(i + 1, dr, i) + d[home][dest[i]]);
return dp[st][dr][home];
}
int main()
{
f >> p >> n >> m;
for(int i = 1; i <= m; i++)
{
int x, y, cost;
f >> x >> y >> cost;
edges[x].push_back(make_pair(y, cost));
edges[y].push_back(make_pair(x, cost));
}
dest[0] = 1;
for(int i = 1; i <= p; i++)
f >> dest[i];
for(int i = 0; i <= p; i++)
{
dijkstra(dest[i], d[i]);
}
g << query(1, p, 0);
return 0;
}