1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
#include <iostream> #include <map> #include <cmath> #include <string> #include <vector> #include <cstring> #define INF 1000000000 #define EP 1e-6 using namespace std; struct point{ int x; int y; }; struct DD{ int i,j; double cst,edge; DD(double cst,double edge,int i,int j):cst(cst),edge(edge),i(i),j(j){ } bool operator < (const DD& d )const{ if(abs(cst-d.cst)>=EP) return cst<d.cst; if(abs(edge-d.edge)>=EP) return edge>d.edge; if(i!=d.i) return i>d.i; if(j!=d.j) return j>d.j; return false; } }; vector<DD> vb; double gr[55][55]; double road[55][55]; point P[55]; int main() { int N,M; int a,b; while(cin>>N) { cin>>M; if(N+M==0) break; for(int i=1;i<=N;i++) { cin>>a>>b; P[i].x = a; P[i].y = b; } for(int i=1;i<=N;i++) { for(int j=1;j<=N;j++) { if(i==j) { gr[i][j]=0; road[i][j]=0; } else { gr[i][j]=INF; road[i][j]=INF; } } } for(int i=1;i<=M;i++) { cin>>a>>b; double d = (P[a].x-P[b].x)*(P[a].x-P[b].x)+(P[a].y-P[b].y)*(P[a].y-P[b].y); d = sqrt(d); road[a][b]=1; road[b][a]=1; gr[a][b]=d; gr[b][a]=d; } for(int k=1;k<=N;k++) { for(int i=1;i<=N;i++) { for(int j=1;j<=N;j++) { if(gr[i][j]>gr[i][k]+gr[k][j]) gr[i][j] = gr[i][k]+gr[k][j]; } } }//end of warshall double cost; double tempedge; DD ans(0,0,0,0); for(int i=1;i<=N;i++) { for(int j=1;j<=N;j++) { if(road[i][j]==1) continue; cost = 0; tempedge = sqrt((P[i].x-P[j].x)*(P[i].x-P[j].x) + (P[i].y-P[j].y)*(P[i].y-P[j].y)); for(int u=1;u<=N;u++) { for(int v=1;v<=N;v++) { cost+= (gr[u][v]- min(gr[u][v],min(gr[u][i]+tempedge+gr[j][v],gr[v][i]+tempedge+gr[j][u]))); } } DD dd = DD(cost,tempedge,i,j); ans = max(ans,dd); } } if(ans.cst-1>EP) cout<<ans.i<<" "<<ans.j<<"\n"; else cout<<"No road required\n"; } return 0; } |