Pagini recente » Cod sursa (job #3121861) | Cod sursa (job #2083788) | Cod sursa (job #2484715) | Cod sursa (job #659273) | Cod sursa (job #456710)
Cod sursa(job #456710)
/*
* File: main.cpp
* Author: virtualdemon
*
* Created on May 16, 2010, 2:09 PM
*/
#include <vector>
#include <cstdlib>
#include <fstream>
#include <iterator>
#define Nmax 20
#define oo 2147483647
/*
*
*/
using namespace std;
typedef pair< int, int > pr;
int N, Mcost=oo;
bool was[Nmax];
int cycle[Nmax];
int C[Nmax][Nmax];
inline void DFS( int x, int cost, int k )
{
if( N == k )
{
if( C[cycle[N-1]][1] && Mcost > cost+C[cycle[N-1]][1] )
Mcost=cost+C[cycle[N-1]][1];
return;
}
for( int i=1; i <= N; ++i )
if( !was[i] && C[x][i] )
{
cycle[k]=i;
was[i]=true;
DFS( i, cost+C[x][i], k+1 );
was[i]=false;
}
}
int main( void )
{
int M, x, y;
ifstream in( "hamilton.in" );
for( in>>N>>M; M; --M )
{
in>>x>>y;
in>>C[x+1][y+1];
}
cycle[0]=1;
was[1]=true;
DFS( 1, 0, 1 );
ofstream out( "hamilton.out" );
if( oo == Mcost )
out<<"Nu exista solutie\n";
else out<<Mcost<<'\n';
return EXIT_SUCCESS;
}