UVa11090 Going in Cycle!!(BellmanFord)

题意

给定一个包含n个顶点,m条边的加权有向图,求平均权值最小的回路

思路

使用BellmanFord的队列形式,使用inq[N],cnt[N]分别表示点是否在队列中,以及进入队列的次数,如果进入队列的次数大于等于顶点数,则认为有环。另外因为需要求最小的回路,使用二分法来计算最小回路

代码

#include <bits/stdc++.h>

using namespace std;

#define _for(i, a, b) for(int i = (a); i < (b); i++)
#define _rep(i, a, b) for (int i = (a); i <= (b); i++)

const int INF = 1e9, NN = 1000;
struct Edge
{
    int from, to;
    double dist;
};

struct BellmanFord
{
    int n, m;
    vector<Edge> edges;
    vector<int> graph[NN];
    bool inq[NN];
    double d[NN];
    int p[NN];
    int cnt[NN];

    void init(int n)
    {
        this->n = n;
        _for(i, 0, n) {
            graph[i].clear();
        }
        edges.clear();
    }

    void  addEdge(int from, int to, double dist)
    {
        edges.push_back((Edge){from, to, dist});
        m = static_cast<int>(edges.size());
        graph[from].push_back(m - 1);
    }

    bool negativeCycle()
    {
        queue<int> q;
        _for(i, 0, n) {
            d[i] = 0;
            inq[i] = true;
            q.push(i);
            cnt[i] = 0;
        }

        while (!q.empty()) {
            int u = q.front(); q.pop();
            inq[u] = false;
            _for(i, 0, graph[u].size()) {
                Edge& e = edges[graph[u][i]];
                if (d[e.to] > d[u] + e.dist) {
                    d[e.to] = d[u] + e.dist;
                    p[e.to] = graph[u][i];

                    if (!inq[e.to]) {
                        q.push(e.to);
                        inq[e.to] = true;
                        if (++cnt[e.to] >= n) {
                            return true;
                        }
                    }
                }
            }
        }

        return false;
    }
};

void fastio()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
}

static BellmanFord solver;

bool test(double x)
{
    _for(i, 0, solver.m) {
        solver.edges[i].dist -= x;
    }

    bool ret = solver.negativeCycle();
    _for(i, 0, solver.m) {
        solver.edges[i].dist += x;
    }

    return ret;
}

int main()
{
    fastio();

    #ifndef ONLINE_JUDGE
        ifstream fin("f:\\OJ\\uva_in.txt");
        streambuf* back = cin.rdbuf(fin.rdbuf());
    #endif

    int t;
    cin >> t;
    _rep(kase, 1, t) {
        int n, m;
        cin >> n >> m;
        solver.init(n);

        int ub = 0;
        _for(i, 0, m) {
            int u, v, w;
            cin >> u >> v >> w;
            ub = max(ub, w);
            solver.addEdge(u - 1, v - 1, w);
        }

        cout << "Case #" << kase << ": " ;
        if (!test(ub + 1)) {
            cout << "No cycle found." << endl;
        } 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;
                }
            }

            cout << fixed << setprecision(2) << L << endl;
        }
    }

    #ifndef ONLINE_JUDGE
        cin.rdbuf(back);
    #endif

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值