HDU Problem 1875 畅通工程再续 【最小生成树Prim】

来源:互联网 发布:电脑怎么摆脱网络控制 编辑:程序博客网 时间:2024/04/17 02:21

畅通工程再续

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 24033    Accepted Submission(s): 7761


Problem Description
相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现。现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题,政府决定实现百岛湖的全畅通!经过考察小组RPRush对百岛湖的情况充分了解后,决定在符合条件的小岛间建上桥,所谓符合条件,就是2个小岛之间的距离不能小于10米,也不能大于1000米。当然,为了节省资金,只要求实现任意2个小岛之间有路通即可。其中桥的价格为 100元/米。
 

Input
输入包括多组数据。输入首先包括一个整数T(T <= 200),代表有T组数据。
每组数据首先是一个整数C(C <= 100),代表小岛的个数,接下来是C组坐标,代表每个小岛的坐标,这些坐标都是 0 <= x, y <= 1000的整数。
 

Output
每组输入数据输出一行,代表建桥的最小花费,结果保留一位小数。如果无法实现工程以达到全部畅通,输出”oh!”.
 

Sample Input
2210 1020 2031 12 21000 1000
 

Sample Output
1414.2oh!
 

Author
8600
 

Source
2008浙大研究生复试热身赛(2)——全真模拟
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  1879 1301 1162 3371 1198 
 

同样的最小生成树,但是这个对数据有要求,要是两个岛不能相连,用一个最大值标记两个岛之间的距离,检查答案是否大于这个最大值,如果大于这个值,就说明这条边必须要用到,这时候路就建不成了。
#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>#define MAXN 10005using namespace std;const int INF = 1e9;struct node{    double x, y;}loc[MAXN];int n, m, sum = 0;bool vis[MAXN];double cost[MAXN][MAXN], mincost[MAXN];double prim() {    double ans = 0; memset(vis, false, sizeof(vis));    for (int i = 0; i <= n; i++) mincost[i] = INF;    mincost[0] = 0; sum = 0;    while (true) {        int v = -1;        for (int u = 0; u < n; u++) {            if (!vis[u] && (v == -1 || mincost[v] > mincost[u]))                v = u;        }        if (v == -1) break;        vis[v] = true; ans += mincost[v];        for (int u = 0; u < n; u++) {            mincost[u] = min(mincost[u], cost[u][v]);        }    }    return ans;}double dis(node x, node y) {    return sqrt((x.x - y.x)*(x.x - y.x) +(x.y - y.y)*(x.y - y.y));}int main() {    int t; scanf("%d", &t);    while (t--) {        scanf("%d", &n);        for (int i = 0; i < n; i++) {            scanf("%lf%lf", &loc[i].x, &loc[i].y);        }        for (int i = 0; i < n; i++) {            for (int j = 0; j < n; j++) {                double tem = dis(loc[i], loc[j]);                //用最大值标记                if (tem > 1000 || tem < 10) cost[i][j] = INF;                else cost[i][j] = tem;            }        }        double ans = prim();         if (ans < INF) printf("%.1lf\n", ans*100);        else printf("oh!\n");    }    return 0;}


0 0
原创粉丝点击