码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-07-26 20:26:29      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:efi   memset   处理   com   cto   return   cpp   algo   最大   

题意:给定一个 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)

标签:efi   memset   处理   com   cto   return   cpp   algo   最大   

原文地址:http://www.cnblogs.com/dwtfukgv/p/7241521.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!