您的位置:首页 > 其它

[置顶] 2017华为软件精英挑战赛系列三

2017-10-09 21:02 579 查看

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

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>();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: