Cod sursa(job #1729457)

Utilizator mariakKapros Maria mariak Data 14 iulie 2016 19:23:15
Problema Ubuntzei Scor 25
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.63 kb
#include <bits/stdc++.h>
#define Nmax 2002
#define INF 1 << 30
#define MAX (1 << 15) + 2
#define pii pair <int, int>
#define Pii pair <int, pii>
#define f first
#define s second
#define pb(x) push_back(x)
//FILE *fin  = freopen("ubuntzei.in", "r", stdin);
FILE *fout = freopen("ubuntzei.out", "w", stdout);

using namespace std;
int n, m, fconf;
struct comp
{
    bool operator() (const Pii &a, const Pii &b)
    {
        return a.s.f > b.s.f;
    }
};
priority_queue<Pii, vector<Pii>, comp > Q;
vector <pii> G[MAX];
int D[Nmax][MAX];
bitset <Nmax> F[MAX];
bitset <Nmax> b;
class InputReader
{
public:
    InputReader() {}
    InputReader(const char *file_name)
    {
        input_file = fopen(file_name, "r");
        cursor = 0;
        fread(buffer, SIZE, 1, input_file);
    }
    inline InputReader &operator >>(int &n)
    {
        while(buffer[cursor] < '0' || buffer[cursor] > '9')
        {
            advance();
        }
        n = 0;
        while('0' <= buffer[cursor] && buffer[cursor] <= '9')
        {
            n = n * 10 + buffer[cursor] - '0';
            advance();
        }
        return *this;
    }
private:
    FILE *input_file;
    static const int SIZE = 1 << 17;
    int cursor;
    char buffer[SIZE];
    inline void advance()
    {
        ++ cursor;
        if(cursor == SIZE)
        {
            cursor = 0;
            fread(buffer, SIZE, 1, input_file);
        }
    }
};
void read()
{
    int x, y, z, k;
    InputReader cin("ubuntzei.in");
    cin >> n >> m >> k;
    while(k --)
    {
        cin >> x;
        b.set(x);
        fconf ^= (1 << (n - x));
    }
    while(m --)
    {
        cin >> x >> y >> z;
        G[x].pb(pii(y, z));
        G[y].pb(pii(x, z));
    }
    Q.push(Pii(1, pii(0, 0)));

    for(int i = 2; i <= n; ++ i)
            for(int j = 0; j <= fconf; ++ j)
                D[i][j] = INF;
}
void solve()
{
    int x, y, z, c1, c2, sz;
    while(!Q.empty())
    {
        x = Q.top().f;
        c1 = Q.top().s.s;
        Q.pop();

        if(F[x].test(c1)) continue;

        sz = G[x].size();
        for(int i = 0; i < sz; ++ i)
        {
            c2 = c1;
            y = G[x][i].f;
            z = G[x][i].s;

            if(b.test(y))
                c2 ^= (1 << (n - y));
            if(D[y][c2] > D[x][c1] + z)
            {
                D[y][c2] = D[x][c1] + z;
                Q.push(Pii(y, pii(D[y][c2], c2)));
            }
        }
        F[x].set(c1);
    }
}
void write()
{
    printf("%d\n", D[n][fconf]);
}
int main()
{
    read();
    solve();
    write();
    return 0;
}