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

用java的牛人们看看为什么会RE?

Posted by MasterLuo at 2009-04-05 23:50:11 on Problem 3723
import java.util.*;
import java.io.*;

class Path {
    public int x, y, cost;
    public Path(int x, int y, int cost) {
        this.x = x;
        this.y = y;
        this.cost = cost;
    }
}
class MyComparator implements Comparator<Path> {
    public int compare(Path p1, Path p2) {
        return p2.cost - p1.cost;
    }
}
class GetAns {
    PriorityQueue<Path> p;
    int total;
    int[] head;
    public GetAns(int total, PriorityQueue<Path> p) {
        this.total = total;
        this.p = p;
        head = new int[total];
    }
    public void getAC() {
        for(int i = 0; i < head.length; ++i)
            head[i] = i;
        int allCost = 0;
        while(true) {
            Path path = p.poll();
            if(path == null)
                break;
            int x = path.x;
            int y = path.y;
            int cost = path.cost;
            while(head[x] != x) {
                x = head[x];
            }
            while(head[y] != y) {
                y = head[y];
            }
            if(x != y) {
                allCost += cost;
                head[y] = x;
            }
        }
        System.out.println(total * 10000 - allCost);
    }
    
}
public class Main {
    
    public static void main(String[] args) {
        try {
            BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
            int test = Integer.valueOf(buff.readLine());
            for(int t = 0; t < test; ++t) {
                buff.readLine();
                StringTokenizer st = new StringTokenizer(buff.readLine());
                int n = Integer.valueOf(st.nextToken());
                int m = Integer.valueOf(st.nextToken());
                int r = Integer.valueOf(st.nextToken());
                PriorityQueue<Path> pri = new PriorityQueue<Path>(r, new MyComparator());;
                for(int i = 0; i < r; ++i) {
                    st = new StringTokenizer(buff.readLine());
                    int x = Integer.valueOf(st.nextToken());
                    int y = Integer.valueOf(st.nextToken()) + n;
                    int cost = Integer.valueOf(st.nextToken());
                    pri.add(new Path(x, y, cost));
                }
                GetAns getAns = new GetAns(m + n, pri);
                getAns.getAC();
            }
        } catch(IOException i) {
            //System.exit(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