Cod sursa(job #1535779)

Utilizator ionut98Bejenariu Ionut Daniel ionut98 Data 25 noiembrie 2015 10:23:05
Problema Ubuntzei Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.78 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
vector<pair<int, int> >graph[Nmax];
int p[Kmax],dist[Nmax];
int edges[Kmax+2][Kmax+2];
int mat[1<<Kmax][Kmax];
int n,m,k;
struct cmp
{
    bool operator()(const int &i,const int &j)
    {
        return dist[i]<dist[j];
    }
};
priority_queue <int,vector <int>,cmp>heap;
void read()
{
    int u,v,c;
    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>>c;
        graph[u].push_back(make_pair(v, c) );
        graph[v].push_back(make_pair(u, c) );
    }
}
void dijkstra(int s)
{
    vector<pair<int,int> >::iterator it;
    int u;
    for(int i=1;i<=n;++i)
      dist[i]=inf;
    dist[s]=0;
    heap.push(s);
    while(!heap.empty())
    {
        u=heap.top();
        heap.pop();
        for(it=graph[u].begin();it!=graph[u].end();++it)
          if(dist[it->first]>dist[u]+it->second)
          {
              dist[it->first]=dist[u]+it->second;
              heap.push(it->first);
          }
    }
}
void solve()
{
    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()
{
    read();
    solve();
    return 0;
}