Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.Hashtable;
- import java.util.Iterator;
- import java.util.List;
- import java.util.LinkedList;
- import java.util.ListIterator;
- import java.util.Random;
- class GraphNodeNeighbor<E> {
- GraphNode<E> node;
- float weight;
- public GraphNodeNeighbor(GraphNode<E> node, float weight) {
- this.node = node;
- this.weight = weight;
- }
- public GraphNodeNeighbor(GraphNode<E> node) {
- this.node = node;
- this.weight = 0;
- }
- @Override
- public boolean equals(Object obj) {
- @SuppressWarnings("unchecked")
- GraphNodeNeighbor<E> pom = (GraphNodeNeighbor<E>)obj;
- return pom.node.equals(this.node);
- }
- }
- class GraphNode<E> {
- private int index; //index (reden broj) na temeto vo grafot
- private E info;
- private LinkedList<GraphNodeNeighbor<E>> neighbors;
- public GraphNode(int index, E info) {
- this.index = index;
- this.info = info;
- neighbors = new LinkedList<GraphNodeNeighbor<E>>();
- }
- public boolean containsNeighbor(GraphNode<E> o){
- GraphNodeNeighbor<E> pom = new GraphNodeNeighbor<E>(o,0);
- return neighbors.contains(pom);
- }
- public void addNeighbor(GraphNode<E> o, float weight){
- GraphNodeNeighbor<E> pom = new GraphNodeNeighbor<E>(o,weight);
- neighbors.add(pom);
- }
- public void removeNeighbor(GraphNode<E> o){
- GraphNodeNeighbor<E> pom = new GraphNodeNeighbor<E>(o,0);
- if(neighbors.contains(pom))
- neighbors.remove(pom);
- }
- @Override
- public String toString() {
- String ret= "INFO:"+info+" SOSEDI:";
- for(int i=0;i<neighbors.size();i++)
- ret+="("+neighbors.get(i).node.info+","+neighbors.get(i).weight+") ";
- return ret;
- }
- public void updateNeighborWeight(GraphNode<E> o, float weight){
- Iterator<GraphNodeNeighbor<E>> i = neighbors.iterator();
- while(i.hasNext()){
- GraphNodeNeighbor<E> pom = i.next();
- if(pom.node.equals(o))
- pom.weight = weight;
- }
- }
- @Override
- public boolean equals(Object obj) {
- @SuppressWarnings("unchecked")
- GraphNode<E> pom = (GraphNode<E>)obj;
- return (pom.info.equals(this.info));
- }
- public int getIndex() {
- return index;
- }
- public void setIndex(int index) {
- this.index = index;
- }
- public E getInfo() {
- return info;
- }
- public void setInfo(E info) {
- this.info = info;
- }
- public LinkedList<GraphNodeNeighbor<E>> getNeighbors() {
- return neighbors;
- }
- public void setNeighbors(LinkedList<GraphNodeNeighbor<E>> neighbors) {
- this.neighbors = neighbors;
- }
- }
- class Edge{
- private int fromVertex, toVertex;
- private float weight;
- public Edge(int from, int to, float weight){
- this.fromVertex = from;
- this.toVertex = to;
- this.weight = weight;
- }
- public int getFrom(){
- return this.fromVertex;
- }
- public int getTo(){
- return this.toVertex;
- }
- public float getWeight(){
- return this.weight;
- }
- @Override
- public String toString() {
- return "("+fromVertex+","+toVertex+")="+weight+" ";
- }
- }
- class Graph<E> {
- int num_nodes;
- GraphNode<E> adjList[];
- @SuppressWarnings("unchecked")
- public Graph(int num_nodes, E[] list) {
- this.num_nodes = num_nodes;
- adjList = (GraphNode<E>[]) new GraphNode[num_nodes];
- for (int i = 0; i < num_nodes; i++)
- adjList[i] = new GraphNode<E>(i, list[i]);
- }
- @SuppressWarnings("unchecked")
- public Graph(int num_nodes) {
- this.num_nodes = num_nodes;
- adjList = (GraphNode<E>[]) new GraphNode[num_nodes];
- for (int i = 0; i < num_nodes; i++)
- adjList[i] = new GraphNode<E>(i, null);
- }
- int adjacent(int x, int y) {
- // proveruva dali ima vrska od jazelot so
- // indeks x do jazelot so indeks y
- return (adjList[x].containsNeighbor(adjList[y])) ? 1 : 0;
- }
- void addEdge(int x, int y, float tezina) {
- // dodava vrska od jazelot so indeks x do jazelot so indeks y so tezina
- if (adjList[x].containsNeighbor(adjList[y])) {
- adjList[x].updateNeighborWeight(adjList[y], tezina);
- adjList[y].updateNeighborWeight(adjList[x], tezina);
- } else{
- adjList[x].addNeighbor(adjList[y], tezina);
- adjList[y].addNeighbor(adjList[x], tezina);
- }
- }
- void deleteEdge(int x, int y) {
- adjList[x].removeNeighbor(adjList[y]);
- adjList[y].removeNeighbor(adjList[x]);
- }
- /*******************************************************************************************************/
- /************************ PRIM **************************************************************************/
- //Metoda koja go naogja najmaloto rebro do
- //teme na neposeten sosed
- private Edge getMinimalEdge(boolean[] included){
- int index1 = Integer.MAX_VALUE, index2 = Integer.MAX_VALUE;
- float minweight = Float.MAX_VALUE;
- for(int i=0;i<this.num_nodes;i++){
- if(included[i]){
- //ako e vkluceno temeto i
- //izmini gi negovite nevkluceni sosedi
- Iterator<GraphNodeNeighbor<E>> it = adjList[i].getNeighbors().iterator();
- while(it.hasNext()){
- GraphNodeNeighbor<E> pom = it.next();
- //ako sosedot ne e poseten i ima do sega najmala tezina
- if(!included[pom.node.getIndex()]&&pom.weight<minweight){
- index1 = i;
- index2 = pom.node.getIndex();
- minweight = pom.weight;
- }
- }
- }
- }
- if(minweight<Float.MAX_VALUE){
- Edge ret = new Edge(index1, index2, minweight);
- return ret;
- }
- return null;
- }
- List<Edge> prim(int start_index) {
- // lista koja kje gi sodrzi MST rebra
- List<Edge> mstEdges = new ArrayList<Edge>();
- boolean included[] = new boolean[this.num_nodes];
- for(int i=0;i<this.num_nodes;i++)
- included[i]=false;
- included[start_index] = true;
- for(int i=0;i<this.num_nodes-1;i++){
- Edge e = this.getMinimalEdge(included);
- if(e==null){
- System.out.println("Ne mozat da se povrzat site jazli");
- break;
- }
- included[e.getFrom()] = included[e.getTo()] = true;
- mstEdges.add(e);
- }
- return mstEdges;
- }
- @Override
- public String toString() {
- String ret = new String();
- for (int i = 0; i < this.num_nodes; i++)
- ret += adjList[i] + "\n";
- return ret;
- }
- }
- public class Hierarchy {
- public static void main(String[] args) throws NumberFormatException, IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- Hashtable< String, Integer> hash = new Hashtable<>();
- int N = Integer.parseInt(br.readLine());
- Graph<String> graf = new Graph<String>(N);
- int M = Integer.parseInt(br.readLine());
- for(int i = 0; i < M; i++){
- String [] niza = br.readLine().split(" ");
- graf.adjList[Integer.parseInt(niza[0])].setInfo(niza[1]);
- graf.adjList[Integer.parseInt(niza[2])].setInfo(niza[3]);
- graf.addEdge(Integer.parseInt(niza[0]), Integer.parseInt(niza[2]), Float.parseFloat(niza[4]));
- hash.put(niza[1], Integer.parseInt(niza[0]));
- hash.put(niza[3], Integer.parseInt(niza[2]));
- }
- String s =br.readLine();
- int start = hash.get(s);
- int vksuma = 0;
- List<Edge> mst = graf.prim(start);
- ListIterator<Edge> it = mst.listIterator();
- while(it.hasNext()){
- Edge e = it.next();
- vksuma += e.getWeight();
- }
- System.out.println(vksuma);
- }
- }
Add Comment
Please, Sign In to add comment