Advertisement
farsid

Untitled

Aug 5th, 2012
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. #define filer() freopen("in.txt","r",stdin)
  2. #define filew() freopen("out.txt","w",stdout)
  3. #define SET(a, x) memset((a), (x), sizeof(a))
  4. #define i64 long long
  5. #include<iostream>
  6. #include<stdio.h>
  7. #include<string.h>
  8. #include<math.h>
  9. #include<algorithm>
  10. #include<queue>
  11. #include<stack>
  12. #include<vector>
  13. #include <map>
  14. #define INF 1<<29
  15.  
  16. double EPS=1e-7;
  17.  
  18. using namespace std;
  19.  
  20. struct NODE
  21. {
  22. int r,c;
  23. double d,sum;
  24. };
  25.  
  26. NODE PQ[2600];
  27. struct POINT
  28. {
  29. double x;
  30. double y;
  31. };
  32.  
  33. bool vst[55][55];
  34. double W[55][55];
  35. double D[55][55];
  36. double dist[55][55];
  37.  
  38. POINT node[55];
  39.  
  40. double compute(int u,int v)
  41. {
  42. //if(dist[u][v]!=0)return dist[u][v];
  43. //if(dist[v][u]!=0)return dist[v][u];
  44. double a,b,c;
  45.  
  46. a=(node[u].x-node[v].x)*(node[u].x-node[v].x);
  47. b=(node[u].y-node[v].y)*(node[u].y-node[v].y);
  48. c=a+b;
  49. c=sqrt(c);
  50. return c;
  51. }
  52.  
  53. bool cmp(NODE a,NODE b)
  54. {
  55. if(fabs(a.sum-b.sum)<EPS && fabs(a.d-b.d)<EPS && a.r==b.r)return a.c<b.c;
  56. if(fabs(a.sum-b.sum)<EPS && fabs(a.d-b.d)<EPS)return a.r<b.r;
  57. if(fabs(a.sum-b.sum)<EPS)return (a.d-b.d)<EPS;
  58. return (a.sum-b.sum)>EPS;
  59. }
  60.  
  61. /*bool cmp(NODE a,NODE b)
  62. {
  63. //if(a.sum==b.sum && a.d==b.d && a.r==b.r)return a.c<b.c;
  64. if(fabs(a.sum-b.sum)<EPS && fabs(a.d-b.d)<EPS)return (a.r+a.c)<(b.r+b.c);
  65. if(fabs(a.sum-b.sum)<EPS)return a.d<b.d;
  66. return a.sum>b.sum;
  67. }*/
  68.  
  69. /*bool cmp(NODE a,NODE b)
  70. {
  71. if(a.sum==b.sum && a.d==b.d && a.r==b.r)return a.c<b.c;
  72. if(a.sum==b.sum && a.d==b.d)return a.r<b.r;
  73. if(a.sum==b.sum)return a.d<b.d;
  74. return a.sum>b.sum;
  75. }*/
  76.  
  77. int main()
  78. {
  79. //filer();
  80. int T,cas=0,i,j,u,v,k,N,M;
  81. POINT A;
  82. while(scanf("%d%d",&N,&M)==2)
  83. {
  84. if(N==0 && M==0)break;
  85. // node.clear();
  86. for(i=0;i<=51;i++)
  87. {
  88. for(j=0;j<=51;j++)
  89. {
  90. vst[i][j]=0;
  91. if(i!=j){
  92. W[i][j]=INF;
  93. D[i][j]=INF;}
  94. else
  95. {
  96. W[i][j]=0;
  97. D[i][j]=0;
  98. }
  99. dist[i][j]=0;
  100. }
  101. }
  102.  
  103. for(i=1;i<=N;i++)
  104. {
  105. scanf("%lf%lf",&A.x,&A.y);
  106. node[i]=A;
  107. }
  108.  
  109. for(i=1;i<=N;i++)
  110. {
  111. for(j=1;j<=N;j++)dist[i][j]=compute(i,j);
  112. }
  113.  
  114. for(i=1;i<=M;i++)
  115. {
  116. scanf("%d%d",&u,&v);
  117. W[u][v]=dist[u][v];
  118. W[v][u]=W[u][v];
  119. D[u][v]=W[u][v];
  120. D[v][u]=W[u][v];
  121. vst[u][v]=1;
  122. vst[v][u]=1;
  123. }
  124.  
  125. for(k=1;k<=N;k++)
  126. {
  127. for(i=1;i<=N;i++)
  128. {
  129. for(j=1;j<=N;j++)
  130. {
  131. D[i][j]=min(D[i][j],D[i][k]+D[k][j]);
  132. }
  133. }
  134. }
  135. NODE S;
  136. k=0;
  137. for(u=1;u<=N;u++)
  138. {
  139. for(v=1;v<=N;v++)
  140. {
  141. if(!vst[u][v] )
  142. {
  143. S.r=u;
  144. S.c=v;
  145. S.d=dist[i][j];
  146. S.sum=0;
  147. for(i=1;i<=N;i++)
  148. {
  149. for(j=1;j<=N;j++)
  150. {
  151. double z=min(D[i][j],D[i][u]+dist[u][v]+D[v][j]);
  152. z=min(z,D[i][v]+dist[v][u]+D[u][j]);
  153. S.sum+=(D[i][j]-z);
  154. }
  155. }
  156.  
  157. PQ[k++]=S;
  158. }
  159. }
  160. }
  161. sort(PQ,PQ+k,cmp);
  162. if(PQ[0].sum<=1.0)printf("No road required\n");
  163. else printf("%d %d\n",PQ[0].r,PQ[0].c);
  164. }
  165. return 0;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement