Pagini recente » Cod sursa (job #39651) | Cod sursa (job #2509338) | Cod sursa (job #644339) | Cod sursa (job #699188) | Cod sursa (job #456709)
Cod sursa(job #456709)
/*
* 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];
vector< int > G[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;
}
vector< int >::const_iterator it=G[x].begin(), iend=G[x].end();
for( ; it < iend; ++it )
if( !was[*it] && C[x][*it] )
{
cycle[k]=*it;
was[*it]=true;
DFS( *it, cost+C[x][*it], k+1 );
was[*it]=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];
G[x+1].push_back(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;
}