编程练习六(最小生成树)

A:Agri-Net(POJ1258)

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 15626 Accepted: 6327
Description
Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course.
Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.
The distance between any two farms will not exceed 100,000.
Input
The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.
Output
For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.
Sample Input
4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
Sample Output
28
题目大意:给定一个N村庄和一个N^2的邻接矩阵表示每两个村庄间的距离,求将这N个点联通所需的最小电线长度

我的代码

//prim算法
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 1000000000;
const int maxn = 100;
int N;
int dist[105][105];
int mindist[105];
int inode;
int tempdist;
int sum;
int main() {
   // freopen("input.txt", "r", stdin);
    while(scanf("%d",&N)!=EOF) {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                scanf("%d",&dist[i][j]);
            }
        }
        //ini
        for (int i = 0; i < N; i++) {
            mindist[i] = dist[0][i];
        }
        sum = 0;
        for (int i = 0; i < N - 1;i++){
            tempdist = 100005;
            //find nearest node
            for (int j = 0 ;j < N; j++) {
                if (mindist[j] && mindist[j] < tempdist) {
                    tempdist = mindist[j];
                    inode = j;
                }
            }
            sum += tempdist;
            mindist[inode] = 0;

            //renew
            for (int i = 0; i < N; i++) {
                if (dist[inode][i] < mindist[i]) {
                    mindist[i] = dist[inode][i];
                }
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}

B:Arctic Network(POJ2349)

摘自:http://blog.csdn.net/lyhvoyage/article/details/19930551
Time Limit: 2000MS Memory Limit: 65536K

Description

The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication technologies are to be used in establishing the network: every outpost will have a radio transceiver and some outposts will in addition have a satellite channel.
Any two outposts with a satellite channel can communicate via the satellite, regardless of their location. Otherwise, two outposts can communicate by radio only if the distance between them does not exceed D, which depends of the power of the transceivers. Higher power yields higher D but costs more. Due to purchasing and maintenance considerations, the transceivers at the outposts must be identical; that is, the value of D is the same for every pair of outposts.

Your job is to determine the minimum D required for the transceivers. There must be at least one communication path (direct or indirect) between every pair of outposts.

Input

The first line of input contains N, the number of test cases. The first line of each test case contains 1 <= S <= 100, the number of satellite channels, and S < P <= 500, the number of outposts. P lines follow, giving the (x,y) coordinates of each outpost in km (coordinates are integers between 0 and 10,000).

Output

For each case, output should consist of a single line giving the minimum D required to connect the network. Output should be specified to 2 decimal points.
Sample Input
1
2 4
0 100
0 300
0 600
150 750
Sample Output
212.13

题意:有S颗卫星和P个哨所,有卫星的两个哨所之间可以任意通信;否则,一个哨所只能和距离它小于等于D的哨所通信。给出卫星的数量和P个哨所的坐标,求D的最小值。
分析:这是一个最小生成树问题。P个哨所最多用P-1条边即可连起来,而S颗卫星可以代替S-1条边,基于贪心思想,代替的边越长,求得的D就越小。所以可以用一个数组保存加入最小生成树的边的长度,共有P-1条边,把前S-1条较长的边代替掉,剩下的边中最长的即为所求,即d[(P-1) - (S-1) - 1] = d[P-S-1]。

我的代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
int T,n,m;
int sal,p;
int i,j,k;
struct Node{
    int x,y;
};
struct edge{
    int u,v;
    double w;
};
Node po[505];
edge ed[300000];
int father[505];
double  dis[505*505];
double distance(int a, int b){
    return sqrt(double((po[a].x-po[b].x)*(po[a].x-po[b].x)+(po[a].y-po[b].y)*(po[a].y-po[b].y)));
}

bool comp(edge a,edge b){
    return a.w < b.w;
}

void init(){
    for (i = 0; i < p; i++) {
        father[i] = i;
    }
}
int find(int a){
    if (father[a]!= a) {
        return find(father[a]);
    }else{
        return a;
    }
}
void merge(int a, int b){
    int a1 = find(a);
    int a2 = find(b);
    if (a1 > a2) {
        father[a2] = a1;
    }else{
        father[a1] = a2;
    }
}
void kruskal(int m){
    int temp = 0;
    for (i = 0; i < m; i++) {
        if (find(ed[i].u) != find(ed[i].v)) {
            merge(ed[i].u,ed[i].v);
            dis[temp++]= ed[i].w;
            if (temp == p-1) {
                return;
            }

        }
    }
}
int main() {
  // freopen("input.txt", "r", stdin);
    scanf("%d",&T);
    while(T--) {
        scanf("%d%d",&sal,&p);
        init();
        for (i = 0; i < p; i++) {
            scanf("%d%d",&po[i].x,&po[i].y);
        }

        m = 0;
        for (i = 0; i < p ; i++) {
            for (j = i+1; j < p; j++) {
                ed[m].u = i;
                ed[m].v = j;
                ed[m].w = distance(i,j);
                m++;
            }
        }
        if (m >= 1) {
            sort(ed,ed+m ,comp);
        }
        kruskal(m);
        printf("%.2f\n",dis[p-1-sal]);
//        for (i = 0; i < p-1; i++) {
//            printf("%.2f\n",dis[i]);
//        }
    }
    return 0;
}

你可能感兴趣的:(问题求解)