Cod sursa(job #1975752)

Utilizator raulmuresanRaul Muresan raulmuresan Data 1 mai 2017 20:59:09
Problema Ubuntzei Scor 75
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.3 kb
#include<fstream>
#include<vector>
#include<string>
#include<queue>
#include<algorithm>
#include<cstring>
#define modulo 666013
#define INF 100000000
using namespace std;

ifstream fin("ubuntzei.in");
ofstream fout("ubuntzei.out");

const int NMAX = 2005;
const int KMAX = 18;

int w[KMAX], d[NMAX];
int cost[KMAX][KMAX];
int best[66000][KMAX];
int i, n, k, j, x, y, m;
vector <pair<int, int> >v[NMAX];


struct cmp
{
    bool operator() (int x,int y)
    {
        return d[x]>d[y];
    }
};

priority_queue<int,vector <int>,cmp> q;

void dijkstra(int start)
{
    for(int i=1;i<=n;i++)
    {
        d[i]=100000000;
    }
    d[start] = 0;
    q.push(start);


    while(! q.empty())
    {
        int nod=q.top();
        q.pop();
        //printf("%d\n",nod);
        for(int i=0;i<v[nod].size();i++)
        {
            if(d[v[nod][i].first] > d[nod]+v[nod][i].second)
            {
                d[v[nod][i].first] = d[nod]+v[nod][i].second;
                q.push(v[nod][i].first);
            }
        }
    }
    /*fout << start<<"\n";
    for(int i = 1; i <= n; i++)
    {
        fout << d[i] <<" ";
    }
    fout<<"\n";*/
}

void init(int n)
{
    for(int i = 1; i <= n; i++)
    {
        for(j = 0; j <= k; j++)
        {
            best[i][j] = INF;
        }
    }

}





int main()
{
    fin >> n >> m >> k;
    w[0] = 1;
    for(i = 1;i <= k; i++)
    {
        fin >> w[i];
    }
    w[++k] = n;
    for(i = 1; i <= m; i++)
    {
        int cost;
        fin >> x >> y >> cost;
        v[x].push_back(make_pair(y, cost));
        v[y].push_back(make_pair(x, cost));
    }


    for(i = 0;i <= k; i++)
    {
        dijkstra(w[i]);
        for(j = 0; j <= k ; j++)
        {
            cost[i][j] = d[w[j]];
        }
    }
    memset (best,INF,sizeof (best));


    int stareFinala = 1 << (k+1);
    init(stareFinala);
    best[1][0] = 0;
    for(int stare = 1; stare < stareFinala; stare++)
    {
        for(int nod = 0; nod <= k; nod++)
        {
            if(best[stare][nod] != INF)
            {
                for(int newNode = 0; newNode <= k; newNode++)
                {
                    //fout<<newNode<<"\n";
                    //fout<<(stare & (1 << (newNode)))<<"\n\n";
                    if((stare & (1 << (newNode))) == 0)
                    {
                        //fout <<"intra\n";
                        //fout<<nod<<" "<<newNode<<" ";
                        //fout<<nod<<" "<<newNode<<" "<<cost[nod][newNode]<<  "\n";
                        //fout<<(stare + (1<<newNode))<<"\n";
                        best[(stare + (1<<newNode))][newNode] = min( best[(stare + (1<<newNode))][newNode], best[stare][nod] + cost[nod][newNode]);
                    }
                }
                //fout << stare <<" "<<nod<<"\n";
            }
             //for(int nod = 0; nod <= k; nod++)
        }
        //fout << stare << " ";
    }
    fout << best[stareFinala - 1][k] << "\n";


    /*for(i = 0; i <= k; i++)
    {
        fout << w[i] << " ";
    }
    fout << "\n";

    for(i = 0;i <= k; i++)
    {
        dijkstra(w[i]);
        for(j = 0; j <= k ; j++)
        {
            fout << cost[i][j] << " ";
        }
        fout << "\n";
    }*/


}