POJ 3621 Two points + judgment negative ring

Title

Portal POJ 3621 Sightseeing Cows

answer

Dichotomous answer, the question is transformed into a decision that satisfies the existence of a ring in the graph and satisfies the largest xx of the following formulax ∑ F i ∑ T j > x \frac{\sum F_i}{\sum T_j}>x TjFi>x exists if the ring is repeated through the point whichF i F_iFiOnly counted once, the number of points and edges in the figure are different. When the above situation occurs, the ring is composed of at least two simple rings. Suppose F i, T i F_i, T_i on two ringsFi,TiThe sums are x 1, y 1 x_1, y_1x1,Y1x 2, y 2 x_2, y_2x2,Y2, A certain intersection point of the ring is ppp,则有 max ⁡ ( x 1 y 1 , x 2 y 2 ) ≥ x 1 + x 2 y 1 + y 2 > x 1 + x 2 − F p y 1 + y 2 \max(\frac{x_1}{y_1},\frac{x_2}{y_2})\geq \frac{x_1+x_2}{y_1+y_2}>\frac{x_1+x_2-F_p}{y_1+y_2} max(Y1x1,Y2x2)Y1+Y2x1+x2>Y1+Y2x1+x2FpIt can be observed that the answer is better when the simple ring is taken, and the number of points and edges on the ring are the same. Follow 0/1 0/10 / 1 Thought planning score, will be converted to the formula Σ (T ix - F i) <0 \ sum (T_ix-F_i) <0(TixFi)<0 S P F A SPFA S P F A can determine whether there is a negative ring in the graph.

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = 1005, maxm = 5005, inf = 0x3f3f3f3f;
const double eps = 1e-4;
int N, M, F[maxn], cnt[maxn];
int tot, head[maxn], to[maxm], T[maxm], nxt[maxm];
double cost[maxm], ds[maxn];
bool in[maxn];

void inline add(int x, int y, int t)
{
    
    
    to[++tot] = y, T[tot] = t, nxt[tot] = head[x], head[x] = tot;
}

bool SPFA()
{
    
    
    memset(cnt, 0, sizeof(cnt));
    memset(in, 0, sizeof(in));
    fill(ds + 1, ds + N + 1, inf);
    ds[1] = 0, in[1] = 1;
    queue<int> q;
    q.push(1);
    while (q.size())
    {
    
    
        int x = q.front();
        q.pop(), in[x] = 0;
        for (int i = head[x]; i; i = nxt[i])
        {
    
    
            int y = to[i];
            double z = cost[i];
            if (ds[x] + z < ds[y])
            {
    
    
                ds[y] = ds[x] + z, cnt[y] = cnt[x] + 1;
                if (cnt[y] >= N)
                    return 1;
                if (!in[y])
                    q.push(y), in[y] = 1;
            }
        }
    }
    return 0;
}

bool judge(double x)
{
    
    
    for (int i = 1; i <= M; ++i)
        cost[i] = T[i] * x - F[to[i]];
    return SPFA();
}

int main()
{
    
    
    scanf("%d%d", &N, &M);
    for (int i = 1; i <= N; ++i)
        scanf("%d", F + i);
    for (int i = 1, x, y, t; i <= M; ++i)
        scanf("%d%d%d", &x, &y, &t), add(x, y, t);
    double lb = 0, ub = 1000 * N;
    while (ub - lb > eps)
    {
    
    
        double mid = (lb + ub) / 2;
        if (judge(mid))
            lb = mid;
        else
            ub = mid;
    }
    printf("%.2f\n", ub);
    return 0;
}

Guess you like

Origin blog.csdn.net/neweryyy/article/details/114714822