poj2421 [java]Constructing Roads

来源:互联网 发布:搜狗 数据分析 面试 编辑:程序博客网 时间:2024/06/05 06:44
Constructing Roads
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 24093 Accepted: 10352

Description

There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected.

We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.

Input

The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j.

Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.

Output

You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum.

Sample Input

30 990 692990 0 179692 179 011 2

Sample Output

179

题目大意:有N个村子,修一条连接所有村子的道路。找出最少要修多长的路。

N:(3<=N<=100)n个村子

下来每一行,都包括n个数子。第一行第二列代表着,第一个村子和第2个村子之间的距离。第一行第三列,代表着第一个村子和第三个村子之间的距离。

注意:有已经连接的道路,只要把他们相连的权重设置为1就行。

思路:Prim求最小生成树。在构建Graph的时候,用的是Edge的二维数组。

import java.util.Queue;import java.util.Scanner;import java.util.concurrent.LinkedBlockingQueue;import java.util.concurrent.PriorityBlockingQueue;public class Main {public static void main(String args[]){int v=scan.nextInt();Graph G=new Graph(v);for(int i=0;i<v;i++){for(int j=0;j<v;j++){G.addEdge(new Edge(i,j,scan.nextInt()));}}int v2=scan.nextInt();for(int i=0;i<v2;i++){int a=scan.nextInt()-1;int b=scan.nextInt()-1;G.array[a][b].weight=0;G.array[b][a].weight=0;}Main test=new Main();test.start(G, v);}//boolean marked[];static Scanner scan=new Scanner(System.in);Queue<Edge> pq;public void start(Graph G,int v){marked=new boolean[v];pq=new PriorityBlockingQueue<Edge>();Queue<Edge> mst=new LinkedBlockingQueue<Edge>();visit(G,0);while(!pq.isEmpty()){Edge e=pq.poll();int w1=e.w;int v1=e.v;if(marked[w1]&&marked[v1]) continue;mst.offer(e);if(!marked[w1]) visit(G,w1);if(!marked[v1]) visit(G,v1);}int sum=0;for(Edge i:mst){sum+=i.weight;}System.out.println(sum);}public void visit(Graph g,int v){marked[v]=true;for(int i=0;i<g.array[v].length;i++){if(i==v){continue;}Edge d=g.array[v][i];int c=d.other(v);if(!marked[c]) pq.offer(d);}}} class Graph{int vSize;int eSize;Edge array[][];public Graph(int v){array=new Edge[v][v];vSize=v;}public void addEdge(Edge e){array[e.v][e.w]=e;eSize++;}}class Edge implements Comparable<Edge>{int weight;int v;int w;public Edge( int v, int w,int weight) {super();this.weight = weight;this.v = v;this.w = w;}@Overridepublic int compareTo(Edge o) {if(this.weight<o.weight){return -1;}else{if(this.weight==o.weight){return 0;}return 1;}}public int other(int point){if(point == v) return w;else return v;}}


如有错误,请留言指出


0 0
原创粉丝点击