HDU 1875 畅通工程再续

来源:互联网 发布:唐晓琳 知乎 编辑:程序博客网 时间:2024/06/05 18:01

题目描述:

相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现。现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题,政府决定实现百岛湖的全畅通!经过考察小组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!

解题思路:

在最小生成树的基础上, 题目增加的条件是对于两点之间的距离必须在10-1000之间。 所以在计算两点间距离时判断如果不符合条件的话将距离设置成inf。 对于判断是否能生成最小生成树只要判断每次加进来的点到离他最近的点是否小于inf即可。

代码:

#include <iostream>#include <sstream>#include <cstdio>#include <algorithm>#include <cstring>#include <iomanip>#include <utility>#include <string>#include <cmath>#include <vector>#include <bitset>#include <stack>#include <queue>#include <deque>#include <map>#include <set>using namespace std;/*tools: *ios::sync_with_stdio(false); *freopen("input.txt", "r", stdin); */typedef long long ll;typedef unsigned long long ull;const int dir[5][2] = {0, 1, 0, -1, 1, 0, -1, 0, 0, 0};const ll ll_inf = 0x7fffffff;const int inf = 0x3f3f3f;const int mod = 1000000;const int Max = (int) 110;int n;struct node {    int x;    int y;}Map[Max];double dis[Max][Max];bool vis[Max];double Dis(node tmp1, node tmp2) {    int x1 = tmp1.x, y1 = tmp1.y;    int x2 = tmp2.x, y2 = tmp2.y;    return sqrt(1.0 * (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));}void Prim() {    double d[Max], ans = 0;    for (int i = 1; i <= n; ++i) {        d[i] = dis[1][i];    }    vis[1] = 1;    for (int i = 1; i < n; ++i) {        double Min = inf;        int mark;        for (int j = 1; j <= n; ++j) {            if (!vis[j] && d[j] < Min) {                Min = d[mark = j];            }        }        // failed        if (Min == inf) {            cout << "oh!" << endl;            return ;        }        vis[mark] = 1;        ans += Min;        for (int j = 1; j <= n; ++j) {            if (!vis[j] && d[j] > dis[mark][j]) {                d[j] = dis[mark][j];            }        }    }    cout << fixed << setprecision(1) << ans * 100 << endl;}int main() {    //freopen("input.txt", "r", stdin);    ios::sync_with_stdio(false);    int t;    cin >> t;    while (t--) {        cin >> n;        for (int i = 1; i <= n; ++i) {            cin >> Map[i].x >> Map[i].y;            vis[i] = 0;        }        for (int i = 1; i <= n; ++i) {            for (int j = 1; j <= n; ++j) {                double temp =  Dis(Map[i], Map[j]);                if (temp > 1000 || temp < 10) temp = inf;                dis[i][j] = temp;            }        }        Prim();    }    return 0;}

原创粉丝点击