UVA11478 Halum

2分法,对于最后能否达到x应满足每一对的:
edge(u, v) + sum(u) - sum(v) >= x
-->sum(v) <= sum(u) + (edge(u, v) - x)
-->d[v] <= d[u] + edge0(u, v)
-->模拟建图,判断是否有负环


注意:题目的目标是所有边的权值都大于0而不是非负

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int maxn = 500+5;
class Edge
{
    public:
    int pos, dist;
    Edge(int pos, int dist)
    {
        this->pos = pos;
        this->dist = dist;
    }
};

vector<Edge> adj[maxn];
bool inq[maxn];
int d[maxn], cnt[maxn], n, m, up;

void Init()
{
    for(int i = 1; i <= n; i++)
        adj[i].clear();
    int a, b, c;
    up = 0;
    for(int i = 1; i <= m; i++)
    {
        scanf("%d%d%d", &a, &b, &c);
        adj[a].push_back(Edge(b, c));
        up = max(up, c);
    }
}

bool Bellman_Ford()
{
    queue<int> myQue;
    for(int i = 1; i <= n; i++)
    {
        myQue.push(i);
        inq[i] = true;
        d[i] = 0;
        cnt[i] = 0;
    }
    while(!myQue.empty())
    {
        int u = myQue.front();
        myQue.pop();
        inq[u] = false;
        for(vector<Edge>::iterator it = adj[u].begin(); it != adj[u].end(); it++)
        {
            int v = it->pos;
            if(d[v] > d[u] + it->dist)
            {
                d[v] = d[u] + it->dist;
                if(!inq[v])
                {
                    myQue.push(v);
                    inq[v] = true;
                    if(++cnt[v] > n)
                        return true;//有负环
                }
            }
        }
    }
    return false;
}

bool Test(int M)
{
    for(int i = 1; i <= n; i++)
        for(vector<Edge>::iterator it = adj[i].begin(); it != adj[i].end(); it++)
            it->dist -= M;
    bool flag = Bellman_Ford();
    for(int i = 1; i <= n; i++)
        for(vector<Edge>::iterator it = adj[i].begin(); it != adj[i].end(); it++)
            it->dist += M;
    return flag;
}

void Solve()
{
    if(!Test(up+1))
        printf("Infinite\n");
    else if(Test(1))
        printf("No Solution\n");
    else
    {
        int L = 1, R = up + 1;
        while(L < R - 1)
        {
            int M = L + (R - L) / 2;
            if(!Test(M))
                L = M;
            else
                R = M;
        }
        printf("%d\n", L);
    }
}

int main()
{
    while(scanf("%d%d", &n, &m) != EOF)
    {
        Init();
        Solve();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值