Time Limit: 2000MS | Memory Limit: 65535KB | 64bit IO Format: %I64d & %I64u |
Description
NJUST carries on the tradition of HaJunGong. NJUST, who keeps up the ”people-oriented, harmonious development” of the educational philosophy and develops the ”unity, dedication, truth-seeking, innovation” school motto, has now become an engineering-based, multidisciplinary university.
As we all know, Nanjing is one of the four hottest cities in China. Students in NJUST find it hard to fall asleep during hot summer every year. They will never, however, suffer from that hot this year, which makes them really excited. NJUST’s 60th birthday is approaching, in the meantime, 50 million is spent to install air conditioning among students dormitories. Due to NJUST’s long history, the old circuits are not capable to carry heavy load, so it is necessary to set new high-load wires. To reduce cost, every wire between two dormitory is considered a segment. Now, known about all the location of dormitories and a power plant, and the cost of high-load wire per meter, Tom200 wants to know in advance, under the premise of all dormitories being able to supply electricity, the minimum cost be spent on high-load wires. And this is the minimum strategy. But Tom200 is informed that there are so many wires between two specific dormitories that we cannot set a new high-load wire between these two, otherwise it may have potential risks. The problem is that Tom200 doesn’t know exactly which two dormitories until the setting process is started. So according to the minimum strategy described above, how much cost at most you'll spend?
Input
The first line of the input contains a single integer T(T ≤ 100), the number of test cases.
For each case, the first line contains two integers n(3 ≤ n ≤ 1000), k(1 ≤ k ≤ 100). n represents n-1 dormitories and one power plant, k represents the cost of high-load wire per meter. n lines followed contains two integers x, y(0 ≤ x, y ≤ 10000000), representing the location of dormitory or power plant. Assume no two locations are the same, and no three locations are on a straight line. The first one is always the location of the power plant.
For each case, the first line contains two integers n(3 ≤ n ≤ 1000), k(1 ≤ k ≤ 100). n represents n-1 dormitories and one power plant, k represents the cost of high-load wire per meter. n lines followed contains two integers x, y(0 ≤ x, y ≤ 10000000), representing the location of dormitory or power plant. Assume no two locations are the same, and no three locations are on a straight line. The first one is always the location of the power plant.
Output
For each case, output the cost, correct to two decimal places.
Sample Input
2 4 2 0 0 1 1 2 0 3 1 4 3 0 0 1 1 1 0 0 1
Sample Output
9.66 9.00
本题据说只能用pime,只好去学了下。。。T_T
然后题意嘛。。。有点复杂。。。算了不解释了。。。
反正关于把最小生成树中断开一条边,再把剩下的两棵树用另一条边(最小的)连起来,这样又形成一棵生成树,求这些生成树中的最大值乘以k
dp[u][v]表示断开u, v之间的边后,把剩下的两棵树用另一条边连起来的边的最小值
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
#define MAXN (1000+5)
const double INF = 1e18;
int n, pre[MAXN];
double MST, X[MAXN], Y[MAXN], map[MAXN][MAXN], dis[MAXN], dp[MAXN][MAXN];
vector<int> G[MAXN];
bool vis[MAXN];
void init(){
for(int i = 0; i < MAXN; i++){
G[i].clear();
for(int j = 0; j < MAXN; j++) dp[i][j] = INF;
dp[i][i] = 0;
}
}
double calc(double a, double b){
return sqrt(a*a+b*b);
}
void prime(){
memset(vis, 0, sizeof(vis));
for(int i = 0; i < n; i++) dis[i] = INF;
dis[0] = pre[0] = MST = 0;
int k;
for(int i = 0; i < n; i++){
double Min = INF;
for(int j = 0; j < n; j++)
if(!vis[j] && dis[j] < Min){
Min = dis[j];
k = j;
}
// printf("k = %d Min = %lf\n", k, Min);
vis[k] = true;
for(int j = 0; j < n; j++)
if(!vis[j] && dis[j] > map[k][j]) dis[j] = map[k][j], pre[j] = k;
if(k){
G[k].push_back(pre[k]);
G[pre[k]].push_back(k);
}
MST += Min;
}
}
double dfs(int pos, int u, int fa){
double ret = INF;
for(int i = 0; i < G[u].size(); i++){
int v = G[u][i];
if(v == fa) continue;
double tmp = dfs(pos, v, u);
dp[u][v] = dp[v][u] = min(tmp, dp[u][v]);
ret = min(ret, tmp);
}
if(fa != pos) ret = min(ret, map[pos][u]);
return ret;
}
double dfs_ans(int u, int fa){
double ret = 0;
for(int i = 0; i < G[u].size(); i++){
int v = G[u][i];
if(v == fa) continue;
ret = max(ret, MST-map[u][v]+dp[u][v]);
ret = max(ret, dfs_ans(v, u));
}
return ret;
}
int main(){
freopen("test.in", "r", stdin);
freopen("test.out", "w", stdout);
int T;
scanf("%d", &T);
while(T--){
init();
int k;
scanf("%d%d", &n, &k);
for(int i = 0; i < n; i++){
scanf("%lf%lf", &X[i], &Y[i]);
map[i][i] = 0;
for(int j = 0; j < i; j++) map[i][j] = map[j][i] = calc(X[i]-X[j], Y[i]-Y[j]);
}
prime();
// printf("mst = %lf\n", MST);
for(int i = 0; i < n; i++) dfs(i, i, -1);
double ans = MST;
for(int i = 0; i < G[0].size(); i++){
int v = G[0][i];
ans = max(ans, dfs_ans(v, 0));
}
printf("%.2lf\n", ans*k);
}
return 0;
}
I can do it!