Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void outputData() throws FileNotFoundException {
- PrintWriter out = new PrintWriter("negcycle.out");
- int start = findNegativeCycle();
- if (start == -1) {
- out.print("NO");
- } else {
- int finish = start;
- for (int i = 0; i < countVertices; i++) {
- finish = parent[finish];
- }
- ArrayList<Integer> path = new ArrayList<>();
- for (int j = finish; ; j = parent[j]) {
- path.add(j);
- if (j == finish && path.size() > 1) break;
- }
- out.println("YES");
- out.println(path.size());
- for (int i = path.size() - 1; i >= 0; i--) {
- out.print((path.get(i) + 1) + " ");
- }
- }
- out.close();
- }
- private int findNegativeCycle() {
- dist = new int[countVertices];
- parent = new int[countVertices];
- Arrays.fill(dist, 1000_000_000);
- Arrays.fill(parent, -1);
- dist[0] = 0;
- int start = -1;
- for (int k = 0; k < countVertices; k++) {
- start = -1;
- for (int i = 0; i < countVertices; i++) {
- for (int j = 0; j < countVertices; j++) {
- if (dist[j] > dist[i] + matrix[i][j]) {
- parent[j] = i;
- dist[j] = dist[i] + matrix[i][j];
- start = j;
- }
- }
- }
- }
- return start;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement