Pagini recente » Cod sursa (job #3256482) | Cod sursa (job #1789368) | Cod sursa (job #2850796) | Cod sursa (job #1047581) | Cod sursa (job #1904435)
#include <fstream>
#include <vector>
#include <utility>
using namespace std;
const int NMAX = 18;
const int INF = 1E9;
int N, M;
vector <pair <int, int> > graph[NMAX + 1];
int ans = INF;
void backtr(int node, int cost, int vis) {
if (vis == ((1 << N) - 1)) {
for (int i = 0; i < graph[node].size(); ++ i)
if (graph[node][i].first == 0)
ans = min(ans, cost + graph[node][i].second);
return ;
}
for (int i = 0; i < graph[node].size(); ++ i)
if (!(vis & (1 << (graph[node][i].first))))
backtr(graph[node][i].first, cost + graph[node][i].second, vis | (1 << (graph[node][i].first)));
}
int main()
{
ifstream cin("hamilton.in");
ofstream cout("hamilton.out");
cin >> N >> M;
for (int i = 0; i < M; ++ i) {
int a, b, c;
cin >> a >> b >> c;
graph[a].push_back(make_pair(b, c));
}
backtr(0, 0, 1);
if (ans == INF)
cout << "Nu exista solutie\n";
else
cout << ans << '\n';
return 0;
}