POJ 3621 Sightseeing Cows 01分数规划

21 篇文章 0 订阅
18 篇文章 0 订阅

找出一个环使得其点权和比边权和最大。

maxviei

既然是分数规划,令答案为a,有
aviei

变形
a×eivi

(a×eivi)0

y= ,有

  • 即存在一个环 y<0 ,如果令sum里面的东西为新边权,那么就是找出一个环使得边权和为负数,即负权回路。
  • 对于所有的环 y0 ,相对地即无负权回路。
    惨痛经历告诉我们SPFA判负环最好用STL queue。。或者队列开循环队列/数组开大点。WA了好久发现数组开小了。。
#include <cstdio>
#include <cstring>
#include <queue>
#define FOR(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int N = 1001, M = 10001;
int h[N], p[M], v[M], w[M], f[N], tot = 0;
int q[2097152], vis[N], cnt[N], n;
double dis[N];

void add(int x, int y, int z) {
    p[++tot] = h[x]; w[tot] = z; v[tot] = y; h[x] = tot;
}

bool spfa(double a) {
    int l = 0, r = 0, i, u;
    FOR(i,2,n) dis[i] = 1e12;
    memset(cnt, 0, sizeof cnt);
    memset(vis, 0, sizeof vis);
    q[r++] = 1; dis[1] = 0;
    while (l < r) {
        u = q[l++]; ++cnt[u]; vis[u] = 0;
        if (cnt[u] > n) return 1;
        for (i = h[u]; i; i = p[i]) {
            double c = a * w[i] - f[v[i]];
            if (dis[v[i]] > dis[u] + c) {
                dis[v[i]] = dis[u] + c;
                if (!vis[v[i]]) vis[v[i]] = 1, q[r++] = v[i];
            }
        }
    }
    return 0;
}

int main() {
    int i, m, q, a, b, c;
    while (scanf("%d%d", &n, &m) != EOF) {
        tot = 0;
        memset(h, 0, sizeof h);
        FOR(i,1,n) scanf("%d", &f[i]);
        while (m--) {
            scanf("%d%d%d", &a, &b, &c);
            add(a, b, c);
        }
        double l = 0, r = 1000, mid;
        while (r - l > 1e-3) {
            mid = (l + r) / 2;
            if (spfa(mid)) l = mid;
            else r = mid;
        }
        printf("%.2f\n", l);
    }
    return 0;
}

Sightseeing Cows

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8978 Accepted: 3029

Description

Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big city! The cows must decide how best to spend their free time.

Fortunately, they have a detailed city map showing the L (2 ≤ L ≤ 1000) major landmarks (conveniently numbered 1.. L) and the P (2 ≤ P ≤ 5000) unidirectional cow paths that join them. Farmer John will drive the cows to a starting landmark of their choice, from which they will walk along the cow paths to a series of other landmarks, ending back at their starting landmark where Farmer John will pick them up and take them back to the farm. Because space in the city is at a premium, the cow paths are very narrow and so travel along each cow path is only allowed in one fixed direction.

While the cows may spend as much time as they like in the city, they do tend to get bored easily. Visiting each new landmark is fun, but walking between them takes time. The cows know the exact fun values Fi (1 ≤ Fi ≤ 1000) for each landmark i.

The cows also know about the cowpaths. Cowpath i connects landmark L1i to L2i (in the direction L1i -> L2i ) and requires time Ti (1 ≤ Ti ≤ 1000) to traverse.

In order to have the best possible day off, the cows want to maximize the average fun value per unit time of their trip. Of course, the landmarks are only fun the first time they are visited; the cows may pass through the landmark more than once, but they do not perceive its fun value again. Furthermore, Farmer John is making the cows visit at least two landmarks, so that they get some exercise during their day off.

Help the cows find the maximum fun value per unit time that they can achieve.

Input

  • Line 1: Two space-separated integers: L and P
  • Lines 2..L+1: Line i+1 contains a single one integer: Fi
  • Lines L+2..L+P+1: Line L+i+1 describes cow path i with three space-separated integers: L1i , L2i , and Ti

Output

  • Line 1: A single number given to two decimal places (do not perform explicit rounding), the maximum possible average fun per unit time, or 0 if the cows cannot plan any trip at all in accordance with the above rules.

Sample Input

5 7
30
10
10
5
10
1 2 3
2 3 2
3 4 5
3 5 2
4 5 5
5 1 3
5 2 2

Sample Output

6.00

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值