Skip to content

Instantly share code, notes, and snippets.

@lackofdream
Created April 18, 2015 01:31
Show Gist options
  • Select an option

  • Save lackofdream/53b027fe7cb81366fca2 to your computer and use it in GitHub Desktop.

Select an option

Save lackofdream/53b027fe7cb81366fca2 to your computer and use it in GitHub Desktop.
HDU1233
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <vector>
#include <fstream>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <string>
#define MAX(a,b) (a>b?a:b)
#define sd(x) scanf("%d",&x)
#define sdd(a,b) scanf("%d%d",&a,&b)
#define sddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define slflf(x,y) scanf("%lf%lf",&x,&y)
#define abs(x) (x<0?(-x):x)
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
using namespace std;
const int dsj_lenth = 105;
int dsj[dsj_lenth];
int graph[dsj_lenth][dsj_lenth];
void init()
{
for (int i=0; i<dsj_lenth; i++) dsj[i]=i;
}
int Find(int x)
{
return dsj[x]==x?x:dsj[x]=Find(dsj[x]);
}
inline void Union(int a,int b)
{
dsj[a]=b;
}
struct road
{
int u,v,w;
} r[dsj_lenth*dsj_lenth];
bool cmp(road a,road b)
{
return a.w<b.w;
}
int main()
{
int n;
while(~sd(n) && n)
{
init();
int cnt = n*(n-1)/2;
for (int i=0;i< cnt;i++)
{
sd(r[i].u);
sd(r[i].v);
sd(r[i].w);
}
sort(r,r+cnt,cmp);
int ans=0,built=0;
for (int i=0;i<cnt;i++)
{
int fa = Find(r[i].u),fb=Find(r[i].v);
if (fa!=fb)
{
ans+=r[i].w;
built++;
Union(fa,fb);
}
if (built==n-1) break;
}
cout << ans << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment