HDU——3371 Connect ther Cities(最小生成树问题)

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3371

在这里插入图片描述
样例:

Sample Input
1
6 4 3
1 4 2
2 6 1
2 3 5
3 4 33
2 1 2
2 1 3
3 4 5 6
 

Sample Output
1

题意: 存在着 n n n座幸存城市,给你一些关于城市之间连通的信息,问判断是否能连通所有城市,若能,求连通所有城市需要的最小花费。

Prim算法解题思路: 我们利用邻接矩阵来存储之间的信息,要注意可能会有重边,而根据我们想要连通的花费最小,就必须选择最优的花费,即判断重边。之后对于输入的已经连通的信息,我们将两个城市之间的花费修改为0即可,处理完这些,就按照正常的Prim算法就做此题就好。若你对Prim算法来不太熟悉,这里指路一篇Prim算法详解blog:https://blog.csdn.net/hzf0701/article/details/107927858

Prim算法AC代码:

/*
*邮箱:unique_powerhouse@qq.com
*blog:https://me.csdn.net/hzf0701
*注:文章若有任何问题请私信我或评论区留言,谢谢支持。
*
*/
#include<bits/stdc++.h>	//POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 505;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll>  pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//

int citys[maxn][maxn];
bool vis[maxn];//判断是否并入。
int t,n,m,k;//t组测试用例,n个城市。
int lowcost[maxn],temp[maxn];//表示每个点到集合的最短距离。
int u,v,w;//临时变量。
void Prim(){
	vis[1]=true;//从城市编号为1的出发。
	rep(i,2,n){
		lowcost[i]=citys[1][i];
	}
	int minn,pos,sum=0;
	rep(i,2,n){
		minn=inf;
		rep(j,2,n){
			if(!vis[j]&&lowcost[j]<minn){
				minn=lowcost[j];pos=j;
			}
		}
		if(minn==inf)break;
		vis[pos]=true;
		sum+=minn;
		//更新最短路径。
		rep(j,2,n){
			if(!vis[j]&&lowcost[j]>citys[pos][j])
				lowcost[j]=citys[pos][j];
		}
	}
	bool flag=false;
	rep(i,1,n){
		if(!vis[i]){
			flag=true;
			break;
		}
	}
	if(flag)cout<<"-1"<<endl;
	else cout<<sum<<endl;
}
int main(){
	//freopen("in.txt", "r", stdin);//提交的时候要注释掉
	IOS;
	while(cin>>t){
		while(t--){
			memset(citys,inf,sizeof(citys));
			memset(vis,false,sizeof(vis));
			cin>>n>>m>>k;
			rep(i,0,m-1){
				cin>>u>>v>>w;
				//需要考虑重边呀。
				if(w<citys[u][v])citys[u][v]=citys[v][u]=w;
			}
			int num;
			while(k--){
				cin>>num;
				rep(i,0,num-1)cin>>temp[i];
				rep(i,0,num-2){
					rep(j,i+1,num-1){
						citys[temp[i]][temp[j]]=citys[temp[j]][temp[i]]=0;//由于已经连通,我们更改花费。
					}
				}
			}
			Prim();
		}
	}
	return 0;
}

Kruskal算法解题思路:对于Kruskal算法而言,我们无需考虑重边,因为我们我根据边权值进行排序,同时我们对于已经连通的信息,利用并查集算法合并即可。然后同样是按照模板来写的。若你对Kruskal算法还不太熟悉的话,这里指路一篇blog:https://blog.csdn.net/hzf0701/article/details/107933639

Kruskal算法AC代码:

/*
*邮箱:unique_powerhouse@qq.com
*blog:https://me.csdn.net/hzf0701
*注:文章若有任何问题请私信我或评论区留言,谢谢支持。
*
*/
#include<bits/stdc++.h>    //POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll>  pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//

int t,n,m,k;
struct Edge{
    int u,v;//表示所连的城市编号
    int w;  //表示所连城市所需的钱。
    bool operator <(const Edge &a){
        return w<a.w;
    }
};
Edge edges[maxn];
int father[maxn];//verx[i]代表城市i所属的连通分量。
int u,v,w;//临时变量。
int Find(int x){
	//寻找x的祖先,并压缩路径。
	int r=x;
	while(r!=father[r])
		r=father[r];
	int i=x,j;
	//回溯压缩路径。
	while(father[i]!=r){
		j=father[i];
		father[i]=r;
		i=j;
	}
	return r;
}
void Kruskal(){
    rep(i,1,n)father[i]=i;//初始化。
    sort(edges,edges+m);
    int num,x,y,fx,fy;
    rep(i,1,k){
        cin>>num;
        cin>>x;
        rep(j,0,num-2){
            cin>>y;
            fx=Find(x);fy=Find(y);
            if(fx!=fy)father[fx]=fy;
        }
    }
    int sum=0;
    rep(i,0,m-1){
        fx=Find(edges[i].u); //寻找根节点进行判断。
        fy=Find(edges[i].v);
        if(fx!=fy){
            father[fx]=fy;
            sum+=edges[i].w;//累加。
        }
    }
    int flag=0;
    rep(i,1,n){
        if(father[i]==i)flag++;
    }
    if(flag>1)cout<<"-1"<<endl;
    else cout<<sum<<endl;
}
int main(){
    //freopen("in.txt", "r", stdin);//提交的时候要注释掉
    IOS;
    while(cin>>t){
        while(t--){
            cin>>n>>m>>k;
            rep(i,0,m-1){
                cin>>u>>v>>w;
                edges[i].u=u;edges[i].v=v;edges[i].w=w;
            }
            Kruskal();
        }
    }
    return 0;
}

posted @   unique_pursuit  阅读(18)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
点击右上角即可分享
微信分享提示