Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Not hard

Posted by Ronny_Chan at 2016-08-11 09:07:57 on Problem 1258
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator