POJ 2485 highways解题报告(最小生成树)(应用prim和Kruskal两种方法)(c++)

让我先把原题贴上来:
Highways
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 27912 Accepted: 12734
Description

The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They’re planning to build some highways so that it will be possible to drive between any pair of towns without leaving the highway system.

Flatopian towns are numbered from 1 to N. Each highway connects exactly two towns. All highways follow straight lines. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town that is located at the end of both highways.

The Flatopian government wants to minimize the length of the longest highway to be built. However, they want to guarantee that every town is highway-reachable from every other town.
Input

The first line of input is an integer T, which tells how many test cases followed.
The first line of each case is an integer N (3 <= N <= 500), 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, 65536]) between village i and village j. There is an empty line after each test case.
Output

For each test case, you should output a line contains an integer, which is the length of the longest road to be built such that all the villages are connected, and this value is minimum.
Sample Input

1

3
0 990 692
990 0 179
692 179 0
Sample Output

692(我看到的原题给的答案是 692,但我自己手算以及两种方法算的都是871,有点奇怪)
Hint

Huge input,scanf is recommended.
Source

POJ Contest,Author:Mathematica@ZSU
题目大意为:Flatopia岛为促进城市繁荣决定修高速路,这个岛上有n个城市,要求修完路后,各城市之间可以相互到达(可以不必直接到达),且修的总路程最短.
可以看出这是非常明显的最小生成树问题
为了多加练习,笔者在这里用了用了prim和Kruscal两种方法分别解:
首先是prim算法(利用邻接表和优先队列优化)

#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
#define MAX_V 501
using namespace std;
typedef pair<int,int> P;
struct edge{int to,cost;//带权的边要用到结构体表示; 
};
struct cmp{ //自定义cmp比较函数; 
    bool operator()(P a,P b){
        if(a.first==b.first) return a.second>b.second;
        return a.first>b.first;
    }
};
vector<edge>G[MAX_V];//因为用的是邻接表表示图,所以用到vector容器; 
bool used[MAX_V];//判断所有的点(即城市)是否已加入集合; 
int prim(int V,int S){
    priority_queue<P,vector<P>,cmp>que;//声明一个按cmp方式从小到大排列的优先队列; 
    fill(used,used+V+1,false);//一开始所有点都不在集合中,所以均初始化为false; 
    int ans=0,flag=0;//flag用来计算加入集合的点个数 
    que.push(P(0,S));
    while(flag<V){
        P p=que.top();que.pop();
        int v=p.second;
        if(used[v]) continue;//如果队列首部的点已经加入过集合则跳过; 
        used[v]=true;//否则将其加入集合并进行下列操作; 
        ans+=p.first;
        flag++;
        for(int i=0;i<G[v].size();i++){
            edge e=G[v][i];
            if(!used[e.to]) que.push(P(e.cost,e.to));
            //若该点周围有没有加入集合的相邻点,则将其加入优先队列 ; 
        }
    }
    return ans;
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        int n,d;
        edge s,t;
        scanf("%d",&n);
        for(int i=1;i<=n;i++){//输入图形; 
            for(int j=1;j<=n;j++){
                scanf("%d",&d);
                if(i==j) continue;
                s.to=j,s.cost=d;
                t.to=i,t.cost=d;
                G[i].push_back(s);
                G[j].push_back(t);
            }
        }
        printf("%d\n",prim(n,1));
        for(int i=1;i<=n;i++) G[i].clear();//最后一定要清空vector容器; 
    }
}

这里用优先队列进行prim算法优化是《挑战程序设计》一书中所提到的,不过书中并没有相应的代码,在网上也搜索过无果所以只好自己瞎琢磨,不过根据自己写的上面代码,优先队列最多加入E(该题中是N*(N-1))条边,每次加入时更新用时O(log|V|),因此总共用时为O(|E|log|V|)应该没错。

接下来看看Kruskal算法

#include<cstdio>
#include<algorithm>
#define MAX_E 250000
#define MAX_V 501
using namespace std;
struct edge{int fm,to,cost;
};
edge es[MAX_E];//结构体数组存放每条边的信息; 
int par[MAX_V];//用来查询每个顶点是否属于同一连通流量的并查集数组; 
void init(int v){
    for(int i =1;i<=v;i++) par[i]=i;
}
int find(int x){
    if(par[x]==x) return x;
    return par[x]=find(par[x]);
}
bool same(int i,int j){
    return find(i)==find(j);
}
void unite(int i,int j){
    i=find(i),j=find(j);
    par[j]=i;
}
bool cmp(const edge& e1,const edge& e2){//自定义cmp比较函数: 
    return e1.cost<e2.cost;
}
int Kruskal(int n){
    sort(es,es+n*(n-1),cmp);//按es[i].cost的大小由小到大排序; 
    init(n);//初始化并查集
    int ans=0;
    for(int i=0;i<n*(n-1);i++){
        edge e=es[i];
        if(!same(e.fm,e.to)){
            unite(e.fm,e.to);
            ans+=e.cost;
        }
    }
    return ans;
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        int n;
        scanf("%d",&n);
        int d;
        int k=0;
        for(int i=1;i<=n;i++){//输入每条边的信息; 
            for(int j=1;j<=n;j++){
                scanf("%d",&d);
                if(i==j) continue;
                es[k].fm=i,es[k].to=j,es[k].cost=d;
                k++;
            }
        }
        printf("%d\n",Kruskal(n));
    }
}


非常明显,该算法复杂度最高的部分就在于sort函数(快速排序)上,因此时间复杂度为 O(|E|log|E|)(E为边的个数);
可以看出,改进后的prim算法和Kruskal在时间复杂度上差别实在不大,然而后者代码比较容易理解,写起来也简便一些(感觉我自己都不信@_@),在都不会超时的情况下我比较推荐后者(讲道理有一个超时的话另一个也好不到哪去吧……那时可能要用到斐波那契堆?……我还不会QAQ)。
总之,以上就是该题的解题报告,本人新手一个,请各位大佬发现有错的地方能够加以指正,最好能提出一些意见和建议QWQ,也欢迎各位发表评论进行提问讨论。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值