3

解决 UVA 的这个套利问题http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=40但我坚持找到最短长度的负循环(这里的长度是顶点数)。这是我成功检测到负循环的代码

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class _104 {

public static void main(String[] args) throws NumberFormatException,
        IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(
            System.in));
    String input;
    while ((input = reader.readLine()) != null) {
        int n = Integer.parseInt(input);
        double[][] cost = new double[n + 1][n + 1];
        double[] spEstimate = new double[n + 1];
        int parent[] = new int[n + 1];
        for (int i = 0; i < n + 1; i++) {
            spEstimate[i] = Double.MAX_VALUE;
            cost[0][i] = 0;
            cost[i][0] = Double.MAX_VALUE;
            parent[i] = Integer.MAX_VALUE;
        }
        spEstimate[0] = 0.0;
        parent[0] = 0;
        for (int i = 1; i < n + 1; i++) {
            String[] line = reader.readLine().split("\\s+");
            for (int j = 1; j < n + 1; j++) {
                if (i == j) {
                    cost[i][j] = 0;
                } else if (i < j) {
                    cost[i][j] = -(Math
                            .log(Double.parseDouble(line[j - 2])) / Math
                            .log(2));
                } else {
                    cost[i][j] = -(Math
                            .log(Double.parseDouble(line[j - 1])) / Math
                            .log(2));
                }
            }
        }
        int save = 1, s = 1;
        boolean flag = BellmanFord(n, cost, spEstimate, parent);
         display(cost);
        // Relax all edges once more
        boolean brk = true;
        for (int i = 0; i < cost.length && brk; i++) {
            for (int j = 0; j < cost.length && brk; j++) {
                //relax(i, j, spEstimate, cost[i][j], parent);
            }
        }

        ArrayList<Integer> path = new ArrayList<Integer>();
        while (parent[save] != s) {
            path.add(save);
            save = parent[save];
        }
        if (flag) {
            System.out.println("no arbitrage sequence exists");
        } else {
            path.add(0, path.get(path.size() - 1));
            for (int i = path.size() - 1; i >= 0; --i) {
                System.out.println(path.get(i));
            }
        }
    }
    reader.close();
}

public static boolean BellmanFord(int n, double[][] cost, double[] sp,
        int[] parent) {
    for (int k = 0; k < n - 1; k++) {
        for (int i = 0; i < cost.length; i++) {
            for (int j = 0; j < cost.length; j++) {
                relax(i, j, sp, cost[i][j], parent);
            }
        }
    }
    // Relax all edges once more to detect cycle
    for (int i = 0; i < cost.length; i++) {
        for (int j = 0; j < cost.length; j++) {
            if (sp[j] > (sp[i] + cost[i][j])) {
                return false;
            }
        }
    }
    return true;
}

static void relax(int i, int j, double[] sp, double cij, int[] parent) {
    if (sp[j] > (sp[i] + cij)) {
        sp[j] = sp[i] + cij;
        System.out.println("relaxed " + i + " " + j + " " + cij + " "
         + sp[i] + " " + sp[j]);
        parent[j] = i;
    }
}

static void display(double[][] cost) {
    System.out.println("Display Cost");
    for (int i = 0; i < cost.length; i++) {
        for (int j = 0; j < cost.length; j++) {
            System.out.print(cost[i][j] + "\t");
        }
        System.out.println();
    }
}

static void display(double[] sp) {
    for (int i = 0; i < sp.length; i++) {
        System.out.println(sp[i]);
    }
}
}
4

2 回答 2

2

你可以这样做:

  1. 修复循环的起始顶点(我们称之为v)。

  2. 假设 运行 Ford-Bellman 算法dist[i] = 0 if i = v and INF otherwise

  3. 如果存在一个包含 的负循环,则在 Ford-Bellman 算法的外循环迭代v后将变为负循环。因此,您可以通过简单地检查每次迭代后是否仍然是非负数来轻松找到这样的最小值。kdist[v]kdist[v]

  4. 其中最小kv是答案。

于 2014-10-18T14:09:51.003 回答
0

可以通过考虑增加长度的循环来解决这个问题,而不是像 kraskevich 描述的那样找到负循环。两种方法的最坏情况复杂度都是 O(n^4)。这种方法类似于 Floyd-Warshall,您考虑增加长度而不是中间顶点。

您可以在此处找到包含图表和代码的详细说明。

于 2016-05-30T04:54:38.767 回答