首页 > 专题系列 > Java解POJ > POJ 3723 Conscription [解题报告] Java
2013
11-13

POJ 3723 Conscription [解题报告] Java

Conscription

问题描述 :

Windy has a country, and he wants to build an army to protect his country. He has picked up N girls and M boys and wants to collect them to be his soldiers. To collect a soldier without any privilege, he must pay 10000 RMB. There are some relationships between girls and boys and Windy can use these relationships to reduce his cost. If girl x and boy y have a relationship d and one of them has been collected, Windy can collect the other one with 10000-d RMB. Now given all the relationships between girls and boys, your assignment is to find the least amount of money Windy has to pay. Notice that only one relationship can be used when collecting one soldier.

输入:

The first line of input is the number of test case.
The first line of each test case contains three integers, N, M and R.
Then R lines followed, each contains three integers xi, yi and di.
There is a blank line before each test case.

1 ≤ N, M ≤ 10000
0 ≤ R ≤ 50,000
0 ≤ xi < N
0 ≤ yi < M
0 < di < 10000

输出:

For each test case output the answer in a single line.

样例输入:

2

5 5 8
4 3 6831
1 3 4583
0 0 6592
0 1 3063
3 3 4975
1 3 2049
4 2 2104
2 2 781

5 5 10
2 4 9820
3 2 6236
3 1 8864
2 4 8326
2 0 5156
2 0 1463
4 1 2439
0 4 4373
3 4 8889
2 4 3133

样例输出:

71071
54223

解题代码:

//* @author: 
//Kruskal 算法:
import java.io.*;
import java.util.*;

class cin
{
static int leave=0;
static StringTokenizer st;
static BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
static int nextInt() throws Exception
{
    while (leave==0)
    {
     st=new StringTokenizer(in.readLine());
     leave=st.countTokens();
    }
    leave--;
    return Integer.parseInt(st.nextToken());
}
}

class E
{
int weight,from,to;
void init(int x,int y,int z)
{
   weight=z;
   from=x;
   to=y;
}
}

class Cmp implements Comparator
{
public int compare(Object a,Object b)
{
   int c=((E)a).weight-((E)b).weight;
   if(c< 0)return -1;
   return 1;
}
}

class Set
{
int father[];
int num;
Set(int n)
{
   num=n;
   father=new int[n];
   this.clear();
}

void clear()
{
   int i;
   for(i=0;i< num;i++)father[i]=i;
}

int findF(int x)
{
   int t=x;
   while(t!=father[t])
   {
    t=father[t];
   }
   while(x!=father[x])
   {
    father[x]=t;
    x=father[x];
   }
   return t;
}

boolean union(int x,int y)
{
   int fx=findF(x),fy=findF(y);
   if(fx==fy)return false;
   father[fy]=fx;
   num--;
   return true;
}
}

class Kruskal
{
E e[];
int numOfE,numOfTree,i,j,numOfD;
Set points;

Kruskal(E a[],int m,int n)
{
   e=a;
   numOfE=m;
   numOfD=n;
   points=new Set(numOfD);
}

int cost()
{
   int sum=0,f1,f2;
   Arrays.sort(e,new Cmp());
   for(i=0;i< numOfE;i++)
   {
    if(points.num==1)break;
    if(points.union(e[i].from,e[i].to))
    {
     sum+=e[i].weight;
    }
   }
   return sum+10000*points.num;
}

}
public class Main {
    public static void main(String args[]) throws Exception
    {
    int g,b,r,t,i;
    E e[];
    t=cin.nextInt();
    Kruskal data;
    while((t--)>0)
    {
       g=cin.nextInt();
       b=cin.nextInt();
       r=cin.nextInt();
       e=new E[r];
       for(i=0;i< r;i++)
       {
        e[i]=new E();
        e[i].init(cin.nextInt(),cin.nextInt()+g,10000-cin.nextInt());
       }
       data=new Kruskal(e,r,g+b);
       System.out.println(data.cost());
    }
    }
}