64,282
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
#include<cstdlib>
using namespace std;
typedef struct
{
int x, y;
int w;
}edge;
const int MAX = 101;
const int MAXN = 10001;
edge e[MAXN];
int ans, m;
int height[101];
int pa[101];
void make(int x)
{
pa[x] = x;
height[x] = 0;
}
int find(int x)
{
if(x != pa[x])
pa[x] = find(pa[x]);
return pa[x];
}
void merge(int x, int y, int w)
{
x = find(x);
y = find(y);
ans += w;
if(height[x] == height[y])
{
pa[x] = y;
height[x]++;
}
else if(height[x] > height[y])
{
pa[y] = x;
}
else
{
pa[x] = y;
}
}
bool judge()
{
for(int i = 2; i <= m; i++)
if(pa[i] != pa[1])
return 0;
return 1;
}
int cmp(const void *a, const void *b)
{
return ((edge *)a) -> w - ((edge *)b) -> w;
}
int main()
{
int n, x, y, i, ans;
while(cin >> n >> m, n){
for(i = 1; i <= m; i++)
make(i);
for(i = 0; i < n; i++)
cin >> e[i].x >> e[i].y >> e[i].w;
qsort(e, n, sizeof(e[0]), cmp);
ans = 0;
for(i = 0; i < n; i++)
{
x = find(e[i].x);
y = find(e[i].y);
if(x != y)
merge(x, y, e[i].w);
}
for(i = 1; i <= m; i++)
find(i);
if(judge())
cout << ans << endl;
else
puts("?");
}
return 0;
}
#include<iostream>
#include<cstdlib>
using namespace std;
typedef struct
{
int x, y;
int w;
}edge;
const int MAX = 100;
const int MAXN = 10001;
edge e[MAXN];
int ans = 0, m;
int height[100];
int pa[100];
void make(int x)
{
pa[x] = x;
height[x] = 0;
}
int find(int x)
{
if(x != pa[x])
pa[x] = find(pa[x]);
return pa[x];
}
void merge(int x, int y, int w)
{
x = find(x);
y = find(y);
ans += w;
if(height[x] == height[y])
{
pa[x] = y;
height[x]++;
}
else if(height[x] > height[y])
{
pa[y] = x;
}
else
{
pa[x] = y;
}
}
bool judge()
{
for(int i = 2; i <= m; i++)
if(pa[i] != pa[1])
return 0;
return 1;
}
int cmp(const void *a, const void *b)
{
return ((edge *)a) -> w - ((edge *)b) -> w;
}
int main()
{
int n, x, y, i;
while(cin >> n >> m, n){
for(i = 1; i <= m; i++)
make(i);
for(i = 0; i < n; i++)
cin >> e[i].x >> e[i].y >> e[i].w;
qsort(e, n, sizeof(e[0]), cmp);
for(i = 0; i < n; i++)
{
x = find(e[i].x);
y = find(e[i].y);
if(x != y)
merge(x, y, e[i].w);
}
for(i = 1; i <= m; i++)
find(i);
if(judge())
cout << ans << endl;
else
puts("?");
}
return 0;
}
#include<iostream>
#include<cstdlib>
using namespace std;
typedef struct
{
int x, y;
int w;
}edge;
const int MAX = 101;
const int MAXN = 10001;
edge e[MAXN];
int ans = 0, m;//在这儿对ans初始化 定义一个全局变量
int height[101];
int pa[101];
void make(int x)
{
pa[x] = x;
height[x] = 0;
}
int find(int x)
{
if(x != pa[x])
pa[x] = find(pa[x]);
return pa[x];
}
void merge(int x, int y, int w)
{
x = find(x);
y = find(y);
ans += w;
if(height[x] == height[y])
{
pa[x] = y;
height[x]++;
}
else if(height[x] > height[y])
{
pa[y] = x;
}
else
{
pa[x] = y;
}
}
bool judge()
{
for(int i = 2; i <= m; i++)
if(pa[i] != pa[1])
return 0;
return 1;
}
int cmp(const void *a, const void *b)
{
return ((edge *)a) -> w - ((edge *)b) -> w;
}
int main()
{
int n, x, y, i;//这儿不要在定义ans的局部变量
while(cin >> n >> m, n){
for(i = 1; i <= m; i++)
make(i);
for(i = 0; i < n; i++)
cin >> e[i].x >> e[i].y >> e[i].w;
qsort(e, n, sizeof(e[0]), cmp);
//ans = 0;不需要了
for(i = 0; i < n; i++)
{
x = find(e[i].x);
y = find(e[i].y);
if(x != y)
{
merge(x, y, e[i].w);
}
}
for(i = 1; i <= m; i++)
find(i);
if(judge())
cout << ans << endl;
else
puts("?");
}
return 0;
}
#include<iostream>
#include<cstdlib>
using namespace std;
typedef struct
{
int x, y;
int w;
}edge;
const int MAX = 101; //不够 const int MAX = 100;
//多了 const int MAXN = 10001;
edge e[MAX]; //edge e[MAXN];
int ans = 0, m;
//int height[100];
int pa[MAX]; //int pa[100];
void make(int x)
{
pa[x] = x;
//height[x] = 0;
}
int find(int x)
{
/*
if(x != pa[x])
pa[x] = find(pa[x]);
return pa[x];
*/
int y = x;
while (y != pa[y]) y = pa[y];
return y;
}
void merge(int x, int y, int w)
{
//x = find(x);
//y = find(y);
ans += w;
/*
if(height[x] == height[y])
{
pa[x] = y;
height[x]++;
}
else if(height[x] > height[y])
*/
if (x < y)
{
pa[y] = x;
}
else
{
pa[x] = y;
}
}
/*
bool judge()
{
for(int i = 2; i <= m; i++)
if(pa[i] != pa[1])
return 0;
return 1;
}
*/
int cmp(const void *a, const void *b)
{
return ((edge *)a) -> w - ((edge *)b) -> w;
}
int main()
{
int n, x, y, i;
while(cin >> n >> m, n){
int count;
ans = 0; //对每个case都要清ans
for(i = 1; i <= m; i++)
make(i);
for(i = 0; i < n; i++)
cin >> e[i].x >> e[i].y >> e[i].w;
qsort(e, n, sizeof(e[0]), cmp);
for(i = 0, count = 1; count < m && i < n; i++)
{
x = find(e[i].x);
y = find(e[i].y);
if(x != y)
{
count++;
merge(x, y, e[i].w);
}
}
/*
for(i = 1; i <= m; i++)
find(i);*/
if (count == m) //if(judge())
cout << ans << endl;
else
puts("?");
}
return 0;
}