当前位置:网站首页>DS图—最小生成树
DS图—最小生成树
2022-07-21 16:26:00 【奔跑的星黛露】
题目描述
根据输入创建无向网。分别用Prim算法和Kruskal算法构建最小生成树。(假设:输入数据的最小生成树唯一。)
输入
顶点数n
n个顶点
边数m
m条边信息,格式为:顶点1顶点2权值
Prim算法的起点v
输出
输出最小生成树的权值之和
对两种算法,按树的生长顺序,输出边信息(Kruskal中边顶点按数组序号升序输出)
样例输入
6
v1 v2 v3 v4 v5 v6
10
v1 v2 6
v1 v3 1
v1 v4 5
v2 v3 5
v2 v5 3
v3 v4 5
v3 v5 6
v3 v6 4
v4 v6 2
v5 v6 6
v1
样例输出
15
prim:
v1 v3 1
v3 v6 4
v6 v4 2
v3 v2 5
v2 v5 3
kruskal:
v1 v3 1
v4 v6 2
v2 v5 3
v3 v6 4
v2 v3 5
代码
#include "bits/stdc++.h"
using namespace std;
typedef pair<int,int> PII;
const int maxn=1010,INF=0x3f3f3f3f;
int n,m,node[maxn][maxn],dis[maxn],vis[maxn],sum;
map<string,int> mp;
map<int,string> ins;
struct NODE{
int val,a,b;
bool operator<(const NODE &rp) const{
if(val==rp.val) {
return a<rp.a;
}
return val>rp.val;
}
};
priority_queue<NODE> q;
vector<NODE> ans;
void init(){
sum=0;
memset(dis,0x3f,sizeof dis);
memset(vis,0,sizeof vis);
}
void prim(int st){
q.push({0,0,st});
dis[st]=0;
while (!q.empty()){
auto it=q.top();
q.pop();
int v=it.val,a=it.a,b=it.b;
if(vis[b]) continue;
sum+=v;
vis[b]=1;
if(a) ans.push_back({a,b,v});
for(int i=1;i<=n;i++){
if(!vis[i] && dis[i]>node[b][i]){
dis[i]=node[b][i];
q.push({dis[i],b,i});
}
}
}
cout<<sum<<endl;
}
priority_queue<NODE> kq;
int f[maxn];
void kinit(){
sum=0;
for(int i=1;i<=n;i++){
f[i]=i;
}
memset(vis,0,sizeof vis);
}
int getfa(int x){
if(f[x]==x) return x;
return f[x]= getfa(f[x]);
}
void kruskal(){
int qwe=1;
while(qwe<n){
auto it=kq.top();
kq.pop();
int v=it.val,a=it.a,b=it.b;
int fa= getfa(a),fb= getfa(b);
if(fa==fb) continue;
qwe++;
cout<<ins[a]<<" "<<ins[b]<<" "<<v<<endl;
f[fa]=fb;
sum+=v;
}
// cout<<sum<<endl;
}
int main(){
// freopen("123.in","r",stdin);
cin>>n;
for(int i=1;i<=n;i++){
string s;
cin>>s;
mp[s]=i;
ins[i]=s;
}
cin>>m;
memset(node,0x3f,sizeof node);
for(int i=1;i<=m;i++){
string a,b;
int x,y,c;
cin>>a>>b>>c;
x=mp[a],y=mp[b];
node[x][y]=node[y][x]=c;
if(x<y) kq.push({c,x,y});
else kq.push({c,y,x});
}
string start;
cin>>start;
int st=mp[start];
init();
prim(st);
cout<<"prim:"<<endl;
for(auto it:ans){
cout<<ins[it.val]<<" "<<ins[it.a]<<" "<<it.b<<endl;
}
kinit();
cout<<"kruskal:"<<endl;
kruskal();
return 0;
}
边栏推荐
- 出入栈顺序的讨论及合法性
- 着手社区建设掌握的两个概念
- 跟我读论文丨Multi-Model Text Recognition Network
- 华泰证券手机开户安全吗,会被泄露信息吗
- 迪拜推出国家元宇宙战略
- Let me show you eight fallacies in software design
- 15. 谈谈这几个常见的多线程面试题
- 20天能拿下PMP吗?
- Read the paper with me - multi model text recognition network
- tsconfig. JSON cannot find any input in the configuration file. What should I do?
猜你喜欢
Dynamics crm: role of non event dependencies in form
Multithreading foundation of concurrent programming
微信小程序 wx.request的简单封装
lombok的注解@Accessors
看完这个,还不会DVMA,请你吃瓜
Read the paper with me - multi model text recognition network
uniapp开发的微信小程序如何上传至微信小程序平台-完整简单步骤
控制台字体怎么改为console?
关键字const、volatile与指针的使用;汇编语言与寄存器状态的查看
Android-SQLite数据库实战
随机推荐
Dynamics crm: add process sessions to the navigation of the form to view the running history of the workflow
tsconfig. JSON cannot find any input in the configuration file. What should I do?
迪拜推出国家元宇宙战略
Huawei cloud koomessage is a new marketing weapon in the hot public beta
控制台字体怎么改为console?
Wechat payment native (I) preparation and related knowledge
【案例设计】事件分发器 — 实现跨类的事件响应思路分享与实现
关键字const、volatile与指针的使用;汇编语言与寄存器状态的查看
Architect growth: when it comes to architecture, what is it
Dynamics crm: how to use query string parameters
How to change the console font to console?
跟我读论文丨Multi-Model Text Recognition Network
Dynamics crm: encountered "the plug-in execution failed because no sandbox hosts are currently available“
15. 谈谈这几个常见的多线程面试题
pytorch 32 onnx模型每次输出的结果都会变的解决方案
SQL行转列、列转行
【解决】UnityWebRequest 下请求 EXCEL数据 返回 PK 结果的解决方案
微信小程序 wx.request的简单封装
Is it safe for Huatai to open an account online? Is it a trap
Comment changer la police de la console en console?