graphprogram1.java

来自「Classes composed of directed weighted gr」· Java 代码 · 共 95 行

JAVA
95
字号

import java.io.*;
import java.util.*;

public class GraphProgram1 {
	
	public static void main(String[] args)  throws IOException {
		
		BufferedReader stdin = new BufferedReader(new FileReader("graph2.in"));
		
		String input = stdin.readLine();
		
		String[] tmp = input.split(" ");
		
		int number = Integer.parseInt(tmp[0]);
		int start = Integer.parseInt(tmp[1]);
		int end = Integer.parseInt(tmp[2]);
		
		int[] list = new int[number+1];
		
		WeightedGraph theGraph = new DirectedWeightedGraph(number);
				
		while (stdin.ready()) {
			input = stdin.readLine();
			String[] tokens = input.split(" ");
			
			int num1 = Integer.parseInt(tokens[0]);
			int num2 = Integer.parseInt(tokens[1]);
			int weight = Integer.parseInt(tokens[2]);
			int cost = Integer.parseInt(tokens[3]);
			
			Vertex v1 = new Vertex(num1);
			Vertex v2 = new Vertex(num2);
			
			if(!search(list,v1.el)) {
				list[num1]=1;
				theGraph.incrementVertices();
			}
			
			if(!search(list,v2.el)) {
				list[num2]=1;
				theGraph.incrementVertices();
			}
			
			
			WeightedEdge e1 = new WeightedEdge(v1, v2, weight, cost);
			theGraph.addEdge(e1);	
		}
	     
	   
		System.out.println();
		System.out.println("No. of vertices = " + theGraph.getVertices());
		System.out.println("No. of edges = " + theGraph.getEdges());
		
		System.out.print("Vertices : ");
		displayVertices(theGraph);
		System.out.println();
		
		System.out.println("Edges : " );
		displayEdges(theGraph);
		System.out.println();
		theGraph.maxFlowMinCostAlgorithm(start, end);
		
	}  //end main
	
		
	static boolean search(int[] list, int number) {
		if(list[number] == 1)
			return true;
		return false;
	}
	
	static void displayVertices(Graph theGraph){
	
		Vector vertexList = theGraph.getVertexList();
		
		for(int i=0; i<vertexList.size(); i++)
			System.out.print((Vertex) vertexList.elementAt(i));
	}
	
	static void displayEdges(Graph theGraph){
	
	 	//list of edges
		Vector edgeList = theGraph.getEdgeList();
		
		for(int i=0; i<edgeList.size(); i++)
			System.out.println((Edge) edgeList.elementAt(i));

	}
	
		
}  //end GraphProgram1


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?