欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

洛谷P3199 [HNOI2009]最小圈(01分数规划)

程序员文章站 2022-10-06 22:01:33
题意 "题目链接" Sol 暴力01分数规划可过 标算应该是 "这个" ......

题意

题目链接

sol

暴力01分数规划可过

标算应该是

#include<bits/stdc++.h> 
#define pair pair<int, double>
#define mp(x, y) make_pair(x, y)
#define fi first
#define se second
//#define int long long 
#define ll long long 
#define fin(x) {freopen(#x".in","r",stdin);}
#define fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int maxn = 3001, mod = 998244353, inf = 2e9 + 10;
const double eps = 1e-9;
template <typename a, typename b> inline bool chmin(a &a, b b){if(a > b) {a = b; return 1;} return 0;}
template <typename a, typename b> inline bool chmax(a &a, b b){if(a < b) {a = b; return 1;} return 0;}
template <typename a, typename b> inline ll add(a x, b y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename a, typename b> inline void add2(a &x, b y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename a, typename b> inline ll mul(a x, b y) {return 1ll * x * y % mod;}
template <typename a, typename b> inline void mul2(a &x, b y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename a> inline void debug(a a){cout << a << '\n';}
template <typename a> inline ll sqr(a x){return 1ll * x * x;}
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int n, m;
vector<pair> v[maxn];
double a[maxn], dis[maxn];
int vis[maxn], times[maxn], can[maxn];
bool spfa(int s,  double k) {
    queue<int> q; q.push(s);
    for(int i = 1; i <= n; i++) vis[i] = 0, times[i] = 0, dis[i] = inf;
    dis[s] = 0;
    times[s]++; 
    while(!q.empty()) {
        int p = q.front(); q.pop(); vis[p] = 0;
        can[p] = 1;
        for(auto &sta : v[p]) {
            int to = sta.fi; double w = sta.se;
            if(chmin(dis[to], dis[p] + w - k)) {
                if(!vis[to]) q.push(to), vis[to] = 1, times[to]++;
                if(times[to] > 50) return 1;
            }
        }
    }
    return 0;
}
bool check(double val) {
    memset(can, 0, sizeof(can));
    for(int i = 1; i <= n; i++)
        if(!can[i] && spfa(i, val)) return 1;
    return 0;
}
signed main() {
    //fin(a);
    n = read(); m = read();
    for(int i = 1; i <= m; i++) {
        int x = read(), y = read(); double z; scanf("%lf", &z);
        v[x].push_back({y, z});
    }
    double l = -1e7 - 10, r = 1e7 + 10;
    while(r - l > eps) {
        double mid = (l + r) / 2;
        if(check(mid)) r = mid;
        else l = mid;
    }
    printf("%.8lf", l);
    return 0;
}
/*
10 3
aaaabbbbab
7 7 3 9 10 6 7 6 6 1
2 6 2
1 3 1
2 9 1
*/