Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package communicationroutingchallenge;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import static java.lang.System.out;
- import java.util.ArrayList;
- import java.util.StringTokenizer;
- public class CommunicationRoutingChallenge {
- // public static int visited = -1;
- public static void main(String[] args) {
- FastReader fastreader = new FastReader();
- int NodeCount,EdgeCount,ConstrainedCount,FlowCount;
- int FlowID,SourceNode,TargetNode,FlowRate;
- int SFL = 200, GFL = 100;
- NodeCount = fastreader.nextInt();
- EdgeCount = fastreader.nextInt();
- ConstrainedCount = fastreader.nextInt();
- FlowCount = fastreader.nextInt();
- int EdgeID[] = new int[EdgeCount];
- int GroupID[] = new int[EdgeCount];
- int StartNodeID[] = new int[EdgeCount];
- int EndNodeID[] = new int[EdgeCount];
- int Distance[] = new int[EdgeCount];
- int Capacity[] = new int[EdgeCount];
- for(int i=0;i<EdgeCount;i++){
- EdgeID[i] = fastreader.nextInt();
- GroupID[i] = fastreader.nextInt();
- StartNodeID[i] = fastreader.nextInt();
- EndNodeID[i] = fastreader.nextInt();
- Distance[i] = fastreader.nextInt();
- Capacity[i] = fastreader.nextInt();
- }
- int NodeID[] = new int[NodeCount];
- int EdgeID1[] = new int[NodeCount];
- int EdgeID2[] = new int[NodeCount];
- for(int j=0; j<ConstrainedCount;j++){
- NodeID[j] = fastreader.nextInt();
- EdgeID1[j] = fastreader.nextInt();
- EdgeID2[j] = fastreader.nextInt();
- }
- FlowID = fastreader.nextInt();
- SourceNode = fastreader.nextInt();
- TargetNode = fastreader.nextInt();
- FlowRate = fastreader.nextInt();
- int GetValue = 0;
- int visited = 0;
- int FlowPath = 0;
- boolean isNodeID = false;
- int array[] = new int[NodeCount];
- int goEnd = 0;
- ArrayList al = new ArrayList();
- for(int i=0;i<NodeCount;i++){
- if(i == NodeID[i]){
- isNodeID = true;
- }else{
- isNodeID = false;
- }
- al.add(GetValue);
- // out.println(Distance[SourceNode]);
- array[i] = SourceNode;
- visited = (i==0? visited =0: array [i-1]);
- // out.println(visited +" visit ");
- if(SourceNode == TargetNode){
- FlowID++;
- break;
- }else{
- if(SourceNode>0 && goEnd == 0){
- GetValue = Comperision(EndNodeID,EdgeCount,SourceNode,Distance,Capacity,SFL,FlowRate,EndNodeID,StartNodeID,goEnd,visited,TargetNode,isNodeID,EdgeID1,EdgeID2,ConstrainedCount);
- }else{
- GetValue = Comperision(StartNodeID,EdgeCount,SourceNode,Distance,Capacity,SFL,FlowRate,EndNodeID,StartNodeID,goEnd,visited,TargetNode,isNodeID,EdgeID1,EdgeID2,ConstrainedCount);
- }
- }
- SourceNode = (goEnd == 0? StartNodeID[GetValue] : EndNodeID[GetValue]); // visited = SourceNode;
- // out.println(SourceNode+" is source node again");
- // out.println(Distance[GetValue]+" Value");
- FlowPath += Distance[GetValue];
- if(SourceNode ==0)
- goEnd = 1;
- }
- out.println(FlowID);
- al.forEach((n) -> out.print(n+" "));
- }
- 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) {
- // out.print(SourceNode+"<- source \n");
- int capacity = 0;
- int distance = 0;
- int returnValue = 0;
- boolean forFirstTime = true;
- for(int i=0; i<EdgeCount;i++){
- if(goEnd == 1){
- if(StartNodeID[i] == SourceNode && EndNodeID[i]==visited){
- continue;
- }
- }
- if((StartNodeID[i] == SourceNode&& EndNodeID[i] == TargetNode)||(EndNodeID[i] == SourceNode&& StartNodeID[i] == TargetNode)){
- returnValue = i;
- }
- if((NodeID[i] == SourceNode)){
- if((Distance[i]>=FlowRate)&&(Distance[i])<=(SFL+FlowRate)){
- if(forFirstTime){
- capacity = Capacity[i];
- distance = Distance[i];
- returnValue = i;
- forFirstTime = false;
- // out.println(returnValue+"From boolean");
- }if(distance == Distance[i]){
- if(capacity < Capacity[i]){
- capacity = Capacity[i];
- distance = Distance[i];
- returnValue = i;
- // out.println(returnValue+"From eqal eqal");
- }else{
- //returnValue = i;
- // out.println(returnValue+"From eqal eqal else");
- }
- }else{
- if(distance < Distance[i]){
- //returnValue = i;
- // out.println(returnValue+"From less than distance");
- }else{
- distance = Distance[i];
- capacity = Capacity[i];
- returnValue = i;
- // out.println(returnValue+"From less than distance else");
- }
- }
- }
- }
- }
- if(isNodeID){
- for(int i=0;i<Contraint;i++){
- if(EdgeE1[i]==returnValue || EdgeE2[i]==returnValue)
- return 0;
- }
- }
- // out.println(returnValue+"<- ReturnValue \n");
- // out.println(visited+"Visited");
- return returnValue;
- }
- private static class FastReader {
- BufferedReader br;
- StringTokenizer st;
- public FastReader() {
- br = new BufferedReader(new InputStreamReader(System.in));
- }
- String next()
- {
- while (st == null || !st.hasMoreElements()) {
- try {
- st = new StringTokenizer(br.readLine());
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }
- return st.nextToken();
- }
- int nextInt() { return Integer.parseInt(next()); }
- long nextLong() { return Long.parseLong(next()); }
- double nextDouble(){ return Double.parseDouble(next()); }
- String nextLine()
- {
- String str = "";
- try {
- str = br.readLine();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- return str;
- }
- }
- }
Add Comment
Please, Sign In to add comment