标签:style blog http io color os ar java for
2 0 0 3 4 3 17 4 19 4 18 5 0
Scenario #1 Frog Distance = 5.000 Scenario #2 Frog Distance = 1.414
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 210; 18 double dis[maxn][maxn],d[maxn]; 19 bool vis[maxn]; 20 vector<int>g[maxn]; 21 int n,x[maxn],y[maxn],p[maxn]; 22 void prim() { 23 for(int i = 0; i <= n; ++i) { 24 vis[i] = false; 25 d[i] = INF; 26 p[i] = -1; 27 } 28 d[1] = 0; 29 while(true) { 30 double minV = INF; 31 int index = -1; 32 for(int i = 1; i <= n; ++i) 33 if(!vis[i] && d[i] < minV) minV = d[index = i]; 34 if(index == -1 || minV >= INF) break; 35 if(p[index] > -1) { 36 g[p[index]].push_back(index); 37 g[index].push_back(p[index]); 38 } 39 vis[index] = true; 40 for(int i = 1; i <= n; ++i) 41 if(!vis[i] && d[i] > dis[index][i]) { 42 d[i] = dis[index][i]; 43 p[i] = index; 44 } 45 } 46 } 47 double getDis(int i,int j) { 48 double tmp = (x[i] - x[j])*(x[i] - x[j]); 49 tmp += (y[i] - y[j])*(y[i] - y[j]); 50 return sqrt(tmp); 51 } 52 double ans; 53 bool dfs(int u,int fa,double maxV) { 54 if(u == 2) { 55 ans = maxV; 56 return true; 57 } 58 for(int i = g[u].size()-1; i >= 0; --i) { 59 if(g[u][i] == fa) continue; 60 if(dfs(g[u][i],u,max(maxV,dis[u][g[u][i]]))) return true; 61 } 62 return false; 63 } 64 int main() { 65 int cs = 1; 66 while(scanf("%d",&n),n) { 67 for(int i = 0; i <= n; ++i) g[i].clear(); 68 for(int i = 1; i <= n; ++i) 69 scanf("%d %d",x+i,y+i); 70 for(int i = 1; i <= n; ++i) 71 for(int j = 1; j <= n; ++j) 72 dis[i][j] = getDis(i,j); 73 prim(); 74 ans = 0; 75 dfs(1,-1,0); 76 printf("Scenario #%d\nFrog Distance = %.3f\n\n",cs++,ans); 77 } 78 return 0; 79 }
Dijkstra版
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 210; 18 double dis[maxn][maxn],d[maxn]; 19 int n,x[maxn],y[maxn]; 20 bool vis[maxn]; 21 double getDis(int i,int j) { 22 double tmp = (x[i] - x[j])*(x[i] - x[j]); 23 tmp += (y[i] - y[j])*(y[i] - y[j]); 24 return sqrt(tmp); 25 } 26 void dijkstra(){ 27 for(int i = 1; i <= n; ++i){ 28 vis[i] = false; 29 d[i] = INF; 30 } 31 d[1] = 0; 32 while(true){ 33 double minV = INF; 34 int index = -1; 35 for(int i = 1; i <= n; ++i) 36 if(!vis[i] && d[i] < minV) minV = d[index = i]; 37 if(index == -1 || minV >= INF) break; 38 vis[index] = true; 39 for(int i = 1; i <= n; ++i) 40 if(!vis[i] && d[i] > max(d[index],dis[index][i])) 41 d[i] = max(d[index],dis[index][i]); 42 } 43 } 44 int main() { 45 int cs = 1; 46 while(scanf("%d",&n),n) { 47 for(int i = 1; i <= n; ++i) 48 scanf("%d %d",x+i,y+i); 49 for(int i = 1; i <= n; ++i) 50 for(int j = 1; j <= n; ++j) 51 dis[i][j] = getDis(i,j); 52 dijkstra(); 53 printf("Scenario #%d\nFrog Distance = %.3f\n\n",cs++,d[2]); 54 } 55 return 0; 56 }
Floyd版
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 210; 18 double dis[maxn][maxn]; 19 int n,x[maxn],y[maxn]; 20 double getDis(int i,int j) { 21 if(i == j) return 0; 22 double tmp = (x[i] - x[j])*(x[i] - x[j]); 23 tmp += (y[i] - y[j])*(y[i] - y[j]); 24 return sqrt(tmp); 25 } 26 void Floyd(){ 27 for(int k = 1; k <= n; ++k){ 28 for(int i = 1; i <= n; ++i){ 29 for(int j = 1; j <= n; ++j) 30 dis[i][j] = min(dis[i][j],max(dis[i][k],dis[k][j])); 31 } 32 } 33 } 34 int main() { 35 int cs = 1; 36 while(scanf("%d",&n),n) { 37 for(int i = 1; i <= n; ++i) 38 scanf("%d %d",x+i,y+i); 39 for(int i = 1; i <= n; ++i) 40 for(int j = 1; j <= n; ++j) 41 dis[i][j] = getDis(i,j); 42 Floyd(); 43 printf("Scenario #%d\nFrog Distance = %.3f\n\n",cs++,dis[1][2]); 44 } 45 return 0; 46 }
标签:style blog http io color os ar java for
原文地址:http://www.cnblogs.com/crackpotisback/p/4066471.html