标签:cin table iss fir ase define contain 初始 bool
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 30571 | Accepted: 9220 |
Description
Input
Output
Sample Input
1 2 4 0 100 0 300 0 600 150 750
Sample Output
212.13
Source
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <iostream> #include <algorithm> #include <iostream> #include<cstdio> #include<string> #include<cstring> #include <stdio.h> #include <queue> #include <string.h> #include <vector> #include <map> #define ME(x , y) memset(x , y , sizeof(x)) #define SF(n) scanf("%d" , &n) #define rep(i , n) for(int i = 0 ; i < n ; i ++) #define INF 0x3f3f3f3f #define mod 1024 using namespace std; typedef long long ll ; int s , n ; double ma[609][609]; double dis[609] ; double d[609]; int vis[609] ; int ans ; struct node{ double x , y ; }a[609]; void prim() { for(int i = 1 ; i <= n ; i++) { dis[i] = ma[1][i]; } vis[1] = 1 ; for(int i = 1 ; i < n ; i++) { double min = INF ; int pos ; for(int j = 1 ; j <= n ; j++) { if(!vis[j] && dis[j] < min) { min = dis[j]; pos = j ; } } vis[pos] = 1 ; d[ans++] = min ; for(int j = 1 ; j <= n ; j++) { if(!vis[j] && dis[j] > ma[pos][j]) { dis[j] = ma[pos][j]; } } } } bool cmp(double a , double b) { return a > b ; } void init() { memset(vis , 0 , sizeof(vis)); for(int i = 1 ; i <= n ; i++) { for(int j = 1 ; j <= n ; j++) { ma[i][j] = INF; } } ans = 0 ; } int main() { int t ; scanf("%d" , &t); while(t--) { scanf("%d%d" , &s , &n); init(); for(int i = 1 ; i <= n ; i++) { scanf("%lf%lf" , &a[i].x , &a[i].y); } for(int i = 1 ; i <= n ; i++) { for(int j = i + 1 ; j <= n ; j++) { double w ; w = sqrt((a[i].y - a[j].y) * (a[i].y - a[j].y) + (a[i].x - a[j].x)*(a[i].x - a[j].x)) ; ma[i][j] = ma[j][i] = min(ma[i][j] , w);//这里if判断大小时,注意方向不要搞错了 } } prim(); sort(d , d + n - 1 , cmp); printf("%.2f\n" , d[s-1]); } return 0 ; }
Kruskal
#include <string.h> #include <iostream> #include <stdio.h> #include <algorithm> #include <math.h> using namespace std; int fa[20000] , ans , n , m , way ; double ds[500000] ; struct Node { int from , to ; int x ; int y ; double d; }a[500000]; bool cmp(const Node &a , const Node &b) { return a.d < b.d ; } int gcd(int x) { if(x == fa[x]) return x ; else return gcd(fa[x]); } void unite(int x , int y) { x = gcd(x) ; y = gcd(y) ; if(x > y) fa[x] = y ; else fa[y] = x ; } void init() { for(int i = 1 ; i <= m ; i++) fa[i] = i ; ans = 0 ; way = 0 ; memset(ds , 0 , sizeof(ds)); } bool cmp1(double a , double b) { return a > b ; } int main() { int t ; cin >> t ; while(t--) { cin >> n >> m ; init(); for(int i = 1 ; i <= m ; i++) { cin >> a[i].x >> a[i].y ; } for(int i = 1 ; i <= m ; i++) { for(int j = i + 1 ; j <= m ; j++) { a[way].from = i ; a[way].to = j ; a[way].d = sqrt(pow((double)(a[i].x - a[j].x) , 2) + pow((double)(a[i].y - a[j].y) , 2)); way++; } } sort(a , a + way , cmp) ; for(int i = 0 ; i < way ; i++) { if( ans == m - 1) break ; if(gcd(fa[a[i].from]) != gcd(fa[a[i].to])) { unite(a[i].from , a[i].to); ds[ans] = a[i].d; ans ++ ; } } sort(ds , ds + ans , cmp1); printf("%.2f\n" , ds[n-1]); } return 0; }
标签:cin table iss fir ase define contain 初始 bool
原文地址:https://www.cnblogs.com/nonames/p/11366403.html