hdu 1233
来源:互联网 发布:葛宝荣淘宝客 编辑:程序博客网 时间:2024/06/05 09:51
克鲁斯卡尔算法 使用并查集检测是否构成回路决定边是否能够加入:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int part[100];
struct node//克鲁斯卡尔算法是加边法,使用node结构表示边信息,而不是prim的数组
{
int from;
int to;
int cost;
};
vector<node> Vec;
int getFa(int n)
{
if(part[n]==n)return n;
part[n]=getFa(part[n]);
return part[n];
}
bool merge(int x,int y)
{
if(getFa(x)==getFa(y))return false;
part[getFa(y)]=x;return true;
}
bool cmp(node x,node y)
{
return x.cost<y.cost;
}
int main()
{
int n;
while(cin>>n&&n)
{
Vec.clear();
for (int i=1;i<=n;i++)part[i]=i;
int from,to,cost;
int m=n*(n-1)/2;
node temp;
while(m--)
{
cin>>from>>to>>cost;
temp.from=from;
temp.to=to;
temp.cost=cost;
Vec.push_back(temp);
}
sort(Vec.begin(),Vec.end(),cmp);//一开始傻到用vec作为迭代器,汗
int count=n-1;
int sum=0;
for(vector<node>::iterator iter=Vec.begin();iter!=Vec.end();iter++)
if(merge(iter->from,iter->to))
{
sum+=iter->cost;
count--;
if(!count)break;
}
cout<<sum<<endl;
}
return 1;
}
- hdu 1233
- hdu 1233
- hdu 1233
- HDU 1233
- hdu 1233
- hdu-1233
- hdu 1233
- HDU 1233
- HDU 1233
- HDU 1233
- HDU 1233
- HDU 1233
- hdu 1233
- HDU 1233
- hdu 1233
- hdu 1233 kruskal
- HDU-1233(prim写法)
- HDU 1233 Prim
- CT的DICOM图象常用标签
- 前方
- 编译DCMTK
- JavaScript 路径中空格%20
- SNMP协议介绍及SNMP library
- hdu 1233
- 子字符串问题
- 终于用上破解的Delphi 2010
- 搭建一个大型网站架构的实验环境(Squid缓存服务器篇)
- 架构设计--起点
- 各位虎年好!帮忙填份有关网络购物的问卷!感激不尽!
- OFFIS DCMTK 3.5.4公开发行的产品 - DICOM标准
- Medical Image Format FAQ - Part 8(1)
- DM网络芯片ping不通原因调查。