Cod sursa(job #1384159)

Utilizator retrogradLucian Bicsi retrograd Data 10 martie 2015 22:00:54
Problema Ubuntzei Scor 45
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.79 kb
#include<fstream>
#include<queue>
#include<unordered_map>

using namespace std;
typedef int var;

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


#define INF (1<<28)
#define MAXK 16
#define MAXN 1001


struct Edge {
    var n;
    int64_t c;
    Edge(var n, int64_t c) {
        this->n = n;
        this->c = c;
    }
};
struct Node {
    var n, c;
    int64_t d;
    Node(var a, var b, int64_t c) {
        n = a;
        this->c = b;
        d = c;
    }
};

unordered_map<var, int64_t> D[MAXN];
var POI[MAXN];
vector<Edge> G[MAXN];
var n, k;

struct cmp {
    const bool operator()(const Node &a, const Node &b) {
        return a.d > b.d;
    }
};

int64_t bellman() {
    priority_queue<Node, vector<Node>, cmp> Q;

    D[1][0] = 0;
    Q.push(Node(1, 0, 0));
    while(!Q.empty()) {
        const Node &top = Q.top();
        var node = top.n;
        var conf = top.c;
        var dist = top.d;
        Q.pop();

        if(dist > D[node][conf])
            continue;

        if(node == n && conf == (1<<k)-1)
            return dist;

        for(auto e : G[node]) {
            var newconf = conf;
            if(POI[e.n]) {
                newconf |= (1 << (POI[e.n]-1));
            }
            if(D[e.n].find(newconf) == D[e.n].end() || D[e.n][newconf] > D[node][conf] + e.c) {
                D[e.n][newconf] = D[node][conf] + e.c;
                Q.push(Node(e.n, newconf, D[e.n][newconf]));
            }
        }
    }
}



int main() {
    var m, a, b, c, t;
    fin>>n>>m>>k;
    for(var i=1; i<=k; i++) {
        fin>>t;
        POI[t] = i;
    }
    while(m--) {
        fin>>a>>b>>c;
        G[a].push_back(Edge(b, c));
        G[b].push_back(Edge(a, c));
    }
    fout<<bellman();

    return 0;
}