Ultraviolet A-11090 going in cycle !! (Bellman-Ford algorithm negative ring)

Source: Internet
Author: User

Description

I IU P C2 0 06

Problem G: Going in cycle !!

Input: Standard Input

Output: standard output

You are given a weighted directed graphNVertices andMEdges. each cycle in the graph has a weight, which equals to sum of its edges. there are so many cycles in the graph with different weights. in this problem we want to find a cycle with the Minimum Mean.

Input

The first line of input gives the number of cases,N.NTest Cases Follow. Each one starts with two numbersNAndM.MLines follow, each has three positive numberA, B, cWhich means there is an edge from vertexAToBWith WeightC.

Output

For each test case output one line containing"Case # X:"Followed by a number that is the lowest mean cycle in graph with 2 digits after decimal place, if there is a cycle. Otherwise print"No cycle found.".

Constraints

-N ≤ 50

-A, B ≤ n

-C ≤ 10000000

Sample Input

Output for sample input

2
2 1
1 2 1
2 2
1 2 2
2 1 3

Case #1: No cycle found.
Case #2: 2.50

Given a weighted directed graph with n vertices and m edges, find the loop with the smallest average weight.

Idea: first use binary solution for mid. Assume that there is a loop containing K edges, and the weight of each edge on the road is W1, W2.... wk, then the average value is less than mid.

Meaning: W1 + W2 .. + wk <K * mid-> (w1-mid) + (w2-mid) +... (WK-mid) <0 indicates whether the new graph contains a negative weight loop.

#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <queue>#include <vector>using namespace std;const int maxn = 60;const int inf = 0x3f3f3f3f;struct Edge {int from, to;double dist;};struct BellmanFord {int n, m;vector<Edge> edges;vector<int> G[maxn];bool inq[maxn];double d[maxn];int p[maxn];int cnt[maxn];void init(int n) {this->n = n;for (int i = 0; i < n; i++)G[i].clear();edges.clear();}void AddEdge(int from, int to, int dist) {edges.push_back((Edge){from, to, dist});m = edges.size();G[from].push_back(m-1);}bool negativeCycle() {queue<int> Q;memset(inq, 0, sizeof(inq));memset(cnt, 0, sizeof(cnt));/*for (int i = 0; i < n; i++) {d[i] = 0;inq[0] = 1;Q.push(i);}*/for (int i = 0; i < n; i++) {d[i] = 0;inq[i] = 1;Q.push(i);cnt[i] = 1;}while (!Q.empty()) {int u = Q.front();Q.pop();inq[u] = 0;for (int i = 0; i < G[u].size(); i++) {Edge &e = edges[G[u][i]];if (d[e.to] > d[u] + e.dist) {d[e.to] = d[u] + e.dist;p[e.to] = G[u][i];if (!inq[e.to]) {Q.push(e.to);inq[e.to] = 1;if (++cnt[e.to] > n)return 1;}}}}return 0;}};BellmanFord solver;bool test(double x) {for (int i = 0; i < solver.m; i++)solver.edges[i].dist -= x;bool ret = solver.negativeCycle();for (int i = 0; i < solver.m; i++)solver.edges[i].dist += x;return ret;}int main() {int t;scanf("%d", &t);for (int cas = 1; cas <= t; cas++) {int n, m;scanf("%d%d", &n, &m);solver.init(n);int ub = 0;while (m--) {int u, v, w;scanf("%d%d%d", &u, &v, &w);u--, v--;ub = max(ub, w);solver.AddEdge(u, v, w);}printf("Case #%d: ", cas);if (!test(ub+1))printf("No cycle found.\n");else {double L = 0, R = ub;while (R-L > 1e-3) {double M = L + (R-L)/2;if (test(M))R = M;else L = M;}printf("%.2lf\n", L);}}return 0;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.