Cod sursa(job #1098682)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 5 februarie 2014 00:43:19
Problema Ubuntzei Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.66 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

const int Nmax = 2001;
const int Kmax = 15;
const int inf = 1000000000;

struct node
{
    int nod;
    int state;

    node( int a, int b )
    {
        nod = a;
        state = b;
    }
};

vector < pair<int,int> > G[Nmax];
queue <node> Q;
int dist[Nmax][1 << Kmax];
int v[Kmax];

int N, M, K;
int final_state;

void Dijkstra()
{
    for ( int i = 1; i <= N; ++i )
            for ( int j = 0; j < ( 1 << Kmax ); ++j )
                    dist[i][j] = inf;

    dist[1][0] = 0;
    Q.push( node( 1, 0 ) );

    while ( Q.size() )
    {
        node n = Q.front();
        Q.pop();

        for ( auto x: G[n.nod] )
        {
            for ( int i = 0; i <= K; ++i )
            {
                if ( i != 0 && v[i] != ( n.nod - 1 ) ) continue;

                if ( dist[x.first][n.state | v[i]] > dist[n.nod][n.state] + x.second )
                {
                    dist[x.first][n.state | v[i]] = dist[n.nod][n.state] + x.second;
                    Q.push( node( x.first, n.state | v[i] ) );
                }
            }
        }
    }

    ofstream g("ubuntzei.out");

    g << dist[N][final_state];
}

int main()
{
    ifstream f("ubuntzei.in");

    f >> N >> M >> K;

    for ( int i = 1; i <= K; ++i )
    {
        f >> v[i];

        v[i]--;

        final_state |= ( v[i] );
    }

    for ( int i = 1, a, b, c; i <= M; ++i )
    {
        f >> a >> b >> c;

        G[a].push_back( make_pair( b, c ) );
        G[b].push_back( make_pair( a, c ) );
    }

    Dijkstra();

    return 0;
}