Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 21st, 2012  |  syntax: None  |  size: 1.99 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <math.h>
  4. using namespace std;
  5.  
  6. const int MAX = 10000;
  7.  
  8. struct punkt
  9. {      
  10.       int x;
  11.       int y;
  12.       };
  13.        
  14.                  
  15. double w(punkt a, punkt b, punkt c)
  16. {
  17.        double ob =(sqrt(pow((a.x-b.x),2)+pow((a.y-b.y),2)))+ (sqrt( pow((a.x-c.x),2)+pow((a.y-c.y),2)))+sqrt( pow((c.x-b.x),2)+pow((c.y-b.y),2));
  18.  
  19.    return ob;    
  20. }
  21. int main()
  22. {
  23.    
  24.     double k,q,T;
  25.     int size;
  26.     cout<<"Podaj ilosc wieszcholkow ;"<<endl;
  27.     cin>>size;
  28.     int n = size-1;
  29.  
  30.    
  31.  punkt *P = new punkt[size];  
  32.    
  33.     double **s = new double * [size];
  34.    for (int j=0; j<size; j=j+1){
  35.      s[j] = new double[size];
  36.    }
  37.    
  38.    
  39.  double **M= new double * [size];
  40.    for (int j=0; j<size; j=j+1){
  41.     M[j] = new double[size];
  42. }
  43.    
  44.    
  45.     cout<<"Podaj wspolrzedne wierzcholkow;"<<endl;
  46.     punkt a;
  47. for(int i=0;i<size;i++)
  48. {        
  49.         cin>>a.x;
  50.         cin>>a.y;
  51.        
  52.         P[i]=a;
  53.         }
  54.    
  55.  
  56.    for (int i=1;i<=n;i++)
  57.     {
  58.             M[i][i] = 0;
  59.             }
  60.    
  61.             for (int l=2;l<=n;l++)
  62.             {
  63.                 for (int i=1;i<=n-l+1;i++)
  64.                     {
  65.                          int j = i+l-1;
  66.                              M[i][j] = MAX;
  67.                                     for(int k=i;k<=j-1;k=k+1)
  68.                                     {
  69.                                    q = M[i][k] + M[k+1][j] + w(P[i-1],P[j],P[k]);
  70.                                         if(q<M[i][j])
  71.                                         {
  72.                                        M[i][j]=q ;
  73.                                     s[i][j]=k ;
  74.                                          }
  75.                                     }
  76.                          }
  77.             }
  78.            
  79.              T=M[1][n];      
  80.                     cout<<"Optymalna triangulacja wielokata = "<<T<<endl;                                  
  81.  
  82.  
  83.     system("PAUSE");
  84.     return EXIT_SUCCESS;
  85. }