某地区经过对城镇交通状况的调查,得到现有城镇间快速道路的统计数据,并提出“畅通工程”的目标:使整个地区任何两个城镇间都可以实现快速交通(但不一定有直接的快速道路相连,只要互相间接通过快速路可达即可)。现得到城镇道路统计表,表中列出了任意两城镇间修建快速路的费用,以及该道路是否已经修通的状态。现请你编写程序,计算出全地区畅通需要的最低成本。
输入格式:
输入的第一行给出村庄数目NN (1\le N \le 1001≤N≤100);随后的N(N-1)/2N(N−1)/2行对应村庄间道路的成本及修建状态:每行给出4个正整数,分别是两个村庄的编号(从1编号到NN),此两村庄间道路的成本,以及修建状态 — 1表示已建,0表示未建。
输出格式:
输出全省畅通需要的最低成本。
输入样例:
4
1 2 1 1
1 3 4 0
1 4 1 1
2 3 3 0
2 4 2 1
3 4 5 0
输出样例:
3
import java.util.PriorityQueue;
import java.util.Scanner;
public class Main {
static Scanner in;
static int[] s;//顶点
static int n;
static int ans=0;//权值代价
public Main(){//构造函数初始化
in=new Scanner(System.in);
n=in.nextInt();
s=new int[n+1];
for(int i=0;i<n;i++){
s[i]=-1;
}
}
public static int find(int x){//采用路径压缩的find()
if(s[x]==-1){
return x;
}else{
return s[x]=find(s[x]);
}
}
public static void union(int root1,int root2){//按大小求并
if(s[root1]>s[root2]){
s[root2]=root1;
}else{
s[root1]=root2;
}
}
public static void kruskal(){//克鲁斯卡尔
int edgesAccepted=0;
PriorityQueue<Edge> pq=new PriorityQueue<Edge>();
for(int i=0;i<n*(n-1)/2;i++){
int begin=in.nextInt();
int end=in.nextInt();
int cost=in.nextInt();
int con=in.nextInt();
Edge edge=new Edge(begin,end,cost,con);
pq.add(edge);
}
while(edgesAccepted<n-1){//选择n-1条边则连通
Edge edge=pq.poll();
int x=find(edge.begin);
int y=find(edge.end);
if(x!=y){//查看顶点x与y是否在同一集合中
if(edge.con==0){
ans+=edge.cost;
}
union(x,y);
edgesAccepted++;
}
}
}
public static void main(String[] args) {
new Main();
kruskal();
System.out.println(ans);
}
public static class Edge implements Comparable<Edge>{
private int begin,end,cost,con;
public Edge(int begin,int end,int cost,int con){
this.begin=begin;
this.end=end;
this.cost=cost;
this.con=con;
}
@Override
public int compareTo(Edge o) {
return this.cost-o.cost;
}
}
}