Cod sursa(job #2033246)

Utilizator oldatlantianSerban Cercelescu oldatlantian Data 6 octombrie 2017 14:41:59
Problema Ciclu hamiltonian de cost minim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;

ifstream fi("hamilton.in");
ofstream fo("hamilton.out");

using pii = pair<int, int>;

const int N = 18;

vector<pii> g[N];
int dp[N][1 << N];

int n, m;

int main() {
	int ant, a, b, c;

	ant = 0x3f3f3f3f;

	fi >> n >> m;
	for (int i = 0; i < m; ++i) {
		fi >> a >> b >> c;
		g[a].emplace_back(b, c); }

	memset(dp, 0x3f, sizeof dp);

	dp[0][1] = 0;
	for (int i = 0; i < (1 << n); ++i)
		for (int j = 0; j < n; ++j) if ((1 << j) & i)
			for (auto e: g[j]) if ((1 << e.x) & ~i)
				dp[e.x][i | (1 << e.x)] = min(dp[j][i] + e.y, dp[e.x][i | (1 << e.x)] );

	for (int i = 1; i < n; ++i)
		for (auto e: g[i]) if (e.x == 0)
			ant = min(ant, (e.y + dp[i][(1 << n) - 1]));
			
	if (ant == 0x3f3f3f3f)
		fo << "Nu exista solutie" << endl;
	else
		fo << ant << endl;

	return 0; }