1. 程式人生 > >HDU 4081 Peach Blossom Spring (最小生成樹+dfs)

HDU 4081 Peach Blossom Spring (最小生成樹+dfs)

clear put max 最大 ios light define algo puts

題意:給定一個 n 個點和相應的權值,要求你用 n-1 條邊連接起來,其中一條邊是魔法邊,不用任何費用,其他的邊是長度,求該魔法邊的兩端的權值與其他邊費用的盡量大。

析:先求出最小生成樹,然後再枚舉每一條邊,求出最大值,任意兩點之間的距離可以通過預處理來解決,最小生成樹時,要用prime算法,要不然可能會超時。

代碼如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e3 + 10;
const LL mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
    return r > 0 && r <= n && c > 0 && c <= m;
}

int cost[maxn][maxn];
int lowc[maxn];
int x[maxn], y[maxn], z[maxn];
bool vis[maxn];
int pre[maxn];
int dp[maxn][maxn];

vector<int> G[maxn];

void add(int u, int v){
  G[u].push_back(v);
  G[v].push_back(u);
}

int dist(int i, int j){
  return ((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
}

double solve(){
  double res = 0;
  memset(vis, 0, sizeof vis);
  memset(pre, -1, sizeof pre);
  vis[0] = 1;
  int p;
  int minc;
  int pr = 0;
  for(int i = 1; i < n; ++i)  lowc[i] = cost[0][i];

  for(int i = 1; i < n; ++i){
    minc = INF;   p = -1;
    for(int j = 0; j < n; ++j)
      if(!vis[j] && minc > lowc[j]){
        minc = lowc[j];
        p = j;
      }
    for(int j = 0; j < n; ++j)  if(vis[j]){
      if(minc == cost[p][j]){
        pre[p] = j;  break;
      }
    }

    res += sqrt(minc);  vis[p] = 1;
    for(int j = 0; j < n; ++j)
      if(!vis[j] && lowc[j] > cost[p][j])
        lowc[j] = cost[p][j];

  }
  return res;
}

void dfs(int u, int fa, int rt, int mmax){
  for(int i = 0; i < G[u].size(); ++i){
    int v = G[u][i];
    if(v == fa)  continue;
    dp[rt][v] = max(mmax, cost[u][v]);
    dfs(v, u, rt, max(mmax, cost[u][v]));
  }
}

int main(){
  int T;  cin >> T;
  while(T--){
    scanf("%d", &n);
    for(int i = 0; i < n; ++i){
      scanf("%d %d %d", x+i, y+i, z+i);
      G[i].clear();
    }

    for(int i = 0; i < n; ++i)
      for(int j = i+1; j < n; ++j)
        cost[i][j] = cost[j][i] = dist(i, j);

    double sum = solve();
    for(int i = 0; i < n; ++i){
        if(pre[i] == -1)  continue;
        add(i, pre[i]);
    }

    memset(dp, 0, sizeof dp);
    for(int i = 0; i < n; ++i)  dfs(i, -1, i, 0);
    double ans = 0.0;
    for(int i = 0; i < n; ++i)
      for(int j = i+1; j < n; ++j){
        double x = z[i] + z[j];
        double t = sum - sqrt(dp[i][j]);
        double xxx = x / t;
        ans = max(ans, xxx);
      }
    printf("%.2f\n", ans);
  }
  return 0;
}

  

HDU 4081 Peach Blossom Spring (最小生成樹+dfs)