Cod sursa(job #1624217)

Utilizator ionut98Bejenariu Ionut Daniel ionut98 Data 2 martie 2016 09:25:49
Problema Ubuntzei Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.53 kb
#include<fstream>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
ifstream f("ubuntzei.in");
ofstream g("ubuntzei.out");
#define Nmax 2005
#define Kmax 17
#define inf 1073741824
int p[Kmax],dist[Nmax];
int edges[Kmax+2][Kmax+2];
int mat[1<<Kmax][Kmax];
int n,m,k;
bool ok;
struct localitati
{
    int x,y,c;
}a[20005];
void citire()
{
    int u,v,cc;
    f>>n>>m>>k;
    for(int i=1;i<=k;++i)
      f>>p[i];
    p[0]=1;
    p[k+1]=n;
    ++k;
    for(int i=1;i<=m;++i)
    {
        f>>u>>v>>cc;
        a[i].x=u;
        a[i+m].x=v;
        a[i].y=v;
        a[i+m].y=u;
        a[i].c=a[i+m].c=cc;
    }
    m*=2;
}
void dijkstra(int s)
{
    for(int i=1;i<=n;++i)
      dist[i]=inf;
    dist[s]=0;
    do
    {
        ok=1;
        for(int j=1;j<=m;j++)
          if(dist[a[j].y]>dist[a[j].x]+a[j].c)
          {
              dist[a[j].y]=dist[a[j].x]+a[j].c;
              ok=0;
          }
    }while(ok==0);
}
void rez()
{
    for(int i=0;i<=k;++i)
    {
        dijkstra(p[i]);
        for(int j=0;j<=k;++j)
          edges[i][j]=dist[ p[j] ];
    }
    for(int i=0;i<1<<(k+1);++i)
      for(int j=0;j<=k;++j)
        mat[i][j]=inf;
    mat[1][0]=0;
    for(int i=0;i<(1<<(k+1));++i)
      for(int j=0;j<=k;++j)
        if((1<<j)&i)
          for(int ii=0;ii<=k;++ii)
            if((1<<ii)&i)
              mat[i][j]=min(mat[i][j],mat[i^(1<<j)][ii]+edges[j][ii]);
    g<<mat[(1<<(k+1))-1][k];
}
int main()
{
    citire();
    rez();
    return 0;
}