Pagini recente » Cod sursa (job #3272647) | Cod sursa (job #115797) | Cod sursa (job #3261280) | Cod sursa (job #6820) | Cod sursa (job #2958376)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f("team.in");
ofstream g("team.out");
int p,n,m,d[501][501],dist[501],poz[51];
struct muchie
{
int x,cost;
};
vector<muchie>a[501];
void dijkstra(int dist[],int s)
{
priority_queue<pair<int,int>, vector<pair<int,int>>,greater<>>q;
q.push({0,s});
for(int i=1; i<=n; i++)
dist[i]=1e9;
dist[s]=0;
while(!q.empty())
{
auto x= q.top();
q.pop();
if(dist[x.second]!=x.first)
continue;
for(auto it :a[x.second])
{
if(dist[it.x]>x.first+it.cost)
{
dist[it.x]=x.first+it.cost;
q.push({dist[it.x],it.x});
}
}
}
}
int w[52][52][52];
int main()
{
f>>p>>n>>m;
for(int i=1; i<=m; i++)
{
int X,Y,Z;
f>>X>>Y>>Z;
a[X].push_back({Y,Z});
a[Y].push_back({X,Z});
}
for(int i=1; i<=p; i++)
f>>poz[i];
poz[0]=1;
for(int i = 0; i <= p; i++)
for(int j = 1; j <= p; j++)
for(int h = j; h <= p; h++)
w[i][j][h] = 1e9;
for(int i=0; i<=p; i++)
{
dijkstra(dist,poz[i]);
for(int j=1; j<=n; j++)
d [i][j]=dist[j];
}
for(int i=0; i<=p; i++)
for(int j=1; j<=p; j++)
w[i][j][j]=d[j][poz[i]];
for(int k=1;k<p;k++)
{
for(int j=1;j<=p-k;j++)
{
for(int ds=0;ds<=p;ds++)
{
w[ds][j][j+k] = 1e9;
for(int i=j;i<=j+k;i++)
{
w[ds][j][j+k] = min(w[ds][j][j+k], d[ds][poz[i]] + w[i][j][i-1] + w[i][i+1][j+k]);
}
}
}
}
g<<w[0][1][p];
return 0;
}