Advertisement
lameski

Gradovi

Jan 12th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.46 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Iterator;
  5. import java.util.LinkedList;
  6. import java.util.Queue;
  7. import java.util.Stack;
  8.  
  9. class Graph<E> {
  10.  
  11. int num_nodes;
  12. GraphNode<E> adjList[];
  13.  
  14. @SuppressWarnings("unchecked")
  15. public Graph(int num_nodes, E[] list) {
  16. this.num_nodes = num_nodes;
  17. adjList = (GraphNode<E>[]) new GraphNode[num_nodes];
  18. for (int i = 0; i < num_nodes; i++)
  19. adjList[i] = new GraphNode<E>(i, list[i]);
  20. }
  21.  
  22. @SuppressWarnings("unchecked")
  23. public Graph(int num_nodes) {
  24. this.num_nodes = num_nodes;
  25. adjList = (GraphNode<E>[]) new GraphNode[num_nodes];
  26. for (int i = 0; i < num_nodes; i++)
  27. adjList[i] = new GraphNode<E>(i, null);
  28. }
  29.  
  30. int adjacent(int x, int y) {
  31. // proveruva dali ima vrska od jazelot so
  32. // indeks x do jazelot so indeks y
  33. return (adjList[x].containsNeighbor(adjList[y])) ? 1 : 0;
  34. }
  35.  
  36. void addEdge(int x, int y, float tezina) {
  37. // dodava vrska od jazelot so indeks x do jazelot so indeks y so
  38. // tezina
  39. if (adjList[x].containsNeighbor(adjList[y])) {
  40. adjList[x].updateNeighborWeight(adjList[y], tezina);
  41. } else
  42. adjList[x].addNeighbor(adjList[y], tezina);
  43. }
  44.  
  45. void deleteEdge(int x, int y) {
  46. adjList[x].removeNeighbor(adjList[y]);
  47. }
  48.  
  49. /* void dfsSearch(int node) {
  50. boolean visited[] = new boolean[num_nodes];
  51. for (int i = 0; i < num_nodes; i++)
  52. visited[i] = false;
  53. dfsRecursive(node, visited);
  54. }
  55.  
  56. /*void dfsRecursive(int node, boolean visited[]) {
  57. visited[node] = true;
  58. System.out.println(node + ": " + adjList[node].getInfo());
  59.  
  60. for (int i = 0; i < adjList[node].getNeighbors().size(); i++) {
  61. GraphNode<E> pom = adjList[node].getNeighbors().get(i);
  62. if (!visited[pom.getIndex()])
  63. dfsRecursive(pom.getIndex(), visited);
  64. }
  65. }
  66.  
  67. /*void dfsNonrecursive(int node) {
  68. boolean visited[] = new boolean[num_nodes];
  69. for (int i = 0; i < num_nodes; i++)
  70. visited[i] = false;
  71. visited[node] = true;
  72. System.out.println(node+": " + adjList[node].getInfo());
  73. Stack<Integer> s = new Stack<Integer>();
  74. s.push(node);
  75.  
  76. GraphNode<E> pom;
  77.  
  78. while (!s.isEmpty()) {
  79. pom = adjList[s.peek()];
  80. GraphNode<E> tmp=null;
  81. for (int i = 0; i < pom.getNeighbors().size(); i++) {
  82. tmp = pom.getNeighbors().get(i);
  83. if (!visited[tmp.getIndex()])
  84. break;
  85. }
  86. if(tmp!=null && !visited[tmp.getIndex()]){
  87. visited[tmp.getIndex()] = true;
  88. System.out.println(tmp.getIndex() + ": " + tmp.getInfo());
  89. s.push(tmp.getIndex());
  90. }
  91. else
  92. s.pop();
  93. }
  94.  
  95. }
  96.  
  97. /* void bfs(int node){
  98. boolean visited[] = new boolean[num_nodes];
  99. for (int i = 0; i < num_nodes; i++)
  100. visited[i] = false;
  101. visited[node] = true;
  102. System.out.println(node+": " + adjList[node].getInfo());
  103. Queue<Integer> q = new LinkedQueue<Integer>();
  104. q.enqueue(node);
  105.  
  106. GraphNode<E> pom;
  107.  
  108. while(!q.isEmpty()){
  109. pom = adjList[q.dequeue()];
  110. GraphNode<E> tmp=null;
  111. for (int i = 0; i < pom.getNeighbors().size(); i++) {
  112. tmp = pom.getNeighbors().get(i);
  113. if (!visited[tmp.getIndex()]){
  114. visited[tmp.getIndex()] = true;
  115. System.out.println(tmp.getIndex()+": " + tmp.getInfo());
  116. q.enqueue(tmp.getIndex());
  117. }
  118. }
  119.  
  120.  
  121. }
  122.  
  123. }
  124. float dijkstra(int from ,int to) {
  125. /* Minimalna cena do sekoe od teminjata */
  126. float distance[] = new float[this.num_nodes];
  127. int ofrom = from;
  128. /* dali za temeto e najdena konecnata (minimalna) cena */
  129. boolean finalno[] = new boolean[this.num_nodes];
  130. int pred[] = new int[this.num_nodes];
  131.  
  132. for (int i = 0; i < this.num_nodes; i++) {
  133. distance[i] = -1;
  134. finalno[i] = false;
  135. pred[i] = i;
  136. }
  137. finalno[from] = true;
  138. distance[from] = 0;
  139. // vo sekoj cekor za edno teme se dobiva konecna minimalna cena
  140. while(from != to) {
  141. /*
  142. * za site sledbenici na from presmetaj ja cenata (ako ne e konecna)
  143. */
  144. Iterator<GraphNodeNeighbor<E>> it = adjList[from].getNeighbors().iterator();
  145. while (it.hasNext()) {
  146. GraphNodeNeighbor<E> pom = it.next();
  147. /* ako grankata kon sosedot nema konecna cena */
  148. if (!finalno[pom.node.getIndex()]) {
  149. /* ako ne e presmetana cena za temeto */
  150. if (distance[pom.node.getIndex()] < 0) {
  151. distance[pom.node.getIndex()] = distance[from] + pom.weight;
  152. pred[pom.node.getIndex()] = from;
  153. }
  154. /* inaku, ako e pronajdena poniska */
  155. else if (distance[from] + pom.weight < distance[pom.node.getIndex()]) {
  156. distance[pom.node.getIndex()] = distance[from] + pom.weight;
  157. pred[pom.node.getIndex()] = from;
  158. }
  159. }
  160. }
  161. /* najdi teme so min. cena koja ne e konecna */
  162. boolean minit = false; /* min. ne e inicijaliziran */
  163. int k = -1;
  164. float minc = 99999;
  165. /* proveri gi site jazli */
  166. for (int j = 0; j < this.num_nodes; j++) {
  167. if (!finalno[j] && distance[j] != -1) {
  168. /* ako cenata ne e konecna */
  169. if (minc > distance[j])
  170. {
  171. k = j;
  172. minc = distance[j];
  173.  
  174. }
  175. }
  176. }
  177. finalno[from = k] = true;
  178. }
  179. Stack<Integer> stack = new Stack();
  180. int i = to;
  181.  
  182. while(i != ofrom)
  183. {
  184. stack.push(i);
  185. i = pred[i];
  186. }
  187. stack.push(ofrom);
  188.  
  189. while(!stack.isEmpty())
  190. System.out.print(adjList[stack.pop()].getInfo() + " ");
  191. System.out.println("");
  192. return distance[to];
  193. }
  194.  
  195.  
  196.  
  197. @Override
  198. public String toString() {
  199. String ret = new String();
  200. for (int i = 0; i < this.num_nodes; i++)
  201. ret += i + ": " + adjList[i] + "\n";
  202. return ret;
  203. }
  204.  
  205. /*public void setInfo(int k, E string) {
  206. adjList[k].setInfo(string);
  207. }*/
  208. public int getIndex(E inf)
  209. {
  210. for(int i=0; i<num_nodes; i++)
  211. {
  212. if(adjList[i].getInfo().equals(inf))
  213. return i;
  214. }
  215. return -1;
  216. }
  217.  
  218. }
  219.  
  220. class GraphNodeNeighbor<E> {
  221. GraphNode<E> node;
  222. float weight;
  223.  
  224. public GraphNodeNeighbor(GraphNode<E> node, float weight) {
  225. this.node = node;
  226. this.weight = weight;
  227. }
  228. }
  229.  
  230. class GraphNode<E> {
  231. private int index;//index (reden broj) na temeto vo grafot
  232. private E info;
  233. private LinkedList<GraphNodeNeighbor<E>> neighbors;
  234.  
  235. public GraphNode(int index, E info) {
  236. this.index = index;
  237. this.info = info;
  238. neighbors = new LinkedList<GraphNodeNeighbor<E>>();
  239. }
  240.  
  241. boolean containsNeighbor(GraphNode<E> o){
  242. GraphNodeNeighbor<E> pom = new GraphNodeNeighbor<E>(o,0);
  243. return neighbors.contains(pom);
  244. }
  245. void addNeighbor(GraphNode<E> o, float weight){
  246. GraphNodeNeighbor<E> pom = new GraphNodeNeighbor<E>(o,weight);
  247. neighbors.add(pom);
  248. }
  249.  
  250.  
  251. void removeNeighbor(GraphNode<E> o) {
  252. GraphNodeNeighbor<E> pom = new GraphNodeNeighbor<E>(o, 0);
  253. if (neighbors.contains(pom))
  254. neighbors.remove(pom);
  255. }
  256.  
  257. void updateNeighborWeight(GraphNode<E> o, float weight) {
  258. Iterator<GraphNodeNeighbor<E>> i = neighbors.iterator();
  259. while (i.hasNext()) {
  260. GraphNodeNeighbor<E> pom = i.next();
  261. if (pom.node.equals(o))
  262. pom.weight = weight;
  263. }
  264. }
  265.  
  266. @Override
  267. public boolean equals(Object obj) {
  268. @SuppressWarnings("unchecked")
  269. GraphNode<E> pom = (GraphNode<E>)obj;
  270. return (pom.info.equals(this.info));
  271. }
  272.  
  273. public int getIndex() {
  274. return index;
  275. }
  276.  
  277. public void setIndex(int index) {
  278. this.index = index;
  279. }
  280.  
  281. public E getInfo() {
  282. return info;
  283. }
  284.  
  285. public void setInfo(E info) {
  286. this.info = info;
  287. }
  288.  
  289. public LinkedList<GraphNodeNeighbor<E>> getNeighbors() {
  290. return neighbors;
  291. }
  292.  
  293. public void setNeighbors(LinkedList<GraphNodeNeighbor<E>> neighbors) {
  294. this.neighbors = neighbors;
  295. }
  296.  
  297.  
  298.  
  299. }
  300. public class Gradovi {
  301. public static void main(String[] args) throws IOException {
  302. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  303. int n = Integer.parseInt(br.readLine());
  304. Graph<String> g = new Graph<String>(n);
  305.  
  306. n = Integer.parseInt(br.readLine());
  307. for(int i = 0; i < n; i++){
  308. String line = br.readLine();
  309. String temp[] = line.split(" ");
  310. int k = Integer.parseInt(temp[0]);
  311. int j = Integer.parseInt(temp[2]);
  312. System.out.println(temp[0]);
  313. g.adjList[k].setInfo(temp[1]);
  314. g.adjList[j].setInfo(temp[3]);
  315. g.addEdge(k, j, Float.parseFloat(temp[4]));
  316. }
  317. int from = g.getIndex(br.readLine()), to = g.getIndex(br.readLine());
  318. br.close();
  319.  
  320. float min = g.dijkstra(from, to);
  321. float min2 = g.dijkstra(to, from);
  322. System.out.println(min + min2);
  323. }
  324. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement