Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <climits>
  4. using namespace std;
  5.  
  6. int maximo(int a, int b) {
  7.    if (a >= b) return a; else return b;
  8. }      
  9.  
  10. int maxdist(int destino, vector< vector<int> > & tarifas, vector<int> & costes) {
  11.    int max = INT_MIN;
  12.    for (int i = 0; i < destino; ++i) {
  13.          if (i == 0) {
  14.              if (max < tarifas[i][destino]) max = tarifas[i][destino];
  15.          }   
  16.          else {
  17.              if (costes[i] == INT_MIN) {
  18.                 costes[i] = maxdist(i,tarifas,costes);
  19.                 int x = maximo(tarifas[i][destino] , costes[i] + tarifas[i][destino]);
  20.                 if (x > max) max = x;
  21.              }
  22.              else {
  23.                 int x = maximo(tarifas[i][destino] , costes[i] + tarifas[i][destino]);
  24.                 if (x > max) max = x;
  25.              }   
  26.          }   
  27.    }
  28.    return max;     
  29.    
  30. }  
  31.  
  32. int main() {
  33.     int n;
  34.     cin >> n;
  35.     if (n == 1) {
  36.      int result;
  37.      cin >> result;
  38.      cout << result << endl;   
  39.     }
  40.     else { 
  41.         vector< vector<int> > tarifas(n,vector<int> (n));
  42.         vector<int> costes(n,INT_MIN);
  43.         int max = INT_MIN;
  44.         for (int i = 0; i < n; ++i) {
  45.            for (int j = 0; j < n; ++j) {
  46.                   cin >> tarifas[i][j];
  47.            }       
  48.         }
  49.        
  50.         for (int i = 1; i < n; ++i) {
  51.            int x = maxdist(i,tarifas,costes);
  52.            if (x > max) max = x;   
  53.         }  
  54.         cout << max << endl;
  55.     }  
  56.    
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement