Cod sursa(job #1621038)

Utilizator bogdanboboc97Bogdan Boboc bogdanboboc97 Data 29 februarie 2016 15:52:25
Problema Ubuntzei Scor 45
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.04 kb
#include <iostream>
#include <vector>
#include <algorithm>
#include <limits>
#include <numeric>
#include <cstring>
#include <string>
#include <queue>
#include <set>
#include <fstream>
#define pb push_back
#define mp make_pair
#define INF 32000
#define bit(x) (-x)&x
#define int64 long long
using namespace std;
ifstream in("ubuntzei.in");
ofstream out("ubuntzei.out");
vector< vector< pair<short,short> > > a;
queue<short> q;
vector<bool> use;
short n,m,k,c[18];
void bellman(short st,vector<short>& d)
{
    use=vector<bool> (n+1);
    d=vector<short> (n+1,INF);
    q.push(st);
    use[st]=true;
    d[st]=0;
    while(!q.empty())
    {
        short x=q.front();
        q.pop();
        use[x]=false;
        for(vector< pair<short,short> >::iterator i=a[x].begin();i!=a[x].end();i++)
        if(d[x] + i->second < d[i->first])
        {
            d[i->first]=d[x] + i->second;
            if(use[i->first]==false)
            {
                use[i->first]=true;
                q.push(i->first);
            }
        }
    }
}
vector<short> v;
short d[18][18],dp[15][17][1<<17];
int main()
{
    in>>n>>m>>k;
    a=vector< vector< pair<short,short> > > (n+1);
    for(short i=2;i<=k+1;i++)
        in>>c[i];
    c[1]=1;
    c[k+2]=n;
    k+=2;
    for(;m;m--)
    {
        short x,y,z;
        in>>x>>y>>z;
        a[x].pb(mp(y,z));
        a[y].pb(mp(x,z));
    }
    for(short i=1;i<=k;i++)
    {
        bellman(c[i],v);
        for(short j=1;j<=k;j++)
            d[i-1][j-1]=v[c[j]];
    }
    n=k;
    for(short i=0;i<n;i++)
    for(short j=0;j<n;j++)
    for(int k=0;k<(1<<n);k++)
        dp[i][j][k]=INF;
    for(int i=1;i<n;i++)
        dp[0][i][1<<i]=d[0][i];
    for(int i=0;i<n-2;i++)
    for(int j=1;j<n;j++)
    for(int k=0;k<(1<<n);k++)
    if(dp[i][j][k]!=INF)
    {
        for(int t=1;t<n;t++)
            if((k&(1<<t))==0 && d[j][t]!=INF)
            dp[i+1][t][k|(1<<t)]=min(dp[i+1][t][k|(1<<t)],short(dp[i][j][k]+d[j][t]));
    }
    short sol=INF;
    for(int k=0;k<(1<<n);k++)
        sol=min(sol,dp[n-2][n-1][k]);
    out<<sol<<'\n';
    return 0;
}