您的位置:首页 > 编程语言 > C语言/C++

G - QS Network

2013-04-20 14:16 106 查看
G -QS Network

读懂题意,就都是是水题

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <algorithm>
#define N 1001
const int maxn=(N*N-N) / 2 + 1;
using namespace std;
struct edge
{
int x, y, value;
}edge[maxn];
int n, len,  fa
;

bool cmp(struct edge a, struct edge b)
{
return a.value < b.value;
}

void input()
{
int price
, i, j, temp;
int k=1;
scanf("%d", &n);
len = (n*n - n) / 2;
for(i=1; i<=n; i++)
scanf("%d", &price[i]);
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
{
scanf("%d", &temp);
if(i<j)
{
edge[k].x = i;
edge[k].y = j;
edge[k].value = price[i] + price[j] + temp;
k++;
}
}
}

int find(int x)
{
/*	while(x != fa[x])
x = fa[x];
return x;*/
return x==fa[x]?x:find(fa[x]);
}

void KRUSKAL()
{
int ans=0;
for(int i=1; i<=n; i++)
fa[i] = i;
sort(edge+1, edge+len+1, cmp);

for(int i=1; i<=len; i++)
{
int x1 = find( edge[i].x );
int x2 = find( edge[i].y );
if(x1 != x2)
{
fa[x1] = x2;
ans += edge[i].value;
}
}
printf("%d\n", ans);
}

int main()
{
int t;
scanf("%d", &t);
while(t--)
{
input();
KRUSKAL();
}
return 0;
}




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  学习笔记 hut 图论 c++