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

1A

Posted by 2742195759 at 2016-03-26 10:48:40 on Problem 3723
#include <stdio.h>
#include <string>
#include <string.h>
#include <queue>
#include <stack>
#include <map>
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#define inf 0x3f3f3f3f
#define mem0(x , y)  memset(x , y , sizeof(x))
#define ll long long
using namespace std;
int sets[30000] ; ///nan + 10000 ;
int find(int x){
    return sets[x] == x ? x : sets[x] = find(sets[x]) ;
}
void unionset(int x,int y){
    int fx = find(x) ; int fy = find(y) ;
    sets[fx] = fy ;
}
struct node{
    int s,e ,w;
    bool operator < (const node & A)const{
        return w < A.w ;
    }
}Edge[100000] ;
int main(){
    ///freopen("1" ,"r" ,stdin) ;
    int T ; scanf("%d",&T );
    while(T --){
        for(int i=0;i<30000;i++) sets[i] = i ;
        int n,m,k ;
        scanf("%d%d%d",&n,&m,&k) ;
        int cnt  =0 ;
        for(int i=0;i<k;i++){
            int ta, tb  ,tc;
            scanf("%d%d%d",&ta,&tb,&tc) ;
            tb += 10000 ;
            Edge[cnt].s = ta ; Edge[cnt].e = tb ; Edge[cnt++].w = 10000 - tc ;
        }
        int ans = 0 ;
        int ct = 0 ;
        sort(Edge , Edge+cnt) ;
        for(int i=0;i<cnt;i++){
            int s = Edge[i].s ;int e = Edge[i].e ;
            if(find(s) != find(e)){
                ans += Edge[i].w ;
                ct ++ ;
                unionset(s , e) ;
            }
        }
        printf("%d\n",10000*(n+m-ct) + ans) ;
    }
}

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