Mai intai trebuie sa te autentifici.
Cod sursa(job #1818266)
Utilizator | Data | 28 noiembrie 2016 22:55:23 | |
---|---|---|---|
Problema | Ubuntzei | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva de probleme | Marime | 2.76 kb |
#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
FILE*f=freopen("ubuntzei.in","r",stdin);
FILE*g=freopen("ubuntzei.out","w",stdout);
const int inf=1<<30;
const int maxn=2005;
vector < pair <int,int> >h;
vector <int> a[maxn],cost[maxn];
int N,M,K,pos;
int d[maxn][maxn],drum[20],dp[35000][25];
int numar[35000],biti[20];
char buff[290000];
void citire(int &nr)
{
while(buff[pos] < '0' || buff[pos] > '9') if(++pos == 290000) fread(buff, 1, 290000, stdin), pos = 0;
nr = 0;
while('0' <= buff[pos] && buff[pos] <= '9')
{
nr = nr * 10 + buff[pos] - '0';
if(++pos == 290000) fread(buff, 1, 290000, stdin), pos = 0;
}
}
void dijkstra(int x)
{
for ( int i = 1; i <= N; i++ )
d[x][i] = inf;
h.push_back(make_pair(0, x));
int nod,next,sz;
long long cdist;
while (!h.empty())
{
nod = h[0].second;
cdist=-h[0].first;
pop_heap(h.begin(), h.end());
h.pop_back();
sz=a[nod].size();
for(int i=0; i<sz; ++i)
{
next=a[nod][i];
if(d[x][next]>cdist+cost[nod][i])
{
d[x][next]=cdist+cost[nod][i];
h.push_back(make_pair(-d[x][next], next));
push_heap(h.begin(), h.end());
}
}
}
}
int main()
{
int mask,x,y,z,i,cpy,j,k,sol;
citire(N);
citire(M);
citire(K);
for (i=1;i<=K;i++)
citire(drum[i]);
for (i=1;i<=M;i++)
{
citire(x);citire(y);citire(z);
a[x].push_back(y);
a[y].push_back(x);
cost[x].push_back(z);
cost[y].push_back(z);
}
dijkstra(N);
dijkstra(1);
for (i=1;i<=K;i++)
dijkstra(drum[i]);
x=1;
numar[1]=1;
y=1;
if (K==0) {printf("%d",d[1][N]);return 0;}
while(x!=16)
{
y*=2;
x++;
numar[y]=x;
}
for (mask=1;mask < (1<<K); mask++)
{
if ((mask & (mask - 1))==0)
{
dp[mask][numar[mask]]=d[1][drum[numar[mask]]];
continue;
}
cpy=mask;
k=0;y=0;
while(cpy)
{
y++;
if (cpy%2==1) {k++;biti[k]=y;}
cpy/=2;
}
for (i=1;i<=k;i++)
{
x=biti[i];
dp[mask][x]=inf;
for (j=1;j<=k;j++)
{
if (i==j) continue;
y=biti[j];
dp[mask][x]=min(dp[mask][x],dp[(mask ^ (1<<(x-1)))][y]+d[drum[y]][drum[x]]);
}
}
}
sol=inf;
mask=(1<<K)-1;
for (i=1;i<=K;i++)
sol=min(sol,dp[mask][i]+d[drum[i]][N]);
printf("%d",sol);
}