Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.73 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.stream.Collectors;
  6. import java.util.HashSet;
  7. import java.util.Scanner;
  8.  
  9.  
  10. class FatKnot {
  11.  
  12.     private String tmx;
  13.     private int miP, vzl, f;
  14.     public FixedRail tk4;
  15.     private HashSet<FatKnot> yxs, ghN;
  16.  
  17.     public void setVzl(int a) {
  18.         vzl = a;
  19.     }
  20.  
  21.     public void setF (int a) {
  22.         f = a;
  23.     }
  24.  
  25. //    public void setTk4(FixedRail a){
  26. //        tk4 = a;
  27. //    }
  28.  
  29.     public String getTmx() {
  30.         return tmx;
  31.     }
  32.  
  33.     public int getMiP() {
  34.         return miP;
  35.     }
  36.  
  37.     public int getVzl() {
  38.         return vzl;
  39.     }
  40.  
  41.     public int getF() {
  42.         return f;
  43.     }
  44.  
  45. //    public FixedRail getTk4() {
  46. //        return tk4;
  47. //    }
  48.  
  49.     public HashSet<FatKnot> getYxs() {
  50.         return yxs;
  51.     }
  52.  
  53.     public HashSet<FatKnot> getGhN() {
  54.         return ghN;
  55.     }
  56.  
  57.     FatKnot(String csk, int x33) {
  58.         this.tmx = csk;
  59.         this.miP = x33;
  60.         tk4 = FixedRail.w;
  61.         yxs = new HashSet<>();
  62.         ghN = new HashSet<>();
  63.     }
  64.  
  65.     public void addToYxs(FatKnot obj){
  66.         yxs.add(obj);
  67.     }
  68.  
  69.     public void addToGhN(FatKnot obj){
  70.         ghN.add(obj);
  71.     }
  72.  
  73.     public String toString() {
  74.         String iid = tmx + " [label = \"" + tmx + "(" + miP + ")\"";
  75.         switch (tk4) {
  76.             case r:
  77.                 return iid + ", color = red]";
  78.             case blu:
  79.                 return iid + ", color = blue]";
  80.             default:
  81.                 return iid + "]";
  82.         }
  83.     }
  84.  
  85. }
  86.  
  87.  
  88. enum FixedRail {
  89.     g, w, bla, blu, r
  90. }
  91.  
  92. public class Cpm {
  93.  
  94.     private static int t;
  95.  
  96.     public static void main(String[] args) throws FileNotFoundException {
  97.         // Scanner scanner = new Scanner(System.in);
  98.         File file = new File("C:\\Users\\79774\\IdeaProjects\\z3\\src\\test.txt");
  99.         Scanner scanner = new Scanner(file);
  100.         scanner.useDelimiter(";\n|\\Z");
  101.         HashMap<String, FatKnot> waw = new HashMap<>();
  102.         FatKnot hpw = null;
  103.         do {
  104.             FatKnot otR = null;
  105.             String wir;
  106.             for (String toR : scanner.next().split("\\s*<\\s*")) {
  107.                 int fkU = toR.indexOf('(');
  108.                 if (fkU != -1) {
  109.                     wir = toR.substring(0, fkU);
  110.                     waw.put(wir, new FatKnot(wir, Integer.parseInt(toR.substring(fkU + 1, toR.lastIndexOf(')')))));
  111.                 } else wir = toR;
  112.                 if (otR == null)
  113.                     otR = waw.get(wir);
  114.                 else
  115.                     otR.getYxs().add(otR = waw.get(wir));
  116.                 if (hpw == null)
  117.                     hpw = otR;
  118.             }
  119.         } while (scanner.hasNext());
  120.         t = 0;
  121.         for (FatKnot jeq : waw.values()) {
  122.             if (jeq.tk4 == FixedRail.w)
  123.                 bfs(jeq, jeq.getMiP());
  124.         }
  125.         for (FatKnot zpD : waw.values())
  126.             for (FatKnot mzH : zpD.getYxs())
  127.                 if (mzH.tk4 != FixedRail.blu && zpD.getF() < mzH.getF())
  128.                     newTaste(mzH);
  129.         ArrayList<FatKnot> od6 = new ArrayList<>();
  130.         hpw = null;
  131.         for (FatKnot ven : waw.values()) {
  132.             if (ven.tk4 == FixedRail.bla) {
  133.                 if (hpw == null) {
  134.                     hpw = ven;
  135.                 } else if (hpw.getVzl() < ven.getVzl()) {
  136.                     od6.clear();
  137.                     hpw = ven;
  138.                 }
  139.                 if (hpw.getVzl() <= ven.getVzl())
  140.                     od6.add(ven);
  141.             }
  142.         }
  143.         for (FatKnot vl4 : od6)
  144.             if (vl4.tk4 != FixedRail.r)
  145.                 fatBox(vl4);
  146.         System.out.println("digraph {\n  " + waw.values().stream().map(FatKnot::toString).collect(Collectors.joining("\n  ")) + waw.values().stream().map(u
  147.                 -> u.getYxs().stream().map(adjacent
  148.                 -> "\n  " + u.getTmx() + " -> " + adjacent.getTmx() + (u.tk4 == FixedRail.r && adjacent.tk4 == FixedRail.r && adjacent.getGhN().contains(u) ? " [color = red]"
  149.                 : (u.tk4 == FixedRail.blu && adjacent.tk4 == FixedRail.blu ? " [color = blue]" : ""))).collect(Collectors.joining())).collect(Collectors.joining()) + "\n}");
  150.     }
  151.  
  152.  
  153.     private static void bfs(FatKnot ghN, int currentvzl) {
  154.         t++;
  155.         ghN.setVzl(currentvzl);
  156.         ghN.tk4 =FixedRail.g;
  157.         for (FatKnot ptq : ghN.getYxs())
  158.             switch (ptq.tk4) {
  159.                 case w:
  160.                     ptq.getGhN().add(ghN);
  161.                     bfs(ptq, currentvzl + ptq.getMiP());
  162.                     break;
  163.                 case g:
  164.                     newTaste(ptq);
  165.                     return;
  166.                 case bla:
  167.                     updatevzl(ptq, ghN, currentvzl + ptq.getMiP());
  168.                     break;
  169.             }
  170.         ghN.setF(t);
  171.         ghN.tk4 =(FixedRail.bla);
  172.     }
  173.  
  174.     private static void newTaste(FatKnot ye8) {
  175.         ye8.tk4 = FixedRail.blu;
  176.         for (FatKnot zvp : ye8.getYxs())
  177.             if (zvp.tk4 != FixedRail.blu)
  178.                 newTaste(zvp);
  179.     }
  180.  
  181.     private static void fatBox(FatKnot ssx) {
  182.         ssx.tk4 = FixedRail.r;
  183.         for (FatKnot fs1 : ssx.getGhN())
  184.             if (fs1.tk4 != FixedRail.r)
  185.                 fatBox(fs1);
  186.     }
  187.  
  188.     private static void updatevzl(FatKnot evq, FatKnot newdv6, int vzl) {
  189.         if (evq.getVzl() < vzl) {
  190.             evq.getGhN().clear();
  191.             evq.getGhN().add(newdv6);
  192.             evq.setVzl(vzl);
  193.             for (FatKnot zbO : evq.getYxs())
  194.                 if (zbO.tk4 != FixedRail.blu)
  195.                     updatevzl(zbO, evq,vzl + zbO.getMiP());
  196.         } else if (evq.getVzl() == vzl) evq.getGhN().add(newdv6);
  197.     }
  198.  
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement