Md_Sakib_Hossain

World Final ICPC

Oct 13th, 2021 (edited)
785
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.94 KB | None | 0 0
  1. package communicationroutingchallenge;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import static java.lang.System.out;
  7. import java.util.ArrayList;
  8. import java.util.StringTokenizer;
  9.  
  10.  
  11. public class CommunicationRoutingChallenge {
  12.  
  13.  //   public static int visited = -1;
  14.  
  15.     public static void main(String[] args) {
  16.       FastReader fastreader = new FastReader();
  17.         int NodeCount,EdgeCount,ConstrainedCount,FlowCount;
  18.         int FlowID,SourceNode,TargetNode,FlowRate;
  19.        
  20.         int SFL = 200, GFL = 100;
  21.      
  22.         NodeCount = fastreader.nextInt();
  23.         EdgeCount = fastreader.nextInt();
  24.         ConstrainedCount = fastreader.nextInt();
  25.         FlowCount = fastreader.nextInt();
  26.        
  27.         int EdgeID[] = new int[EdgeCount];
  28.         int GroupID[] = new int[EdgeCount];
  29.         int StartNodeID[] = new int[EdgeCount];
  30.         int EndNodeID[] = new int[EdgeCount];
  31.         int Distance[] = new int[EdgeCount];
  32.         int Capacity[] = new int[EdgeCount];
  33.        
  34.        
  35.         for(int i=0;i<EdgeCount;i++){
  36.             EdgeID[i] = fastreader.nextInt();
  37.             GroupID[i] = fastreader.nextInt();
  38.             StartNodeID[i] = fastreader.nextInt();
  39.             EndNodeID[i] = fastreader.nextInt();
  40.             Distance[i] = fastreader.nextInt();
  41.             Capacity[i] = fastreader.nextInt();            
  42.         }
  43.         int NodeID[] = new int[NodeCount];
  44.         int EdgeID1[] = new int[NodeCount];
  45.         int EdgeID2[] = new int[NodeCount];
  46.        
  47.         for(int j=0; j<ConstrainedCount;j++){
  48.             NodeID[j] = fastreader.nextInt();
  49.             EdgeID1[j] = fastreader.nextInt();
  50.             EdgeID2[j] = fastreader.nextInt();            
  51.         }
  52.             FlowID = fastreader.nextInt();
  53.             SourceNode = fastreader.nextInt();
  54.             TargetNode = fastreader.nextInt();
  55.             FlowRate = fastreader.nextInt();                                            
  56.                  
  57.             int GetValue = 0;                  
  58.             int visited = 0;
  59.             int FlowPath = 0;
  60.             boolean isNodeID = false;
  61.             int array[] = new int[NodeCount];
  62.             int goEnd = 0;
  63.             ArrayList al = new ArrayList();
  64.             for(int i=0;i<NodeCount;i++){
  65.               if(i == NodeID[i]){
  66.                   isNodeID = true;
  67.               }else{
  68.                   isNodeID = false;
  69.               }
  70.                 al.add(GetValue);
  71.              // out.println(Distance[SourceNode]);
  72.               array[i] = SourceNode;            
  73.  
  74.              visited = (i==0? visited =0: array [i-1]);
  75.            //       out.println(visited +" visit ");
  76.                if(SourceNode == TargetNode){
  77.                    FlowID++;
  78.                 break;    
  79.                }else{
  80.                       if(SourceNode>0 && goEnd == 0){
  81.                        GetValue = Comperision(EndNodeID,EdgeCount,SourceNode,Distance,Capacity,SFL,FlowRate,EndNodeID,StartNodeID,goEnd,visited,TargetNode,isNodeID,EdgeID1,EdgeID2,ConstrainedCount);  
  82.                       }else{
  83.                        GetValue = Comperision(StartNodeID,EdgeCount,SourceNode,Distance,Capacity,SFL,FlowRate,EndNodeID,StartNodeID,goEnd,visited,TargetNode,isNodeID,EdgeID1,EdgeID2,ConstrainedCount);
  84.                       }
  85.                 }
  86.                
  87.                SourceNode = (goEnd == 0? StartNodeID[GetValue] : EndNodeID[GetValue]);                                                           //  visited = SourceNode;
  88.     //           out.println(SourceNode+" is source node again");
  89.              
  90.            //   out.println(Distance[GetValue]+" Value");
  91.               FlowPath += Distance[GetValue];
  92.              
  93.               if(SourceNode ==0)
  94.                goEnd = 1;        
  95.                              
  96.             }
  97.             out.println(FlowID);
  98.              
  99.              al.forEach((n) -> out.print(n+" "));  
  100.     }
  101.  
  102.     private static int Comperision(int[] NodeID,int EdgeCount,int SourceNode,int[] Distance,int[] Capacity,int SFL,int FlowRate,int[] EndNodeID,int[] StartNodeID,int goEnd,int visited,int TargetNode,boolean isNodeID,int[] EdgeE1, int[] EdgeE2,int Contraint) {
  103.               // out.print(SourceNode+"<- source \n");
  104.                
  105.                int capacity = 0;
  106.                int distance = 0;
  107.                int returnValue = 0;
  108.                boolean forFirstTime = true;
  109.            
  110.                 for(int i=0; i<EdgeCount;i++){
  111.                     if(goEnd == 1){
  112.                          if(StartNodeID[i] == SourceNode && EndNodeID[i]==visited){
  113.                            continue;
  114.                           }
  115.                       }
  116.                        if((StartNodeID[i] == SourceNode&& EndNodeID[i] == TargetNode)||(EndNodeID[i] == SourceNode&& StartNodeID[i] == TargetNode)){
  117.                       returnValue = i;                        
  118.                     }
  119.                  if((NodeID[i] == SourceNode)){
  120.                     if((Distance[i]>=FlowRate)&&(Distance[i])<=(SFL+FlowRate)){
  121.                        if(forFirstTime){
  122.                            capacity = Capacity[i];
  123.                             distance = Distance[i];
  124.                            returnValue = i;
  125.                            forFirstTime = false;
  126.                          //  out.println(returnValue+"From boolean");
  127.                        }if(distance == Distance[i]){
  128.                             if(capacity < Capacity[i]){
  129.                                capacity = Capacity[i];
  130.                                distance = Distance[i];
  131.                                returnValue = i;
  132.                              //  out.println(returnValue+"From eqal eqal");
  133.                            }else{
  134.                                //returnValue = i;
  135.                             //   out.println(returnValue+"From eqal eqal else");
  136.                            }
  137.                        }else{
  138.                            if(distance < Distance[i]){
  139.                                //returnValue = i;
  140.                               // out.println(returnValue+"From less than distance");
  141.                            }else{
  142.                                distance = Distance[i];
  143.                                capacity = Capacity[i];
  144.                                returnValue = i;
  145.                              //  out.println(returnValue+"From less than distance else");
  146.                            }
  147.                        }
  148.                    }
  149.                  }
  150.                }
  151.                 if(isNodeID){
  152.                       for(int i=0;i<Contraint;i++){
  153.                           if(EdgeE1[i]==returnValue || EdgeE2[i]==returnValue)
  154.                               return 0;
  155.                       }
  156.                      }
  157.       //  out.println(returnValue+"<- ReturnValue \n");
  158.       //  out.println(visited+"Visited");
  159.         return returnValue;
  160.     }
  161.       private static class FastReader {
  162.         BufferedReader br;
  163.         StringTokenizer st;
  164.  
  165.         public FastReader() {
  166.             br = new BufferedReader(new InputStreamReader(System.in));
  167.         }
  168.         String next()
  169.         {
  170.             while (st == null || !st.hasMoreElements()) {
  171.                 try {
  172.                     st = new StringTokenizer(br.readLine());
  173.                 }
  174.                 catch (IOException e) {
  175.                     e.printStackTrace();
  176.                 }
  177.             }
  178.             return st.nextToken();
  179.         }
  180.  
  181.         int nextInt() { return Integer.parseInt(next()); }
  182.  
  183.         long nextLong() { return Long.parseLong(next()); }
  184.  
  185.         double nextDouble(){ return Double.parseDouble(next()); }
  186.  
  187.         String nextLine()                                
  188.         {
  189.             String str = "";
  190.             try {
  191.                 str = br.readLine();
  192.             }
  193.             catch (IOException e) {
  194.                 e.printStackTrace();
  195.             }
  196.             return str;
  197.         }
  198.     }
  199.    
  200. }
  201.  
Add Comment
Please, Sign In to add comment