一些最小生成树的(水)题

题目总结:
HDU1102
HDU2647
POJ1251
HDU1875
POJ1287
HDU1232
HDU1863
正题:
HDU1102
Problem Description
There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected.

We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.

Input
The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j.

Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.

Output
You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum.

Sample Input
3
0 990 692
990 0 179
692 179 0
1
1 2

Sample Output
179

kruskal算法:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdlib>

using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define inf 0x3f3f3f3f
#define ll long long
int n,ave[105][105],pre[105],num,nums,ant;
struct mmp
{
    int a,b,c;
    mmp(){}
    mmp(int x,int y,int z){
        a = x;b = y,c = z;
    }
}ak[10005];
void init()
{
    for(int i = 1;i <= n ;i++)
        for(int j = 1;j <= n;j++)
        cin>>ave[i][j];
    for(int i = 1;i <= n;i++) pre[i] = i;
}
int find(int k)
{
    while(pre[k] != k){
        k = pre[k];
    }
    return k;
}
bool cmp(mmp a,mmp b)
{
    return a.c<b.c;
}
void change()
{   int k = 0;
    for(int i = 1;i <=n ;i++){
        for(int j = i+1;j <= n;j++){
            ak[k++] =mmp(i,j,ave[i][j]);
        }
    }
    sort(ak,ak+k,cmp);
    nums = k;
}
void kruskal()
{
    for(int i = 0;i < nums;i++){
        int x = find(ak[i].a);
        int y = find(ak[i].b);
        if(x != y){
            pre[x] = y;
            ant += ak[i].c;
            num++;
            if(num == n-1) break;
        }
    }
}
int main()
{   while(~scanf("%d",&n)){
        init();
        int q,a,b;
        num = 0;
        scanf("%d",&q);
        while(q--){
            scanf("%d%d",&a,&b);
            int x = find(a);
            int y = find(b);
            if(x != y) num++;
            pre[x] = y;
        }
        change();
        ant = 0;
        kruskal();
        cout<<ant<<endl;
    }
return 0;
}

这题还有另一个思路,我们只需要将这些修好的路的花费置0,然后,
按照一般的 Kruskal算法就可以得到正确答案。

HDU2647
Problem Description
Dandelion’s uncle is a boss of a factory. As the spring festival is coming , he wants to distribute rewards to his workers. Now he has a trouble about how to distribute the rewards.
The workers will compare their rewards ,and some one may have demands of the distributing of rewards ,just like a’s reward should more than b’s.Dandelion’s unclue wants to fulfill all the demands, of course ,he wants to use the least money.Every work’s reward will be at least 888 , because it’s a lucky number.

Input
One line with two integers n and m ,stands for the number of works and the number of demands .(n<=10000,m<=20000)
then m lines ,each line contains two integers a and b ,stands for a’s reward should be more than b’s.

Output
For every case ,print the least money dandelion ‘s uncle needs to distribute .If it’s impossible to fulfill all the works’ demands ,print -1.

Sample Input
2 1
1 2
2 2
1 2
2 1

Sample Output
1777
-1

这不是一道最小生成树的题,这题是拓扑排序。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdlib>

using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define inf 0x3f3f3f3f
#define ll long long
int n,m,num,ak[10005],akki;
struct mmp
{
    int in,out,s; //in为入度的数量,out为出度的数量,s是判断是否被判断过
    vector<int> a;
}ave[10005];
stack<int> q;

int main()
{   int a,b,flag;
    while(cin>>n>>m){
        akki = 0;
        memset(ak,0,sizeof(ak));//一定要记得初始化
        memset(ave,0,sizeof(ave));//原来还能这样
        for(int i = 1;i <= m;i++){
            cin>>a>>b;
            ave[b].out++;
            ave[a].in++;
            ave[b].a.push_back(a);
        }
        num = 0;
        while(num < n){
            flag = 0;
            for(int i = 1;i <= n;i++){
                if(ave[i].s) continue;
                if( ave[i].in == 0) {
                    q.push(i);
                    ak[akki] ++;
                    ave[i].s = 1;
                }
                else flag++;
            }
            akki++;
            if(flag == n - num) {
                flag = -1;break;
            }
            while(!q.empty()){
                int x = q.top();
                q.pop();
                for(int i = 0;i < ave[x].a.size();i++){
                    int p = ave[x].a[i];
                    ave[p].in--;
                }
                ave[x].a.clear();//这句话可以不用写,前面的memset就把这clear了。
                num++;
            }
        }
        if(flag == -1) {
            cout<<-1<<endl;
        }
        else {
            int ans = 0;
            for(int i = 0;i < akki;i++){

                ans += ak[i] * i;
            }
            ans += n * 888;
            cout<<ans<<endl;
        }
    }
return 0;
}

POJ1251

