Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. #include <fstream>
  2. #include<cmath>
  3. #define inf 1.0*1e8
  4. #define nmax 109
  5. #define mmax 50009
  6. #include<algorithm>
  7. #include<iomanip>
  8. using namespace std;
  9. ifstream f("elicoptere.in");
  10. ofstream g("elicoptere.out");
  11. struct as
  12. {
  13. int x,y;
  14. double cost;
  15. }muchie[mmax];
  16. bool cmp(as a,as b)
  17. {
  18. return a.cost<b.cost;
  19. }
  20. struct point{
  21. double x;
  22. double y;
  23. };
  24. struct line{
  25. point a;
  26. point b;
  27. };
  28. struct triunghi
  29. {
  30. point A;
  31. point B;
  32. point C;
  33. };
  34. triunghi v[105];
  35. double dist(point A, point B, point C)
  36. {
  37.  
  38. double a=B.y-C.y;
  39. double b=C.x-B.x;
  40. double c=C.y*(B.x-C.x)-C.x*(B.y-C.y);
  41. double raspuns=1.0*1e7*1e7;
  42. if(A.x==B.x) raspuns=min(raspuns,abs(A.y-B.y));
  43. if(A.x==C.x) raspuns=min(raspuns,abs(A.y-C.y));
  44. if(A.y==B.y) raspuns=min(raspuns,abs(A.x-B.x));
  45. if(A.y==C.y) raspuns=min(raspuns,abs(A.x-C.x));
  46. if(min(B.x,C.x)<A.x && max(B.x,C.x)>A.x)
  47. {
  48. double rez=(-a*A.x-c)/b;
  49. raspuns=min(raspuns,abs(A.y-rez));
  50. }
  51. if(min(B.y,C.y)<A.y && max(B.y,C.y)>A.y)
  52. {
  53. double rez=(-b*A.y-c)/a;
  54. raspuns=min(raspuns,abs(A.x-rez));
  55. }
  56. return raspuns;
  57. }
  58. double calc_dist(triunghi a, triunghi b)
  59. {
  60. double drum=dist(a.A,b.A,b.B);
  61. drum=min(drum,dist(a.A,b.A,b.C));
  62. drum=min(drum,dist(a.A,b.B,b.C));
  63. drum=min(drum,dist(a.B,b.A,b.C));
  64. drum=min(drum,dist(a.B,b.B,b.C));
  65. drum=min(drum,dist(a.B,b.A,b.B));
  66. drum=min(drum,dist(a.C,b.A,b.C));
  67. drum=min(drum,dist(a.C,b.B,b.C));
  68. drum=min(drum,dist(a.C,b.A,b.B));
  69. drum=min(drum,dist(b.A,a.A,a.B));
  70. drum=min(drum,dist(b.A,a.A,a.C));
  71. drum=min(drum,dist(b.A,a.B,a.C));
  72. drum=min(drum,dist(b.B,a.A,a.C));
  73. drum=min(drum,dist(b.B,a.B,a.C));
  74. drum=min(drum,dist(b.B,a.A,a.B));
  75. drum=min(drum,dist(b.C,a.A,a.C));
  76. drum=min(drum,dist(b.C,a.B,a.C));
  77. drum=min(drum,dist(b.C,a.A,a.B));
  78. return drum;
  79. }
  80. int i,j,m,cerinta,star[nmax],t[2][mmax],n,k,stiva[nmax],varf,element,vecin,coloana,elicoptere,conexe[nmax],componente,cate,arbore[nmax],a,b,mini,maxi;
  81. double ki,rez3,cost;
  82. bool viz[nmax];
  83. long long afisat;
  84. int main()
  85. {
  86. f>>cerinta>>n>>ki;
  87. for(i=1;i<=n;i++)
  88. f>>v[i].A.x>>v[i].A.y>>v[i].B.x>>v[i].B.y>>v[i].C.x>>v[i].C.y;
  89. for(i=1;i<n;i++)
  90. for(j=i+1;j<=n;j++)
  91. {
  92. double drum=calc_dist(v[i],v[j]);
  93. if(drum<=ki)
  94. {
  95. k++;
  96. t[0][k]=j;
  97. t[1][k]=star[i];
  98. star[i]=k;
  99. muchie[++cate].x=i;
  100. muchie[cate].y=j;
  101. muchie[cate].cost=drum;
  102. k++;
  103. t[0][k]=i;
  104. t[1][k]=star[j];
  105. star[j]=k;
  106. }
  107. }
  108. if(cerinta<=2)
  109. {
  110. for(i=1;i<=n;i++)
  111. {
  112. if(viz[i]==0)
  113. {
  114. componente++;
  115. varf=1,stiva[varf]=i;
  116. conexe[componente]=1;
  117. viz[i]=1;
  118. while(varf)
  119. {
  120. element=stiva[varf];
  121. coloana=star[element];
  122. int ok=0;
  123. while(coloana && ok==0)
  124. {
  125. vecin=t[0][coloana];
  126. if(viz[vecin]==0)
  127. viz[vecin]=1,stiva[++varf]=vecin,ok=1,elicoptere++,conexe[componente]++;
  128. coloana=t[1][coloana];
  129. }
  130. if(ok==0)
  131. varf--;
  132. }
  133. }
  134. }
  135. if(cerinta==1)
  136. g<<elicoptere;
  137. else
  138. {
  139. for(i=1;i<=componente;i++)
  140. afisat+=conexe[i]*(conexe[i]-1)/2;
  141. g<<afisat;
  142. }
  143. }
  144. else
  145. {
  146. for(i=1;i<=n;i++)
  147. arbore[i]=i;
  148. sort(muchie+1,muchie+cate+1,cmp);
  149. for(i=1;i<=cate;i++)
  150. {
  151. a=muchie[i].x;
  152. b=muchie[i].y;
  153. cost=muchie[i].cost;
  154. if(arbore[a]!=arbore[b])
  155. {
  156. rez3+=cost;
  157. mini=min(arbore[a],arbore[b]);
  158. maxi=max(arbore[a],arbore[b]);
  159. for(j=1;j<=n;j++)
  160. if(arbore[j]==maxi)
  161. arbore[j]=mini;
  162.  
  163. }
  164. }
  165. g<<setprecision(6)<<fixed<<rez3;
  166. }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement