zoukankan      html  css  js  c++  java
  • POJ2349 Arctic Network

    原题链接

    先随便找一棵最小生成树,然后贪心的从大到小选择边,使其没有贡献。
    显然固定生成树最长边的一个端点安装卫星频道后,从大到小选择边的一个端点作为卫星频道即可将该边的贡献去除。
    所以最后的答案就是最小生成树上第(m)长的边。

    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    using namespace std;
    const int N = 510;
    struct cod {
    	int x, y;
    };
    cod a[N];
    int n;
    double eg[N][N], dis[N];
    bool v[N];
    inline int re()
    {
    	int x = 0;
    	char c = getchar();
    	bool p = 0;
    	for (; c < '0' || c > '9'; c = getchar())
    		p |= c == '-';
    	for (; c >= '0' && c <= '9'; c = getchar())
    		x = x * 10 + c - '0';
    	return p ? -x : x;
    }
    inline double minn(double x, double y)
    {
    	return x < y ? x : y;
    }
    void prim()
    {
    	int i, j, x;
    	memset(dis, 65, sizeof(dis));
    	memset(v, 0, sizeof(v));
    	dis[1] = 0;
    	for (i = 1; i <= n; i++)
    	{
    		x = 0;
    		for (j = 1; j <= n; j++)
    			if (!v[j] && (!x || dis[j] < dis[x]))
    				x = j;
    		if (!x)
    			break;
    		v[x] = 1;
    		for (j = 1; j <= n; j++)
    			if (!v[j])
    				dis[j] = minn(dis[j], eg[x][j]);
    	}
    }
    int main()
    {
    	int i, j, m, t;
    	t = re();
    	while (t--)
    	{
    		m = re();
    		n = re();
    		for (i = 1; i <= n; i++)
    		{
    			a[i].x = re();
    			a[i].y = re();
    		}
    		for (i = 1; i < n; i++)
    			for (j = i + 1; j <= n; j++)
    				eg[j][i] = eg[i][j] = sqrt(1.0 * (a[i].x - a[j].x) * (a[i].x - a[j].x) + 1.0 * (a[i].y - a[j].y) * (a[i].y - a[j].y));
    		prim();
    		sort(dis + 1, dis + n + 1);
    		printf("%.2f
    ", dis[n - m + 1]);
    	}
    	return 0;
    }
    
  • 相关阅读:
    Java并发编程:synchronized
    对一致性Hash算法,Java代码实现的深入研究
    在Xcode中使用Git进行源码版本控制
    这些 Git 技能够你用一年了
    简明 Git 命令速查表
    Git 分支管理是一门艺术
    2016年Web前端面试题目汇总
    iOS 面试基础题目
    Json数据交互格式介绍和比较
    SpringBoot端口和访问路径
  • 原文地址:https://www.cnblogs.com/Iowa-Battleship/p/9696580.html
Copyright © 2011-2022 走看看