agri-net - codetrick
Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course.
Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.
The distance between any two farms will not exceed 100,000.
https://github.com/scv119/USACO/blob/master/src/agrinet.java
http://qingtangpaomian.iteye.com/blog/1635938
基本的Prim算法,按照算法实现的步骤来做就可以了。首先将起始点加入最小生成树中,然后遍历每次取最小的边加入,重复n-1次,就可以得到一颗完整的最小生成树。
Prim: http://massivealgorithms.blogspot.com/2014/07/greedy-algorithms-set-5-prims-minimum.html
Read full article from agri-net - codetrick
Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course.
Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.
The distance between any two farms will not exceed 100,000.
PROGRAM NAME: agrinet
INPUT FORMAT
Line 1: | The number of farms, N (3 <= N <= 100). |
Line 2..end: | The subsequent lines contain the N x N connectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem. |
SAMPLE INPUT (file agrinet.in)
4 0 4 9 21 4 0 8 17 9 8 0 16 21 17 16 0
OUTPUT FORMAT
The single output contains the integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.SAMPLE OUTPUT (file agrinet.out)
28X. PriorityQueue Prime
https://github.com/scv119/USACO/blob/master/src/agrinet.java
static class Node implements Comparable{ int id; | |
int value; | |
int visit; | |
@Override | |
public int compareTo(Object o) { | |
return this.value - ((Node)o).value; | |
} | |
} | |
static int prim(){ | |
Node list[] = new Node[N]; | |
for(int i = 0; i < N; i ++) | |
{ | |
list[i] = new Node(); | |
list[i].id = i; | |
list[i].value = INF; | |
} | |
list[0].value = 0; | |
PriorityQueue<Node> q = new PriorityQueue<Node>(); | |
for(Node node:list) | |
q.add(node); | |
int ret = 0; | |
while(q.size()>0){ | |
Node node = q.poll(); | |
if(node.value == INF) | |
break; | |
node.visit = 1; | |
ret += node.value; | |
for(int j = 0 ; j < N ; j ++){ | |
if(g[node.id][j]<list[j].value && list[j].visit == 0) | |
{ | |
list[j].value = g[node.id][j]; | |
q.remove(list[j]); | |
q.offer(list[j]); | |
} | |
} | |
} | |
return ret; | |
} |
http://qingtangpaomian.iteye.com/blog/1635938
基本的Prim算法,按照算法实现的步骤来做就可以了。首先将起始点加入最小生成树中,然后遍历每次取最小的边加入,重复n-1次,就可以得到一颗完整的最小生成树。
- ArrayList<Integer> now = new ArrayList<Integer>();//在最小生成树当中的节点
- ArrayList<Integer> next = new ArrayList<Integer>();//不在最小生成树中的节点
- int num = in.nextInt();
- int[][] dist = new int[num][num];
- for (int i=0;i<num;i++){
- next.add(i);
- for (int j=0;j<num;j++){
- dist[i][j] = in.nextInt();
- dist[j][i] = dist[i][j];
- }
- }
- now.add(0);
- next.remove(new Integer(0));
- int cnt = 0;
- for(int i=1;i<num;i++){
- int min = Integer.MAX_VALUE;
- int nextNode = -1;
- for (int j=0;j<now.size();j++){
- for (int k=0;k<next.size();k++){
- if (dist[now.get(j)][next.get(k)]!=0){
- if(dist[now.get(j)][next.get(k)]<min){
- min = dist[now.get(j)][next.get(k)];
- nextNode = next.get(k);
- }
- }
- }
- }
- cnt = cnt + min;
- now.add(nextNode);
- next.remove(new Integer(nextNode));
- }
private static void run()
{
//根据边的距离排序
Collections.sort(edges);
//依次考虑每条边
Iterator it = edges.iterator();
while (it.hasNext())
{
edge e = (edge)it.next();
//如果边的两个顶点属于不同的集合
if (e.u.set != e.v.set)
{
//把此边加入最小生成树
cost += e.w;
//合并两个集合
union(e.u.set, e.v.set);
}
}
}
{
//根据边的距离排序
Collections.sort(edges);
//依次考虑每条边
Iterator it = edges.iterator();
while (it.hasNext())
{
edge e = (edge)it.next();
//如果边的两个顶点属于不同的集合
if (e.u.set != e.v.set)
{
//把此边加入最小生成树
cost += e.w;
//合并两个集合
union(e.u.set, e.v.set);
}
}
}
private static void union(Set s1, Set s2)
{
s1.addAll(s2);//把s2集合中的元素加入s1中
//让s2中的每个顶点指向s1集合
Iterator it = s2.iterator();
while (it.hasNext())
{
vertex v = (vertex)it.next();
v.set = s1;
}
}
Kruska: http://massivealgorithms.blogspot.com/2014/07/greedy-algorithms-set-2-kruskals.html{
s1.addAll(s2);//把s2集合中的元素加入s1中
//让s2中的每个顶点指向s1集合
Iterator it = s2.iterator();
while (it.hasNext())
{
vertex v = (vertex)it.next();
v.set = s1;
}
}
Prim: http://massivealgorithms.blogspot.com/2014/07/greedy-algorithms-set-5-prims-minimum.html
Read full article from agri-net - codetrick