POJ - 2349 Arctic Network(連通圖)

題目連接:

  https://vjudge.ppsucxtt.cn/problem/POJ-2349ios

思路:

   先將全部點之間距離處理出來,再跑一遍prim,注意跑prim時用res數組記錄加入邊的權值,由於m個衛星通道可使m-1個連通圖連通,因此將res數組sort一下, 刪去m-1條最長的邊後,此時res最長的邊即爲所求最長接收距離。

代碼:

#include <iostream>
#include <queue>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <iomanip>
#define fastio ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define debug(a) cout << "debug : " << (#a) << " = " << a << endl

using namespace std;

typedef long long ll;
typedef pair<ll, ll> PII;

const int N = 1010;
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
const int mod = 998244353;

double a[N][N];
PII b[N];
vector<double> res;
double dis[N];
bool vis[N];
int n, m;

void prim()
{

    for (int i = 0; i < N; i++)
        dis[i] = INF;
    memset(vis, false, sizeof vis);
    for (int i = 1; i <= n; i++)
    {
        int t = -1;
        for (int j = 1; j <= n; j++)
        {
            if (vis[j])
                continue;
            if (t == -1 || dis[j] < dis[t])
                t = j;
        }
        if (t != 1)
            res.push_back(dis[t]);
        vis[t] = true;
        for (int j = 1; j <= n; j++)
            dis[j] = min(dis[j], a[t][j]);
    }
}

int main()
{
    fastio;
    int T;
    cin >> T;
    while (T--)
    {
        cin >> m >> n;
        for (int i = 1; i <= n; i++)
            cin >> b[i].first >> b[i].second;
        for (int i = 1; i <= n; i++)
            for (int j = i + 1; j <= n; j++)
                a[i][j] = a[j][i] = sqrt(pow(b[i].first - b[j].first, 2) + pow(b[i].second - b[j].second, 2));
        prim();
        sort(res.begin(), res.end());
        int idx = res.size() - 1;
        if (idx - (m - 1) < 0)
            cout << 0 << endl;
        else
            cout << fixed << setprecision(2) << res[idx - (m - 1)] << endl;
        res.clear();
    }
    return 0;
}
相關文章
相關標籤/搜索