Pagini recente » Cod sursa (job #2924415) | Cod sursa (job #787623) | Cod sursa (job #3251223) | Cod sursa (job #2559492) | Cod sursa (job #2129903)
#include<stdio.h>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
FILE *in, *out;
const int MAXN = 2001;
const int INF = 0x7fffffff;
const int STARI = (1 << 17);
struct edge
{
const bool operator <(const edge &oth)const
{
return c > oth.c;
}
int nod, c;
};
vector <edge> g[MAXN];
priority_queue <edge> q;
vector <int> spc;
int viz[MAXN];
int dj[MAXN], dbu[STARI][17], dist[17][17];
int n, m, k;
void cleandjk()
{
for(int i = 0; i < MAXN; i++)
{
viz[i] = false;
dj[i] = INF;
}
}
void djk(int st)
{
edge nou;
//q.erase();
dj[st] = 0;
nou.nod = st;
nou.c = 0;
q.push(nou);
while(!q.empty())
{
edge cur = q.top();
q.pop();
if(!viz[cur.nod])
{
viz[cur.nod] = true;
for(auto &it : g[cur.nod])
{
int d = it.c + dj[cur.nod];
if(d < dj[it.nod])
{
edge nou;
nou.nod = it.nod;
nou.c = d;
q.push(nou);
dj[it.nod] = d;
}
}
}
}
}
void printmat()
{
for(int i = 1; i < (1 << (k + 2)); i++) {
for(int j = 0; j < k + 2; j++) {
printf("%10d ", dbu[i][j]);
}
printf("\n");
}
printf("-------------------------------\n");
}
void dst(int nod)
{
for(int i = 1; i < (1 << (k + 2)); i++) {
for(int j = 0; j < k + 2; j++) {
dbu[i][j] = INF;
}
}
dbu[1][nod] = 0;
for(int i = 1; i < (1 << (k + 2)); i++) {
for(int j = 0; j < k + 2; j++) {
if(dbu[i][j] != INF) {
//printmat();
for(int t = 1; t < k + 2; t++) {
int xp = (1 << t);
if((i & xp) == 0) {
dbu[i + xp][t] = min(dbu[i + xp][t], dbu[i][j] + dist[j][t]);
}
}
}
}
}
}
int main ()
{
in = fopen("ubuntzei.in", "r");
out = fopen("ubuntzei.out", "w");
fscanf(in, "%d%d%d", &n, &m, &k);
//printf("EJGAY");
spc.push_back(0);
spc.push_back(n - 1);
for(int i = 0; i < k; i++)
{
int x;
fscanf(in, "%d", &x);
x--;
spc.push_back(x);
}
sort(spc.begin(), spc.end());
for(int i = 0; i < m; i++)
{
int a, b, c;
fscanf(in, "%d %d %d", &a, &b, &c);
a--;
b--;
edge nou;
nou.nod = b;
nou.c = c;
g[a].push_back(nou);
nou.nod = a;
g[b].push_back(nou);
}
for(int i = 0; i < k + 2; i++)
{
cleandjk();
djk(spc[i]);
for(int j = 0; j < k + 2; j++)
{
if(i != j)
{
dist[i][j] = dj[spc[j]];
}
}
}
dst(0);
fprintf(out, "%d", dbu[(1 << (k + 2)) - 1][k + 1]);
fclose(in);
fclose(out);
return 0;
}