Pagini recente » Cod sursa (job #1580202) | Cod sursa (job #2299628) | Cod sursa (job #1508438) | Cod sursa (job #879107) | Cod sursa (job #2999381)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
///#include <tryhardmode>
///#include <GODMODE::ON>
using namespace std;
class InParser {
private:
FILE *fin;
char *buff;
int sp;
char read_ch() {
++sp;
if (sp == 4096) {
sp = 0;
fread(buff, 1, 4096, fin);
}
return buff[sp];
}
public:
InParser(const char* nume) {
fin = fopen(nume, "r");
buff = new char[4096]();
sp = 4095;
}
InParser& operator >> (int &n) {
char c;
while (!isdigit(c = read_ch()) && c != '-');
int sgn = 1;
if (c == '-') {
n = 0;
sgn = -1;
} else {
n = c - '0';
}
while (isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
InParser& operator >> (long long &n) {
char c;
n = 0;
while (!isdigit(c = read_ch()) && c != '-');
long long sgn = 1;
if (c == '-') {
n = 0;
sgn = -1;
} else {
n = c - '0';
}
while (isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
};
InParser fin ("ubuntzei.in");
ofstream fout ("ubuntzei.out");
#define int long long
const int INF=2e9;
const int NMAX=2e3+5;
const int MASK=20;
vector<pair<int,int>>v[NMAX];
int dist[NMAX];
int dp2[NMAX][NMAX];
int dp[(1<<MASK)][MASK];
int c[NMAX];
void dijkstra(int p)
{
priority_queue<pair<int,int>>pq;
pq.push(make_pair(0,p));
dist[p]=0;
while(!pq.empty())
{
pair<int,int>p=pq.top();
pq.pop();
for(auto i:v[p.second])
{
if(dist[i.first]>dist[p.second]+i.second)
{
dist[i.first]=dist[p.second]+i.second;
pq.push(make_pair(-dist[i.first],i.first));
}
}
}
}
signed main()
{
int n,m,i,j,k,mask;
fin>>n>>m>>k;
for(i=1;i<=k;i++)
fin>>c[i];
c[0]=1;
c[k+1]=n;
for(i=1;i<=m;i++)
{
int x,y,cost;
fin>>x>>y>>cost;
v[x].push_back(make_pair(y,cost));
v[y].push_back(make_pair(x,cost));
}
for(i=0;i<=k+1;i++)
{
for(j=1;j<=n;j++)
dist[j]=INF;
dijkstra(c[i]);
for(j=0;j<=k+1;j++)
{
dp2[i][j]=dist[c[j]];
dp2[j][i]=dist[c[j]];
}
dp2[i][i]=INF;
}
for(i=0;i<(1<<(k+2));i++)
for(j=0;j<=k+1;j++)
dp[i][j]=INF;
for(i=0;i<=k+1;i++)
dp[0][i]=0;
dp[1][0]=0;
for(mask=1;mask<(1<<(k+2));mask++)
{
for(i=0;i<=k+1;i++)
{
if(mask & (1<<i))
{
for(j=0;j<=k+1;j++)
{
if(dp[mask][i]>dp[mask^(1<<i)][j]+dp2[i][j])
dp[mask][i]=dp[mask^(1<<i)][j]+dp2[i][j];
}
}
}
}
fout<<dp[(1<<(k+2))-1][k+1];
return 0;
}