标签:namespace 转换 cut iat bre rtu ssi ges div
Input
Output
Sample Input
2 0 0 3 4 3 17 4 19 4 18 5 0
Sample Output
Scenario #1 Frog Distance = 5.000 Scenario #2 Frog Distance = 1.414
题目大意:有两只青蛙a,b,求青蛙a在能跳到青蛙b的所有路径上最长边的最小值。
思路:对于给定的坐标我们可以将之转换成一个距离矩阵,距离由两点间距离公式求出来,之后用一遍最短路就OK啦(不过在松弛的时候是求最长边的最小值)
1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<iomanip> 5 #include<vector> 6 #include<queue> 7 #include<cmath> 8 9 using namespace std; 10 const int INF = 900000000; 11 struct point { 12 int x, y; 13 }e[220]; 14 int n, vis[220], f[220]; 15 double mp[220][220], dis[220]; 16 void SPFA(int s) 17 { 18 for (int i = 1; i <= n; i++) { 19 dis[i] = INF; 20 vis[i] = 0; f[i] = 0; 21 } 22 queue<int>Q; 23 dis[s] = 0; vis[s] = 1; f[s]++; 24 Q.push(s); 25 while (!Q.empty()) { 26 int t = Q.front(); Q.pop(); 27 vis[t] = 0; 28 for (int i = 1; i <= n; i++) { 29 if (dis[i] > max(dis[t], mp[t][i])) { 30 dis[i] = max(dis[t], mp[t][i]); 31 if (!vis[i]) { 32 vis[i] = 1; 33 Q.push(i); 34 if (++f[i] > n)return; 35 } 36 } 37 } 38 } 39 } 40 int main() 41 { 42 ios::sync_with_stdio(false); 43 int T = 1; 44 while ((cin >> n)) { 45 if (n == 0)break; 46 for (int i = 1; i <= n; i++)cin >> e[i].x >> e[i].y; 47 if (n == 2) { 48 double dis = sqrt((e[1].x - e[2].x)*(e[1].x - e[2].x) + (e[1].y - e[2].y)*(e[1].y - e[2].y)); 49 cout << "Scenario #" << T++ << endl; 50 cout << "Frog Distance = " << fixed << setprecision(3) << dis << endl << endl; 51 continue; 52 } 53 for (int i = 1; i <= n; i++) 54 for (int j = 1; j <= i; j++)//求出ij之间的距离转换成距离矩阵 55 mp[i][j] = mp[j][i] = sqrt((double)((e[i].x - e[j].x)*(e[i].x - e[j].x)) + (double)((e[i].y - e[j].y)*(e[i].y - e[j].y))); 56 SPFA(1); 57 cout << "Scenario #" << T++ << endl; 58 cout << "Frog Distance = " << fixed << setprecision(3) << dis[2] << endl << endl;//行末两个换行!!!!! 59 } 60 61 return 0; 62 }
标签:namespace 转换 cut iat bre rtu ssi ges div
原文地址:https://www.cnblogs.com/wangrunhu/p/9496702.html