Guest User

Untitled

a guest
May 20th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <vector>
  5. #include <algorithm>
  6. using namespace std;
  7. const double eps = 0.0000001;
  8. struct vect
  9. {
  10.     double x, y;
  11. };
  12. struct point
  13. {
  14.     double x,y;
  15. };
  16. struct line
  17. {
  18.     double a,b,c;
  19. };
  20.  
  21. int n;
  22. point a[250];
  23. double BC,AC,AB;
  24. int ans1,ans2,ans3;
  25. vector<pair <int, int > > ansost;
  26. double ans;
  27. point ansp;
  28. double dist(point aa, point b)
  29. {
  30.     return sqrt((aa.x-b.x)*(aa.x-b.x)+(aa.y-b.y)*(aa.y-b.y));
  31. }
  32. vector<pair <double,pair<int,int> > > g;
  33. const int INF = 1000000000;
  34.  
  35.  
  36.  
  37. vector < pair<int,int> >res;
  38. double solve()
  39. {
  40.     double cost=0;
  41.     res.clear();
  42.     sort(g.begin(),g.end());
  43.     vector<int> tree_id(g.size());
  44.     for(int i=0;i<g.size();++i)
  45.     tree_id[i]=i;
  46.     for(int i=0;i<g.size();++i)
  47.     {
  48.         int a1=g[i].second.first,b=g[i].second.second;
  49.         double l=g[i].first;
  50.         if(tree_id[a1]!=tree_id[b])
  51.         {
  52.             cost+=l;
  53.             res.push_back(make_pair(a1,b));
  54.             int old_id=tree_id[b],new_id=tree_id[a1];
  55.             for(int j=0;j<g.size();++j)
  56.                 if(tree_id[j]==old_id)
  57.                     tree_id[j]=new_id;
  58.         }
  59.     }
  60.     return cost;
  61. }
  62. void make_chnge(int h,int h1,int h2,point pp)
  63. {
  64.     g.push_back(make_pair(dist(pp,a[h]),make_pair(n,h)));
  65.     g.push_back(make_pair(dist(pp,a[h1]),make_pair(n,h1)));
  66.     g.push_back(make_pair(dist(pp,a[h2]),make_pair(n,h2)));
  67.     double tmp=solve();
  68.     if(tmp<ans)
  69.     {
  70.         ans=tmp;
  71.         ansp=pp;
  72.         ansost=res;
  73.         ans1=h+1;
  74.         ans2=h1+1;
  75.         ans3=h2+1;
  76.     }
  77. }
  78. void deyk()
  79. {
  80.     int s = 5;
  81.     vector <pair<int, int> > g1[10];
  82.    
  83.     vector<int> d (n, INF),  p (n);
  84.     d[s] = 0;
  85.     vector<char> u (n);
  86.     for (int i=0; i<n; ++i) {
  87.         int v = -1;
  88.         for (int j=0; j<n; ++j)
  89.             if (!u[j] && (v == -1 || d[j] < d[v]))
  90.                 v = j;
  91.         if (d[v] == INF)
  92.             break;
  93.         else
  94.         u[v] = true;
  95.  
  96.         for (size_t j=0; j<g1[v].size(); ++j) {
  97.             int to = g1[v][j].first,
  98.                 len = g1[v][j].second;
  99.             if (d[v] + len < d[to]) {
  100.                 d[to] = d[v] + len;
  101.                 p[to] = v;
  102.             }
  103.         }
  104.     }
  105. }
  106. line make_line(point p1,point p2)
  107. {
  108.     line ans;
  109.     ans.a=p2.y-p1.y;
  110.     ans.b=p1.x-p2.x;
  111.     ans.c=(-(p2.y-p1.y)*p1.x-(p1.x-p2.x)*p1.y);
  112.     return ans;
  113. }
  114. int check_on_line_all_ost(point Ao,point Bo,point Co)
  115. {
  116.     line lineBC=make_line(Bo,Co);
  117.     double ss=lineBC.a*Ao.x+lineBC.b*Ao.y+lineBC.c;
  118.     if(ss<=eps && ss>=-eps)
  119.     {
  120.         return 0;
  121.     }
  122.     return 1;
  123. }
  124. bool check_alf(double A,double B,double C)
  125. {
  126.     return((A*A-B*B-C*C)/(-2*B*C)<-0.5);
  127. }
  128. bool check_ost(int f)
  129. {
  130.     return ansost[f].first!=n && ansost[f].second!=n;
  131. }
  132. point tochka_vershyna(point a,point b,point c)
  133. {
  134.     vect v1;
  135.     point P1,P2,F1;
  136.     v1.x=b.x-a.x;
  137.     v1.y=b.y-a.y;
  138.     P1.x=(a.x+b.x)/2+v1.y*sqrt(3)/2.0;
  139.     P1.y=(a.y+b.y)/2+(-v1.x*sqrt(3)/2.0);
  140.     P2.x=(a.x+b.x)/2-v1.y * sqrt(3)/2.0;
  141.     P2.y=(a.y+b.y)/2-(-v1.x*sqrt(3)/2.0);
  142.     if(dist(P1,c)>dist(P2,c))
  143.         F1=P1;
  144.     else
  145.         F1=P2;
  146.     return F1;
  147. }
  148.  
  149. point Ferma(point A,point B,point C)
  150. {
  151.    
  152.     BC=dist(B,C);
  153.     AC=dist(A,C);
  154.     AB=dist(A,B);
  155.     if(!check_on_line_all_ost(A,B,C))
  156.     {
  157.         point gg;
  158.         gg.x=-10000;
  159.         gg.y=-10000;
  160.         return gg;
  161.     }
  162.     if(check_alf(BC,AC,AB))
  163.     {
  164.         point gg;
  165.         gg.x=-10000;
  166.         gg.y=-10000;
  167.         return gg;
  168.     }
  169.     if(check_alf(AC,BC,AB))
  170.     {
  171.         point gg;
  172.         gg.x=-10000;
  173.         gg.y=-10000;
  174.         return gg;
  175.     }
  176.     if(check_alf(AB,AC,BC))
  177.     {
  178.         point gg;
  179.         gg.x=-10000;
  180.         gg.y=-10000;
  181.         return gg;
  182.     }
  183.     point P1=tochka_vershyna(A,B,C),P2=tochka_vershyna(A,C,B);
  184.     line L1=make_line(C,P1),L2=make_line(B,P2);
  185.     point Pans;
  186.     Pans.x=(L2.b*L1.c-L1.b*L2.c)/(L2.a*L1.b-L1.a*L2.b);
  187.     Pans.y=(L1.b!=0?(-L1.c-L1.a*Pans.x)/L1.b:(-L2.c-L2.a*Pans.x)/L2.b);
  188.     //double ansdist=0;
  189.     /*for(int i=0;i<n;i++)
  190.         ansdist+=dist(Pans,a[i]);
  191.     printf("%.7lf",ansdist);
  192.     cout<<endl;
  193.     printf("%.7lf %.7lf",Pans.x,Pans.y);
  194.     cout<<endl;
  195.     cout<<"3 ";
  196.     cout<<"1 2 3";
  197.     cout<<endl;
  198.     cout<<"0";*/
  199.     //ansdist+=dist(Pans,A)+dist(Pans,B)+dist(Pans,C);
  200.     return Pans;
  201.  
  202. }
  203. int check_on_line_all()
  204. {
  205.     line lineBC=make_line(a[1],a[2]);
  206.     double ss=lineBC.a*a[0].x+lineBC.b*a[0].y+lineBC.c;
  207.     if(ss<=eps && ss>=-eps)
  208.     {
  209.         double ans;
  210.         ans=max(AB, max(AC,BC));
  211.         printf("%.6lf\n", ans);
  212.         cout<<"0 0";
  213.         cout<<endl;
  214.         cout<<"0";
  215.         cout<<endl;
  216.         cout<<"2";
  217.         cout<<endl;
  218.         if(ans==AB)
  219.         {
  220.             cout<<"1 ";
  221.             cout<<"3";
  222.             cout<<endl;
  223.             cout<<"2 ";
  224.             cout<<"3";
  225.             cout<<endl;
  226.         }
  227.         if(ans==AC)
  228.         {
  229.             cout<<"1 ";
  230.             cout<<"2";
  231.             cout<<endl;
  232.             cout<<"2 ";
  233.             cout<<"3";
  234.             cout<<endl;
  235.         }
  236.         if(ans==BC)
  237.         {
  238.             cout<<"1 ";
  239.             cout<<"2";
  240.             cout<<endl;
  241.             cout<<"1 ";
  242.             cout<<"3";
  243.             cout<<endl;
  244.         }
  245.         return 0;
  246.     }
  247.     return 1;
  248. }
  249.  
  250. int kol_perem(int size)
  251. {
  252.     int ds=0;
  253.     for(int i=0;i<size;i++)
  254.         if(check_ost(i))
  255.                 ds++;
  256.     return ds; 
  257. }
  258. void cout_perem(int size)
  259. {
  260.     for(int i=0;i<size;i++)
  261.         {
  262.             if(check_ost(i))
  263.                 cout<<ansost[i].first+1<<" "<<ansost[i].second+1<<endl;
  264.         }
  265. }
  266. int main()
  267. {
  268.     cin>>n;
  269.     if(n==1)
  270.     {
  271.         cout<<"0";
  272.         cout<<endl;
  273.         cout<<"0 ";
  274.         cout<<"0";
  275.         cout<<endl;
  276.         cout<<"0";
  277.         cout<<endl;
  278.         cout<<"0";
  279.         return 0;
  280.     }
  281.     if (n == 2)
  282.     {
  283.         point a1[2];
  284.         cin>>a1[0].x>>a1[0].y>>a1[1].x>>a1[1].y;
  285.         printf("%.7lf\n",dist(a1[0],a1[1]));
  286.         cout<<"0 ";
  287.         cout<<"0";
  288.         cout<<endl;
  289.         cout<<"0";
  290.         cout<<endl;
  291.         cout<<"1";
  292.         cout<<endl;
  293.         cout<<"1 ";
  294.         cout<<"2";
  295.         return 0;
  296.     }
  297.    
  298.     for(int i=0;i<n;i++)
  299.         cin>>a[i].x>>a[i].y;
  300.     g.clear();
  301.     for(int i=0;i<n;i++)
  302.         for(int j=0;j<n;j++)
  303.             g.push_back(make_pair(dist(a[i],a[j]),make_pair(i,j)));
  304.         ans=solve();
  305.         //point ansp;
  306.         ansp.x=-10000;
  307.        
  308.         ansost=res;
  309.         for(int i=0;i<n;i++)
  310.             for(int j=0;j<n;j++)
  311.                 for(int k=0;k<n;k++)
  312.                     if(i!=j && i!=k && j!=k)
  313.                     {
  314.                         point fer=Ferma(a[i],a[j],a[k]);
  315.                         if(fer.x!=-10000)
  316.                         {
  317.                             make_chnge(i,j,k,fer);
  318.                             g.clear();
  319.                             for(int i1=0;i1<n;i1++)
  320.                                 for(int j1=0;j1<n;j1++)
  321.                                     g.push_back(make_pair(dist(a[i1],a[j1]),make_pair(i1,j1)));
  322.                         }
  323.                     }
  324.         printf("%.7lf",ans);
  325.         cout<<endl;
  326.         if(ansp.x==-10000)
  327.         {
  328.             cout<<0<<" ";
  329.             cout<<0;
  330.             cout<<endl;
  331.             cout<<0;
  332.             cout<<endl;
  333.         }
  334.         else
  335.         {
  336.             printf("%.7lf %.7lf",ansp.x,ansp.y);
  337.             cout<<endl;
  338.             cout<<"3 ";
  339.             cout<<ans1;
  340.             cout<<" ";
  341.             cout<<ans2;
  342.             cout<<" ";
  343.             cout<<ans3;
  344.             cout<<endl;
  345.         }
  346.         int perem=0;
  347.         int sz_ostov=ansost.size();
  348.         perem=kol_perem(sz_ostov);
  349.         cout<<perem<<endl;
  350.         cout_perem(sz_ostov);
  351.                
  352.     return 0;
  353. }
Add Comment
Please, Sign In to add comment