2017华为软件精英挑战赛系列三

前面的一篇博客介绍了如何通过费用流算法得到每条边经过的带宽,然后建立费用流图,通过深度优先算法得到源点到汇点的路径,去掉源点就可以得到各服务器节点到消费节点的路径。


public class GetPath {
	public static int[][] adjmat ;
	public static StringBuffer sb = new StringBuffer(); ;
	public static String[] gPath(Graphx graphx,ArrayList<Integer> serviceList) {
		adjmat =  MyZkw.getVirtexFlowArrays(graphx,serviceList);
		Graph graph = new Graph(adjmat.length);
		for(int i = 0;i<adjmat.length;i++){
			graph.newNode(i);
		}
		for(int i = 0;i<adjmat.length;i++){
			for(int j = 0;j<adjmat.length;j++){
				if(adjmat[i][j] != 0){
					graph.addVertex(i, j);
				}
			}
		}
		int [] arf = GetService.SeverLevel();
		dfsPath(graph,arf);
		return convert();
	}
	public static void dfsPath(Graph graph,int[] arf) {
		Stack<Integer> theStack = new Stack<Integer>();
		theStack.push(MyZkw.src);
		while (!theStack.isEmpty()) {
			int v = -1;
			int peek = theStack.peek();
			while (graph.vertexList[peek].allVisitedStack.size() != 0) {
				if (!theStack.contains(graph.vertexList[peek].allVisitedStack.peek())) {
					v = graph.vertexList[peek].allVisitedStack.pop();
					break;
				} else {
					graph.vertexList[peek].allVisitedStack.pop();
				}
			}
			if(theStack.peek() == MyZkw.des){
				int minband = 1000000;
				for(int i =0;i<theStack.size()-1;i++){
					if(adjmat[theStack.get(i)][theStack.get(i+1)]<minband){
						minband = adjmat[theStack.get(i)][theStack.get(i+1)];
					}
				}
				
				for(int i =1;i<theStack.size()-1;i++){
					if(i == theStack.size()-2){
						sb.append(theStack.get(i)-GetService.nodeNum+" ");
					}else{
						sb.append(theStack.get(i)+" ");
					}
				}
				sb.append(minband+" "+arf[theStack.get(1)]+"\n");
				//sb.append(minband+"\n");
				int index = 0;
				for(int i =0;i<theStack.size()-1;i++){
					adjmat[theStack.get(i)][theStack.get(i+1)] -= minband;
				}
				
				for(int i =0;i<theStack.size()-1;i++){
					if(adjmat[theStack.get(i)][theStack.get(i+1)] ==0){
						index = theStack.get(i);
						break;
					}
				}
				while(theStack.peek()!=index){
					graph.vertexList[theStack.peek()].allVisitedStack.clear();
					graph.vertexList[theStack.peek()].allVisitedStack.addAll(graph.vertexList[theStack.peek()].org_VisitedStack);
					theStack.pop();
				}
				continue;
			}
			if (v == -1) {
				graph.vertexList[peek].allVisitedStack.clear();
				graph.vertexList[peek].allVisitedStack.addAll(graph.vertexList[peek].org_VisitedStack);
				theStack.pop();
			} else {
				if (adjmat[theStack.peek()][v] >0) {
					theStack.push(v);
				}
			}
		}
		
	}
	public static String[] convert() {
		String[] str = sb.toString().split("\n");
		sb.append("\n" + str.length);
		String[] strx = sb.toString().split("\n");
		for (int start = 0, end = strx.length - 1; start < end; start++, end--) {
			String temp = strx[end];
			strx[end] = strx[start];
			strx[start] = temp;
		}
		return strx;
	}

}
final class Graph {
	private int MAX_VERTS;

	public Graph(int MAX_VERTS) {
		this.MAX_VERTS = MAX_VERTS;
		init();
	}

	private void init() {
		vertexList = new Vertex[MAX_VERTS];
	}

	Vertex[] vertexList;

	public void newNode(int cur) {
		vertexList[cur] = new Vertex();
	}
	public void addVertex(int cur, int edge) {
		vertexList[cur].allVisitedStack.add(edge);//
		vertexList[cur].org_VisitedStack.add(edge);//
	}
}

final class Vertex {
	Stack<Integer> org_VisitedStack = new Stack<Integer>();
	Stack<Integer> allVisitedStack = new Stack<Integer>();

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值