DamSi

Untitled

Dec 21st, 2015
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.05 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.ArrayList;
  5. import java.util.Hashtable;
  6. import java.util.Iterator;
  7. import java.util.List;
  8. import java.util.LinkedList;
  9. import java.util.ListIterator;
  10. import java.util.Random;
  11.  
  12.  class GraphNodeNeighbor<E> {
  13.         GraphNode<E> node;
  14.         float weight;
  15.        
  16.         public GraphNodeNeighbor(GraphNode<E> node, float weight) {
  17.                 this.node = node;
  18.                 this.weight = weight;
  19.         }
  20.        
  21.         public GraphNodeNeighbor(GraphNode<E> node) {
  22.                 this.node = node;
  23.                 this.weight = 0;
  24.         }
  25.  
  26.         @Override
  27.         public boolean equals(Object obj) {
  28.                 @SuppressWarnings("unchecked")
  29.                 GraphNodeNeighbor<E> pom = (GraphNodeNeighbor<E>)obj;
  30.                 return pom.node.equals(this.node);
  31.         }
  32.        
  33. }
  34.  
  35.  
  36.   class GraphNode<E> {
  37.         private int index; //index (reden broj) na temeto vo grafot
  38.         private E info;
  39.         private LinkedList<GraphNodeNeighbor<E>> neighbors;
  40.        
  41.         public GraphNode(int index, E info) {
  42.                 this.index = index;
  43.                 this.info = info;
  44.                 neighbors = new LinkedList<GraphNodeNeighbor<E>>();
  45.         }
  46.        
  47.         public boolean containsNeighbor(GraphNode<E> o){
  48.                 GraphNodeNeighbor<E> pom = new GraphNodeNeighbor<E>(o,0);
  49.                 return neighbors.contains(pom);
  50.         }
  51.        
  52.         public void addNeighbor(GraphNode<E> o, float weight){
  53.                 GraphNodeNeighbor<E> pom = new GraphNodeNeighbor<E>(o,weight);
  54.                 neighbors.add(pom);
  55.         }
  56.        
  57.         public void removeNeighbor(GraphNode<E> o){
  58.                 GraphNodeNeighbor<E> pom = new GraphNodeNeighbor<E>(o,0);
  59.                 if(neighbors.contains(pom))
  60.                         neighbors.remove(pom);
  61.         }
  62.        
  63.         @Override
  64.         public String toString() {
  65.                 String ret= "INFO:"+info+" SOSEDI:";
  66.                 for(int i=0;i<neighbors.size();i++)
  67.                         ret+="("+neighbors.get(i).node.info+","+neighbors.get(i).weight+") ";
  68.                 return ret;
  69.                
  70.         }
  71.  
  72.         public void updateNeighborWeight(GraphNode<E> o, float weight){
  73.                 Iterator<GraphNodeNeighbor<E>> i = neighbors.iterator();
  74.                 while(i.hasNext()){
  75.                         GraphNodeNeighbor<E> pom = i.next();
  76.                         if(pom.node.equals(o))
  77.                                 pom.weight = weight;
  78.                 }
  79.                        
  80.         }
  81.  
  82.         @Override
  83.         public boolean equals(Object obj) {
  84.                 @SuppressWarnings("unchecked")
  85.                 GraphNode<E> pom = (GraphNode<E>)obj;
  86.                 return (pom.info.equals(this.info));
  87.         }
  88.  
  89.         public int getIndex() {
  90.                 return index;
  91.         }
  92.  
  93.         public void setIndex(int index) {
  94.                 this.index = index;
  95.         }
  96.  
  97.         public E getInfo() {
  98.                 return info;
  99.         }
  100.  
  101.         public void setInfo(E info) {
  102.                 this.info = info;
  103.         }
  104.  
  105.         public LinkedList<GraphNodeNeighbor<E>> getNeighbors() {
  106.                 return neighbors;
  107.         }
  108.  
  109.         public void setNeighbors(LinkedList<GraphNodeNeighbor<E>> neighbors) {
  110.                 this.neighbors = neighbors;
  111.         }
  112.        
  113.        
  114.        
  115.  }
  116.  
  117.   class Edge{
  118.                 private int fromVertex, toVertex;
  119.                 private float weight;
  120.                 public Edge(int from, int to, float weight){
  121.                         this.fromVertex = from;
  122.                         this.toVertex = to;
  123.                         this.weight = weight;
  124.                 }
  125.                
  126.                 public int getFrom(){
  127.                         return this.fromVertex;
  128.                 }
  129.                 public int getTo(){
  130.                         return this.toVertex;
  131.                 }
  132.                 public float getWeight(){
  133.                         return this.weight;
  134.                 }
  135.  
  136.                 @Override
  137.                 public String toString() {
  138.                         return "("+fromVertex+","+toVertex+")="+weight+" ";
  139.                 }
  140.                
  141.                
  142.         }
  143.  
  144.   class Graph<E> {
  145.  
  146.                 int num_nodes;
  147.                 GraphNode<E> adjList[];
  148.  
  149.                 @SuppressWarnings("unchecked")
  150.                 public Graph(int num_nodes, E[] list) {
  151.                         this.num_nodes = num_nodes;
  152.                         adjList = (GraphNode<E>[]) new GraphNode[num_nodes];
  153.                         for (int i = 0; i < num_nodes; i++)
  154.                                 adjList[i] = new GraphNode<E>(i, list[i]);
  155.                 }
  156.  
  157.                 @SuppressWarnings("unchecked")
  158.                 public Graph(int num_nodes) {
  159.                         this.num_nodes = num_nodes;
  160.                         adjList = (GraphNode<E>[]) new GraphNode[num_nodes];
  161.                         for (int i = 0; i < num_nodes; i++)
  162.                                 adjList[i] = new GraphNode<E>(i, null);
  163.                 }
  164.  
  165.                 int adjacent(int x, int y) {
  166.                         // proveruva dali ima vrska od jazelot so
  167.                         // indeks x do jazelot so indeks y
  168.                         return (adjList[x].containsNeighbor(adjList[y])) ? 1 : 0;
  169.                 }
  170.  
  171.                 void addEdge(int x, int y, float tezina) {
  172.                         // dodava vrska od jazelot so indeks x do jazelot so indeks y so tezina
  173.                         if (adjList[x].containsNeighbor(adjList[y])) {
  174.                                 adjList[x].updateNeighborWeight(adjList[y], tezina);
  175.                                 adjList[y].updateNeighborWeight(adjList[x], tezina);
  176.  
  177.                         } else{
  178.                                 adjList[x].addNeighbor(adjList[y], tezina);
  179.                                 adjList[y].addNeighbor(adjList[x], tezina);
  180.  
  181.                         }
  182.                 }
  183.  
  184.                 void deleteEdge(int x, int y) {
  185.                         adjList[x].removeNeighbor(adjList[y]);
  186.                         adjList[y].removeNeighbor(adjList[x]);
  187.  
  188.                 }
  189.  
  190.  
  191.                 /*******************************************************************************************************/
  192.                 /************************ PRIM **************************************************************************/
  193.            
  194.             //Metoda koja go naogja najmaloto rebro do
  195.             //teme na neposeten sosed
  196.             private Edge getMinimalEdge(boolean[] included){
  197.                 int index1 = Integer.MAX_VALUE, index2 = Integer.MAX_VALUE;
  198.                 float minweight = Float.MAX_VALUE;
  199.                
  200.                 for(int i=0;i<this.num_nodes;i++){
  201.                         if(included[i]){
  202.                                 //ako e vkluceno temeto i
  203.                                 //izmini gi negovite nevkluceni sosedi
  204.                                 Iterator<GraphNodeNeighbor<E>> it = adjList[i].getNeighbors().iterator();
  205.                                 while(it.hasNext()){
  206.                                         GraphNodeNeighbor<E> pom = it.next();
  207.                                         //ako sosedot ne e poseten i ima do sega najmala tezina
  208.                                         if(!included[pom.node.getIndex()]&&pom.weight<minweight){
  209.                                                 index1 = i;
  210.                                                 index2 = pom.node.getIndex();
  211.                                                 minweight = pom.weight;
  212.                                         }
  213.                                 }
  214.                         }
  215.                 }
  216.                
  217.                 if(minweight<Float.MAX_VALUE){
  218.                         Edge ret = new Edge(index1, index2, minweight);
  219.                         return ret;
  220.                 }
  221.                 return null;
  222.             }
  223.  
  224.            
  225.                 List<Edge> prim(int start_index) {
  226.                         // lista koja kje gi sodrzi MST rebra
  227.                         List<Edge> mstEdges = new ArrayList<Edge>();
  228.                        
  229.                         boolean included[] = new boolean[this.num_nodes];
  230.                         for(int i=0;i<this.num_nodes;i++)
  231.                                 included[i]=false;
  232.                        
  233.                         included[start_index] = true;
  234.                        
  235.                         for(int i=0;i<this.num_nodes-1;i++){
  236.                                 Edge e = this.getMinimalEdge(included);
  237.                                 if(e==null){
  238.                                         System.out.println("Ne mozat da se povrzat site jazli");
  239.                                         break;
  240.                                 }
  241.                                 included[e.getFrom()] = included[e.getTo()] = true;
  242.                                 mstEdges.add(e);
  243.                         }
  244.  
  245.                         return mstEdges;
  246.                 }
  247.  
  248.                 @Override
  249.                 public String toString() {
  250.                         String ret = new String();
  251.                         for (int i = 0; i < this.num_nodes; i++)
  252.                                 ret += adjList[i] + "\n";
  253.                         return ret;
  254.                 }
  255.  
  256.   }
  257. public class Hierarchy {
  258. public static void main(String[] args) throws NumberFormatException, IOException {
  259.          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  260.          
  261.                 Hashtable< String, Integer> hash = new Hashtable<>();
  262.                
  263.  
  264.                 int N = Integer.parseInt(br.readLine());
  265.                
  266.                 Graph<String> graf = new Graph<String>(N);
  267.                
  268.                 int M = Integer.parseInt(br.readLine());
  269.                
  270.                 for(int i = 0; i < M; i++){
  271.                         String [] niza = br.readLine().split(" ");
  272.                         graf.adjList[Integer.parseInt(niza[0])].setInfo(niza[1]);
  273.                        
  274.                         graf.adjList[Integer.parseInt(niza[2])].setInfo(niza[3]);
  275.                        
  276.                         graf.addEdge(Integer.parseInt(niza[0]), Integer.parseInt(niza[2]), Float.parseFloat(niza[4]));
  277.                        
  278.                         hash.put(niza[1], Integer.parseInt(niza[0]));
  279.                        
  280.                         hash.put(niza[3], Integer.parseInt(niza[2]));
  281.  
  282.                 }
  283.              
  284.                 String s =br.readLine();
  285.                 int start = hash.get(s);
  286.                
  287.                 int vksuma = 0;
  288.                
  289.                 List<Edge> mst = graf.prim(start);
  290.                 ListIterator<Edge> it = mst.listIterator();
  291.         while(it.hasNext()){
  292.                 Edge e = it.next();
  293.                 vksuma += e.getWeight();
  294.         }
  295.         System.out.println(vksuma);
  296.  
  297.         }
  298. }
Add Comment
Please, Sign In to add comment