Cod sursa(job #865715)

Utilizator ericptsStavarache Petru Eric ericpts Data 26 ianuarie 2013 21:12:12
Problema Ubuntzei Scor 45
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.65 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

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

const int maxn = 2005;
int n,m,k;

int must[20];

int best[maxn][1<<16];
bool viz[maxn][1<<16];
struct edge
{
    int to,cost;
};
struct state
{
    int nod,conf;
};

vector<edge> graf[maxn];
vector<edge> :: iterator it;
queue<state> q;

int bsearch(const int &nod)
{
    int i,step = (1<<3);
    for(i=0;step;step>>=1)
        if(i+step <= k && must[i+step] <= nod)
            i += step;
    return must[i] == nod ? i-1 : -1;
}

void bford()
{
    state c;
    int aux;
    int x;
    while(!q.empty())
    {
        c = q.front();
        q.pop();

        for(it = graf[c.nod].begin();it != graf[c.nod].end(); ++ it)
        {

            aux = c.conf;
            x = bsearch(it->to);

            if(x != -1)
                aux |= (1 << x);

            if(best[c.nod][c.conf] + it->cost < best[it->to][aux] || !viz[it->to][aux])
            {
                viz[it->to][aux] = 1;
                best[it->to][aux] = best[c.nod][c.conf] + it->cost;
                q.push((state){it->to,aux});
            }
        }
    }
}
int main()
{
    int i,x,y,z;
    in >> n >> m >> k;
    for(i=1;i<=k;++i)
    {
        in >> x;
        must[i] = x;
    }
    sort(must+1,must+k+1);
    for(;m;--m)
    {
        in >> x >> y >> z;
        graf[x].push_back((edge){y,z});
        graf[y].push_back((edge){x,z});
    }
    viz[1][0] = 1;
    q.push((state){1,0});
    bford();
    out << best[n][(1 << k)-1];
    return 0;
}