Cod sursa(job #532482)

Utilizator david_raucaRauca Ioan David david_rauca Data 11 februarie 2011 19:52:54
Problema Traseu Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.5 kb
#include<fstream>
#include<vector>
using namespace std;

ifstream fin("traseu.in");
ofstream fout("traseu.out");

int n, m;

int a[100][100];
int lung[100];

void Read();
void DF( int x, int sum );

int main()
{
    Read();
    DF(1, 0);
    
    fin.close();
    fout.close();
    
    return 0;
}

void Read()
{
     fin >> n >> m;
     int x, y, c;
     while( fin >> x >> y >> c )
     {
          a[x][y] = c;
          lung[x] = lung[x]+1;
     }
/*
     for( int i = 1; i <= n; ++i )
     {
          fout << i << ": ";
          for( int j = 1; j <= n; ++j )
               if( a[i][j] != 0 )
                   fout << "(" << j << ", " << a[i][j] << ") ";
               else
                   fout << " * ";
          fout << '\n';
     }
  */   
}

void DF( int x, int sum )
{
     if( lung[x] == 0 )
     {
         fout << sum;
         exit(0);
     }
     
     for( int j = 1; j <= n; ++j )
          if( a[x][j] != 0 )
          {
              if( j == 1 && x != 1 )
              {
                  lung[x] = lung[x] - 1;
                  int k = a[x][j];
                  a[x][j] = 0;
                  DF( j, sum+k);
              }
              /*
              if( lung[j] == 1 )
              {
                  int k = a[x][j];
                  a[x][j] = 0;
                  lung[x] = lung[x] - 1;
                  DF( j, sum+k );
              }
              */
              DF( j, sum + a[x][j] );
          }
}