The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road network is too expensive to maintain. The Council of Elders must choose to stop maintaining some roads. The map above on the left shows all the roads in use now and the cost in aacms per month to maintain them. Of course there needs to be some way to get between all the villages on maintained roads, even if the route is not as short as before. The Chief Elder would like to tell the Council of Elders what would be the smallest amount they could spend in aacms per month to maintain roads that would connect all the villages. The villages are labeled A through I in the maps above. The map on the right shows the roads that could be maintained most cheaply, for 216 aacms per month. Your task is to write a program that will solve such problems.

Input
The input consists of one to 100 data sets, followed by a final line containing only 0. Each data set starts with a line containing only a number n, which is the number of villages, 1 < n < 27, and the villages are labeled with the first n letters of the alphabet, capitalized. Each data set is completed with n-1 lines that start with village labels in alphabetical order. There is no line for the last village. Each line for a village starts with the village label followed by a number, k, of roads from this village to villages with labels later in the alphabet. If k is greater than 0, the line continues with data for each of the k roads. The data for each road is the village label for the other end of the road followed by the monthly maintenance cost in aacms for the road. Maintenance costs will be positive integers less than 100. All data fields in the row are separated by single blanks. The road network will always allow travel between all the villages. The network will never have more than 75 roads. No village will have more than 15 roads going to other villages (before or after in the alphabet). In the sample input below, the first data set goes with the map above.
Output
The output is one integer per line for each data set: the minimum cost in aacms per month to maintain a road system that connect all the villages. Caution: A brute force solution that examines every possible set of roads will not finish within the one minute time limit.
Sample Input
9
A 2 B 12 I 25
B 3 C 10 H 40 I 8
C 2 D 18 G 55
D 1 E 44
E 2 F 60 G 38
F 0
G 1 H 35
H 1 I 35
3
A 2 B 10 C 40
B 1 C 20
0
Sample Output
216
30

#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdlib>

using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define inf 0x3f3f3f3f
#define ll long long
int n,pre[30];
struct mmp
{
    int start,end,val;
}ave[1005];
bool cmp(mmp a,mmp b)
{
    return a.val<b.val;
}
int find(int k)
{
    while(pre[k] != k){
        k = pre[k];
    }
    return k;
}
int main()
{   IOS;
    while(cin>>n&&n){
        for(int i = 1;i <= 28;i++) pre[i] = i;
        int a,b,c,d,num,nums;
        char s;
        num = 0;
        for(int i = 1;i < n;i++){
            cin>>s>>b;
            a = s - 'A'+1;
            for(int j = 1;j <= b;j++){
                cin>>s>>d;
                c = s - 'A'+1;
                ave[num].start = a;
                ave[num].end = c;
                ave[num++].val = d;
            }
        }
        sort(ave,ave+num,cmp);
        int ans = 0;
        nums = 0;
        for(int i = 0;i < num;i++){
            int x = find(ave[i].start);
            int y = find(ave[i].end);
            if(x != y){
                ans += ave[i].val;
                nums++;
                pre[x] = y;
                if(nums == n-1) break;
            }
        }
        cout<<ans<<endl;
    }
}

HDU1875
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
2
2
10 10
20 20
3
1 1
2 2
1000 1000

Sample Output
1414.2
oh!

Author
8600

#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdlib>

using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define inf 0x3f3f3f3f
#define ll long long
int n;
double ave[105][105],dis[105],ant,x[105],y[105];

int main()
{   IOS;
    int t;
    cin>>t;
    while(t--){
        cin>>n;
//        memset(ave,inf,sizeof(ave));
        for(int i = 1;i <= n;i++){
            cin>>x[i]>>y[i];
        }
        for(int i = 1;i <= n;i++){
            for(int j = i;j <= n;j++){
                double sum = (x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
                sum = sqrt(sum);
                if(sum < 10||sum > 1000){
                    ave[i][j] = ave[j][i] = inf;
                }
                else {
                    ave[i][j] = ave[j][i] = sum;
                }
            }
        }
        for(int i = 1;i <= n;i++){
            dis[i] = ave[1][i];
        }
        dis[1] = 0,ant = 0;
        int num = 1;
        while(num < n ){
            int k;
            double ans = inf;
            for(int i = 1;i <= n;i++){
                if(dis[i] && ans > dis[i]){
                    ans = dis[i];
                    k = i;
                }
            }
            if(ans == inf) {
                ant = -1;break;
            }
            ant += dis[k];
            dis[k] = 0;
            for(int i = 1;i <= n;i++){
                if(dis[i] && ave[k][i] < dis[i]) dis[i] = ave[k][i];
            }
            num++;
        }
        if(ant == -1) cout<<"oh!"<<endl;
        else {
                printf("%.1lf\n",ant*100);
        }
    }
return 0;
}

同理题目:
POJ1287
HDU1232
HDU1863

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值