Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.LinkedHashSet;
- import java.util.LinkedList;
- import java.util.Map;
- public final class Path {
- public final class Edge{
- private int k;
- private float w;
- public Edge(int k, float w){
- setK(k);
- setW(w);
- }
- public int getK() {
- return this.k;
- }
- public double getW(){
- return this.w;
- }
- public void setK(int k){
- this.k = k;
- }
- public void setW(float w){
- this.w = w;
- }
- }
- private int last;
- private double min;
- private final Map<Integer, LinkedHashSet<Edge>> map;
- public Path (int last){
- this.map = new HashMap<Integer, LinkedHashSet<Edge>>();
- setMin(Float.MAX_VALUE);
- setLast(last);
- }
- public void setLast(int last){
- this.last = last;
- }
- public void setMin(double min){
- this.min = min;
- }
- public double getMin(){
- return this.min;
- }
- public int getLast(){
- return this.last;
- }
- void addEdge(int knot1, Edge knot2){
- LinkedHashSet<Edge> adjacent = map.get(knot1);
- if (adjacent==null){
- adjacent = new LinkedHashSet<Edge>();
- map.put(knot1, adjacent);
- }
- adjacent.add(knot2);
- }
- private LinkedList<Edge> adjacentknots(Integer last){
- LinkedHashSet<Edge> adjacent = map.get(last);
- if (adjacent==null) {
- return new LinkedList<Edge>();
- }
- return new LinkedList<Edge>(adjacent);
- }
- public void depthFirst(LinkedList<Edge> visited) {
- LinkedList<Edge> knots = adjacentknots(visited.getLast().getK());
- boolean pom=false;
- for (Edge knot : knots){
- pom=knot.getK() == getLast();
- if (visited.contains(knot))
- continue;
- if (pom){
- visited.add(knot);
- checkPath(visited);
- visited.removeLast();
- break;
- }
- }
- for (Edge knot : knots){
- if (pom){
- continue;
- }
- if( visited.contains(knot) || pom){
- continue;
- }
- visited.addLast(knot);
- depthFirst(visited);
- visited.removeLast();
- }
- }
- private void checkPath(LinkedList<Edge> visited){
- double prev = 0;
- double temp = 0;
- for (Edge knot : visited){
- if (prev != 0 && knot.w != 0){
- temp = temp + 1./knot.w + Math.abs(prev-knot.w);
- prev = knot.w;
- }
- if (prev == 0 && knot.w != 0){
- temp = temp + 1./knot.w;
- prev = knot.w;
- }
- }
- if (temp < getMin())
- setMin(temp);
- }
- }
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.*;
- public class Main {
- private static Path graph;
- private static void initConnection(final String database, int endPoint) throws SQLException{
- Connection conn = DriverManager.getConnection(database);
- graph = new Path(endPoint);
- buildGraph(conn);
- }
- private static void buildGraph(Connection conn) throws SQLException{
- Statement st = conn.createStatement();
- ResultSet rs = st.executeQuery("SELECT x, y, p FROM Gtable");
- while (rs.next()) {
- graph.addEdge(rs.getInt(1), graph.new Edge(rs.getInt(2), rs.getFloat(3)));
- }
- }
- public static void main(String[] args) throws SQLException {
- //String database = args[0].trim();
- String database ="jdbc:sqlserver://localhost:1433;databaseName=master;user=test1;password=test1";
- //int endPoint = Integer.parseInt(args[1]);
- int endPoint = 1;
- initConnection(database, endPoint);
- LinkedList<Path.Edge> visited = new LinkedList<Path.Edge>();
- visited.add(graph.new Edge(1, 0));
- graph.depthFirst(visited);
- System.out.format("Koszt: %.3f%n", graph.getMin());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement