Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Not hard#include <cstdio> #include <cstdlib> #include <iostream> #include <algorithm> #include <cstring> using namespace std; struct NODE{ int from, to, val; }edge[101 * 101]; int n; int num; int ans; int father[101]; const int cmp(NODE p1, NODE p2){ return p1.val < p2.val; } int find(int point){ return point == father[point] ? point : father[point] = find(father[point]); } void find_circle(int number){ int x, y; x = find(edge[number].from); y = find(edge[number].to); if(x != y){ father[x] = y; ans += edge[number].val; } } int main(){ //Filework(); int i, j; int p; while(scanf("%d", &n) != EOF){ ans = 0; num = 1; memset(edge, 0, sizeof(edge)); for(i = 1; i <= n; i ++){ father[i] = i; for(j = 1; j <= n; j ++){ scanf("%d", &p); if(i < j){ edge[num].from = i; edge[num].to = j; edge[num].val = p; num ++; } } } sort(edge + 1, edge + 1 + num, cmp); for(i = 1; i <= num; i ++) find_circle(i); printf("%d\n", ans); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator