急求克鲁斯卡尔的算法代码(java实现的) 如题,谢谢了!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 #include <iostream>#include <queue>using namespace std;const long Max=101;struct node{ long from; long to; long cost;};int set[Max];priority_queue<node> q;bool operator <(const node &a, const node &b){ return a.cost>b.cost;}long find(long x){ long i=x,r=x,j; while(set[r]!=r) r=set[r]; while(i!=r) { j=set[i]; set[i]=r; i=j; } return r;}void merge(long a,long b){ long x,y; x=find(a); y=find(b); if(x!=y) set[x]=y;}long Kruskal(){ long rs=0,i; node e; for(i=0;i<Max;i++) set[i]=i; while(!q.empty()) { e=q.top(); q.pop(); if(find(e.from)!=find(e.to)) { merge(e.from,e.to); rs+=e.cost; } } return rs;}int main(){ /**//* while(!q.empty()) //清空 { q.pop(); } while(m--) //边数 { scanf("%ld%ld%ld",&e.from,&e.to,&e.cost); q.push(e); swap(e.from,e.to);//无向图 q.push(e); } rs=Kruskal(); printf("%ld\n",rs); } return 0;*/} C++很好实现,资料很多。Java就难求了~ 关于编写线程 java教科书中哪一章讲序列化问题 Scanner的疑惑 高手进来请教个问题 系统托盘加速键,如何写 求java混洧工具 想借鉴一下仁兄的画图程序,类似于windows的画板或Image. 加载图象的问题!!!!求你了,告诉我吧!!! 在dll中怎样改变参数值(实现c++中的引用) java协会通知: 新申请了一个邮件列表,大家可以加入交流,地址: 请教一个java字符串处理的问题 html如何解析javascript
#include <queue>
using namespace std;
const long Max=101;
struct node
{
long from;
long to;
long cost;
};
int set[Max];
priority_queue<node> q;
bool operator <(const node &a, const node &b)
{
return a.cost>b.cost;
}long find(long x)
{
long i=x,r=x,j;
while(set[r]!=r)
r=set[r];
while(i!=r)
{
j=set[i];
set[i]=r;
i=j;
}
return r;
}
void merge(long a,long b)
{
long x,y;
x=find(a);
y=find(b);
if(x!=y)
set[x]=y;
}long Kruskal()
{
long rs=0,i;
node e;
for(i=0;i<Max;i++)
set[i]=i;
while(!q.empty())
{
e=q.top();
q.pop();
if(find(e.from)!=find(e.to))
{
merge(e.from,e.to);
rs+=e.cost;
}
}
return rs;
}
int main()
{
/**//*
while(!q.empty()) //清空
{
q.pop();
}
while(m--) //边数
{
scanf("%ld%ld%ld",&e.from,&e.to,&e.cost);
q.push(e);
swap(e.from,e.to);//无向图
q.push(e);
}
rs=Kruskal();
printf("%ld\n",rs);
}
return 0;*/
}