Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**************************************
- ** Fichero Arista.java
- *************************************/
- package stargate;
- public class Arista {
- public int source;
- public int destination;
- public int weight;
- public Arista(int source, int destination, int weight) {
- this.source = source;
- this.destination = destination;
- this.weight = weight;
- }
- }
- /**************************************
- ** Fichero BellmanFord.java
- *************************************/
- package stargate;
- public class BellmanFord {
- public static int INFINITO = 999999999;
- public static int algoritmoBellmanFord(Arista aristas[], int nodos,
- int inicio, int fin) {
- int numArista = aristas.length;
- int distancia[] = new int[32];
- for (int i = 0; i < nodos; ++i) {
- distancia[i] = INFINITO;
- }
- distancia[inicio] = 0;
- for (int i = 0; i < nodos; ++i) {
- for (int j = 0; j < numArista; ++j) {
- if (distancia[aristas[j].source] != INFINITO) {
- if (distancia[aristas[j].source] + aristas[j].weight < distancia[aristas[j].destination]) {
- int dummy = distancia[aristas[j].source]
- + aristas[j].weight;
- if (dummy < distancia[aristas[j].destination])
- distancia[aristas[j].destination] = dummy;
- }
- }
- }
- }
- for (int i = 0; i < numArista; ++i) {
- if (distancia[aristas[i].destination] > distancia[aristas[i].source]
- + aristas[i].weight) {
- return INFINITO;
- }
- }
- return distancia[fin];
- }
- }
- /**************************************
- ** Fichero Stargate.java
- *************************************/
- package stargate;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.StringTokenizer;
- public class Stargate {
- int numeroPlanetas;
- int indiceTierra;
- int indiceAtlantis;
- public Stargate(String linea) {
- StringTokenizer datos = new StringTokenizer(linea);
- numeroPlanetas = Integer.parseInt(datos.nextToken());
- indiceTierra = Integer.parseInt(datos.nextToken());
- indiceAtlantis = Integer.parseInt(datos.nextToken());
- Arista[] edge = new Arista[datos.countTokens()];
- int i = 0;
- while (datos.hasMoreTokens()) {
- StringTokenizer datosAux = new StringTokenizer(datos.nextToken(), ",");
- int planetaInicio = Integer.parseInt(datosAux.nextToken());
- int planetaFin = Integer.parseInt(datosAux.nextToken());
- edge[i] = new Arista(planetaInicio, planetaFin, Integer.parseInt(datosAux.nextToken()));
- i++;
- }
- int distancia = BellmanFord.algoritmoBellmanFord(edge, numeroPlanetas, indiceTierra, indiceAtlantis);
- if (distancia == BellmanFord.INFINITO) {
- System.out.println("BAZINGA");
- } else {
- System.out.println(25000+distancia);
- }
- }
- public static void main(String[] args) {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- try {
- while (br.ready()) {
- String linea = br.readLine();
- new Stargate(linea);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement