UVa1151 Buy or Build

77 篇文章 0 订阅

World Wide Networks (WWN) is a leading company that operates large telecommunication networks.
WWN would like to setup a new network in Borduria, a nice country that recently managed to get rid
of its military dictator Kurvi-Tasch and which is now seeking for investments of international companies
(for a complete description of Borduria, have a look to the following Tintin albums “King Ottokar’s
Sceptre”, “The Calculus Affair” and “Tintin and the Picaros”). You are requested to help WWN
todecide how to setup its network for a minimal total cost.
There are several local companies running small networks (called subnetworks in the following) that
partially cover the n largest cities of Borduria. WWN would like to setup a network that connects all
n cities. To achieve this, it can either build edges between cities from scratch or it can buy one or
several subnetworks from local companies. You are requested to help WWN to decide how to setup its
network for a minimal total cost.
• All n cities are located by their two-dimensional Cartesian coordinates.
• There are q existing subnetworks. If q ≥ 1 then each subnetwork c (1 ≤ c ≤ q) is defined by a set
of interconnected cities (the exact shape of a subnetwork is not relevant to our problem).
• A subnetwork c can be bought for a total cost wc and it cannot be split (i.e., the network cannot
be fractioned).
• To connect two cities that are not connected through the subnetworks bought, WWN has to build
an edge whose cost is exactly the square of the Euclidean distance between the cities.
You have to decide which existing networks you buy and which edges you setup so that the total
cost is minimal. Note that the number of existing networks is always very small (typically smaller than
8).
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases
following, each of them as described below. This line is followed by a blank line, and there is also a
blank line between two consecutive inputs.
Each test case is described by one input file that contains all the relevant data. The first line
contains the number n of cities in the country (1 ≤ n ≤ 1000) followed by the number q of existing
subnetworks (0 ≤ q ≤ 8). Cities are identified by a unique integer value ranging from 1 to n. The
first line is followed by q lines (one per subnetwork), all of them following the same pattern: The first
integer is the number of cities in the subnetwork. The second integer is the the cost of the subnetwork
(not greater than 2 × 106
). The remaining integers on the line (as many as the number of cities in the
subnetwork) are the identifiers of the cities in the subnetwork. The last part of the file contains n lines
that provide the coordinates of the cities (city 1 on the first line, city 2 on the second one, etc). Each
line is made of 2 integer values (ranging from 0 to 3000) corresponding to the integer coordinates of
the city.
Output
For each test case, your program has to write the optimal total cost to interconnect all cities.
The outputs of two consecutive cases will be separated by a blank line.
A 115 Cities Instance
Consider a 115 cities instance of the problem with 4 subnetworks (the 4 first graphs in Figure 1).
As mentioned earlier the exact shape of a subnetwork is not relevant still, to keep figures easy to read,
we have assumed an arbitrary tree like structure for each subnetworks. The bottom network in Figure
1 corresponds to the solution in which the first and the third networks have been bought. Thin edges
correspond to edges build from scratch while thick edges are those from one of the initial networks.
Figure 1: A 115 Cities Instance and a Solution (Buying the First and the Third Network)
Sample Explanation:
The sample input instance is shown in Figure 2. An optimal solution is described in Figure 3 (thick
edges come from an existing network while thin edges have been setup from scratch).
Figure 2: The 7 City instance of the sample input
Figure 3
An optimal solution of the 7 City instance in which which the
first and second existing networkshave been bought while two extra
edges (1,5) and (2,4) have been setup
Sample Input
1
7 3
2 4 1 2
3 3 3 6 7
3 9 2 4 5
0 2
4 0
2 0
4 2
1 3
0 5
4 4
Sample Output
17

问题描述:

第1行输入n和q,n是城市数量(编号为1~n),q是方案数。接下来有q行,每行的第1个数为城市数k,第2个数为用该方案需要的代价cost,后面的k个数为城市的编号。接下来n行为第n个城市所在的坐标位置。
在城市间建设道路,使得相互连通。建设道路的花费是两个城市之间的坐标的欧几里德距离;若买方案,则方案内的城市已经连通。求最小花费。

问题分析:

这是一个最小生成树的为问题,解决的算法有Kruskal(克鲁斯卡尔)算法和Prim(普里姆) 算法。

先求一个最小生成树,再枚举方案求最小生成树,求最小值作为最小花费。

程序说明:

本程序使用Kruskal算法实现。有关最小生成树的问题,使用克鲁斯卡尔算法更具有优势,只需要对所有的边进行排序后处理一遍即可。程序中使用了并查集,用来判定加入一条边后会不会产生循环。程序中,图采用边列表的方式存储,排序一下就好了。

这个题与参考链接的题是同一个题,只是输入输出不同。

/* UVALive3505 Buy or Build
 * 我暖着我的玫瑰 不让她枯萎
 * 先做一遍Kursual   然后枚举购买方案这样效率会大大提升
 * */
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;
#define Maxn 1005
const int Q = 8;
vector<int> g[Q + 1];
int cost[Q + 1];
int n,q,fa[Maxn];
inline void UNFint(int n ) {
    for(int i=0; i<=n; i++) fa[i] = i;
}
struct Edge{ int u,v,w; }e[Maxn * (Maxn - 1)];
struct Point { int x,y; }point[Maxn + Maxn];

int Find(int x) {
     if(x == fa[x]) return x;
     else return fa[x] = Find(fa[x]);
}

inline int calc(Point& a,Point& b) {
    int x = a.x - b.x,y = a.y - b.y;
    return x * x + y * y;
}
inline bool cmp(Edge a,Edge b) { return a.w < b.w; }

inline bool Union(int a,int b) {
    a = Find(a),b = Find(b);
    if(a != b) { fa[a] = b; return true; }
    else return false;
}

int Kursual(int m) {
    int Ans = 0,cnt = 0;
    for(int i=0; i<m; i++) {
        if(Union(e[i].u,e[i].v)) {
            Ans += e[i].w;
            if(++cnt == n - 1) break;
        }
    }
    return Ans;
}

int main() {
    int T; cin >> T;
    while(T--) {
        for(int i=0; i<=Q; i++) g[i].clear();
        scanf("%d %d",&n,&q);
        for(int k,i=0; i<q; i++) {
            scanf("%d %d",&k,&cost[i]);// 第i中方案的花费
            for(int x,j=0; j<k; j++) {
                scanf("%d",&x);
                g[i].push_back(x);
            }
        }

        for(int i=0; i<n; i++) scanf("%d %d",&point[i].x,&point[i].y);

        int cnt = 0;
        for(int i=0; i<n; i++) {
            for(int j=i+1; j<n; j++) {
                e[cnt].u = i + 1;
                e[cnt].v = j + 1;
                e[cnt++].w = calc(point[i],point[j]);
            }
        }// 没两个点之间 建边
        sort(e ,e + cnt ,cmp);
        UNFint(n);
        int Ans = Kursual(cnt);
        for(int i=0; i<(1<<q); i++) {
            UNFint(n);
            int w = 0;
            for(int j=0; j<q; j++){
                if((i >> j) & 1) continue;
                w += cost[j];//  我购买了第j中方案
                for(int k=1; k<g[j].size(); k++)
                    Union(g[j][k],g[j][0]);// 将第j中方案里的点连通
            }
            w += Kursual(cnt);
            if(w < Ans) Ans = w;
        }
        cout << Ans << endl;
        if(T) printf("\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七情六欲·

学生党不容易~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值