Cod sursa(job #1628347)

Utilizator radarobertRada Robert Gabriel radarobert Data 3 martie 2016 23:18:07
Problema Ciclu hamiltonian de cost minim Scor 65
Compilator cpp Status done
Runda Arhiva educationala Marime 2.93 kb
#include <cstdio>
#include <queue>
#include <algorithm>
#include <iostream>

using namespace std;

const int INF = 0x3f3f3f3f;

queue< pair<int, int> > q;
int g[20][20], cost[20][20], g2[20][20];
int c[20][524289], c2[20][524289];
int nr[524289];
bool in_queue[20][524289];

int main()
{
    freopen("hamilton.in", "r", stdin);
    freopen("hamilton.out", "w", stdout);

    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 1, x, y; i <= m; i++)
    {
        scanf("%d%d", &x, &y);
        scanf("%d", &cost[x][y]);
        g[x][++g[x][0]] = y;
        g2[y][++g2[y][0]] = x;
    }

    int sol = INF;

    nr[1] = 1;

    c2[0][1] = 1;
    q.push(make_pair(0, 1));
    int node, new_config;
    while (!q.empty())
    {
        int first = q.front().first;
        int config = q.front().second;
        q.pop();
        in_queue[first][config] = false;
        for (int i = 1; i <= g2[first][0]; i++)
            if (!(config & (1<<g2[first][i])))
            {
                node = g2[first][i];
                new_config = config | (1<<node);
                if (c2[node][new_config] == 0 || c2[node][new_config] > c2[first][config] + cost[node][first])
                {
                    c2[node][new_config] = c2[first][config] + cost[node][first];
                    nr[new_config] = nr[config]+1;
                    if (!in_queue[node][new_config] && nr[new_config] < (n+3)/2)
                    {
                        in_queue[node][new_config] = true;
                        q.push(make_pair(node, new_config));
                    }
                }
            }
    }

    int full = (1<<n)-2;
    c[0][1] = 1;
    q.push(make_pair(0, 1));
    while (!q.empty())
    {
        int last = q.front().first;
        int config = q.front().second;
        q.pop();
        in_queue[last][config] = false;
        for (int i = 1; i <= g[last][0]; i++)
            if (!(config & (1<<g[last][i])))
            {
                node = g[last][i];
                new_config = config | (1<<node);
                if (c[node][new_config] == 0 || c[node][new_config] > c[last][config] + cost[last][node])
                {
                    c[node][new_config] = c[last][config] + cost[last][node];
                    if (nr[new_config] == (n+2)/2)
                    {
                        int inverted = (new_config ^ full) + (1<<node);
                        if (c2[node][inverted])
                            sol = min(sol, c[node][new_config] + c2[node][inverted] - 2);
                    }
                    else if (!in_queue[node][new_config])
                    {
                        in_queue[node][new_config] = true;
                        q.push(make_pair(node, new_config));
                    }
                }
            }
    }

    if (sol == INF)
        printf("Nu exista solutie\n");
    else
        printf("%d\n", sol);

    return 0;
}