Pagini recente » Cod sursa (job #3223197) | Cod sursa (job #1421082) | Cod sursa (job #1383365) | Cod sursa (job #1005453) | Cod sursa (job #1904406)
#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];
bool vis[NMAX + 1];
int ans = INF;
void backtr(int node, int cnt, int cost) {
if (cnt == N) {
for (int i = 0; i < graph[node].size(); ++ i)
if (graph[node][i].first == 1)
ans = min(ans, cost + graph[node][i].second);
return ;
}
for (int i = 0; i < graph[node].size(); ++ i)
if (!vis[graph[node][i].first]) {
vis[graph[node][i].first] = true;
backtr(graph[node][i].first, cnt + 1, cost + graph[node][i].second);
vis[graph[node][i].first] = false;
}
}
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; ++ a, ++ b;
graph[a].push_back(make_pair(b, c));
}
backtr(1, 1, 0);
if (ans == INF)
cout << "Nu exista solutie\n";
else
cout << ans << '\n';
return 0;
}