本文主要是介绍洛谷 P2868 观光奶牛Sightseeing Cows 01分数规划 + 最短路判负环,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
按照惯例,不想写题目大意,转一个
https://blog.csdn.net/liangzihao1/article/details/79716799
题目描述
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.
作为对奶牛们辛勤工作的回报,Farmer John决定带她们去附近的大城市玩一天。旅行的前夜,奶牛们在兴奋地讨论如何最好地享受这难得的闲暇。 很幸运地,奶牛们找到了一张详细的城市地图,上面标注了城市中所有L(2 <= L <= 1000)座标志性建筑物(建筑物按1..L顺次编号),以及连接这些建筑物的P(2 <= P <= 5000)条道路。按照计划,那天早上Farmer John会开车将奶牛们送到某个她们指定的建筑物旁边,等奶牛们完成她们的整个旅行并回到出发点后,将她们接回农场。由于大城市中总是寸土寸金,所有的道路都很窄,政府不得不把它们都设定为通行方向固定的单行道。 尽管参观那些标志性建筑物的确很有意思,但如果你认为奶牛们同样享受穿行于大城市的车流中的话,你就大错特错了。与参观景点相反,奶牛们把走路定义为无趣且令她们厌烦的活动。对于编号为i的标志性建筑物,奶牛们清楚地知道参观它能给自己带来的乐趣值F_i (1 <= F_i <= 1000)。相对于奶牛们在走路上花的时间,她们参观建筑物的耗时可以忽略不计。 奶牛们同样仔细地研究过城市中的道路。她们知道第i条道路两端的建筑物 L1_i和L2_i(道路方向为L1_i -> L2_i),以及她们从道路的一头走到另一头所需要的时间T_i(1 <= T_i <= 1000)。 为了最好地享受她们的休息日,奶牛们希望她们在一整天中平均每单位时间内获得的乐趣值最大。当然咯,奶牛们不会愿意把同一个建筑物参观两遍,也就是说,虽然她们可以两次经过同一个建筑物,但她们的乐趣值只会增加一次。顺便说一句,为了让奶牛们得到一些锻炼,Farmer John要求奶牛们参观至少2个建筑物。 请你写个程序,帮奶牛们计算一下她们能得到的最大平均乐趣值。
输入输出格式
输入格式:
* 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
输出格式:
* 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.输入输出样例
输入样例#1:
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
输出样例#1:
6.00大意:在图中找一个环,求环上点权/边权的最大值。
分析:很显然的01分数规划,之前了解过,但不知道这是01分数规划(好高级)。
我们二分一个答案mid,假设答案能更大,有F/T>mid,那么可以看作是mid*T-F<0。看作每条边的贡献就是mid*w-f[x],也就是边权*估计答案-入点权(也可以看作出点权,因为是环,点数=边数,不会算重)。跑spfa求负环,如果存在,则答案可以更大,且该状态合法。
spfa判断负环不一定要用每个点经过n次,也可以设cnt[x]为x的最短路经过的边数,cnt[x]>=n则存在负环。
01分数规划的第一题
#include<stdio.h>
#include<iostream>
#include<queue>#define rep(i, j, k) for (int i=j; i<k; i++)
#define ll long long
#define dprintf if (debug) printf
const int maxn = 1050;
const int maxm = 5050;
const ll INF = 1e10;
using namespace std;double dis[maxn];
int tail[maxn], fr, to, w, n, m, v[maxn], in[maxn], cnt, vis[maxn];
double l, r, mid, ans;struct Edge{int to, w, nxt;
}edges[maxm*2];
queue<int> Q;
void addEdge(int fr, int to, int w){edges[++cnt].to = to;edges[cnt].w = w;edges[cnt].nxt = tail[fr];tail[fr] = cnt;
}
int spfa(double ans){while(!Q.empty()) Q.pop();rep(i, 1, n+1){dis[i] = INF;in[i] = 0;vis[i] = 0;}dis[1] = 0; Q.push(1); vis[1] = 1;while (!Q.empty()){int now = Q.front(); Q.pop();for (int i=tail[now]; i; i=edges[i].nxt){int to = edges[i].to; double w = (double) edges[i].w * ans - v[to];if (dis[now] + w < dis[to]){dis[to] = dis[now] + w;in[to] = in[now] + 1;if (in[to] >= n) return 1;if (!vis[to]){vis[to] = 1;Q.push(to);}}}vis[now] = 0;}return 0;
}
int main(){scanf("%d%d", &n, &m);rep(i, 1, n+1){scanf("%d", &v[i]);}rep(i, 0, m){scanf("%d%d%d", &fr, &to, &w);addEdge(fr, to, w);}l = 1; r = 1e7;while (l <= r){mid = (l + r)/2;if (spfa(mid/10000)) {l = mid+1; ans = mid;}//you fu huanelse r = mid-1;}printf("%.2f", ans/10000);
}
打的草稿 当然,这里的sumv 和 sumt表示 点权 和 边权,x表示当前二分假设的答案(当然,这里的sumv和sumt是由点权集和边权集组合成的)
sumv/sumt = ans
sumv - ans * sumt = 0
if there exist sumv - x * sumt > 0 (x * sumt - sumv < 0) then there exist x2 > x s.t. v - x2 * t = 0
我们考虑一下,什么时候存在 x * sumt - sumv < 0?
sumt 和 sumv 看似没关系,但是里面的元素是成对出现的,比如我们选择一条边 i,那么我们可以把edges[i].w 和 edges[i].to 组合一下,那么就变成了典型的01分数规划了!然而还有一个要求,选择的元素必须相邻且构成环(奶牛要回到出发点)
所以当我们判断是否存在x * sumt - sumv < 0,即 sigma {( x * edges[i].w - edges[i].to) * xuanbuxuan[i]} < 0时,可将原图的所有边权改成 x * edges[i].w - edges[i].to 判断是否存在一个负环!!
所以这个题是01分数规划+判负环
用spfa判断负环时,有个技巧。 平时看的很多版本里都是判断一个节点进队的次数>=n,
我转的这篇博客里用的是: in[to] = in[now] + 1;
貌似速度要更快!!
这篇关于洛谷 P2868 观光奶牛Sightseeing Cows 01分数规划 + 最短路判负环的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!