Crazy

Хиерархија

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