POJ 3621 Sightseeing Cows 最优比率环
来源:互联网 发布:手机屏幕亮点修复软件 编辑:程序博客网 时间:2024/05/16 17:22
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 73010105101 2 32 3 23 4 53 5 24 5 55 1 35 2 2
Sample Output
6.00
Source
给了一个有向图,每个点有点权,每条边有边权。求所有环当中,点权和除以边权和最大是多少。
二分答案。
对于一个二分的答案mid,分数规划的分子是点权ai和,分母是点权bi和,则分数规划的函数是sigma(ai-mid*bi).发现环上的点数和边数肯定相等,于是可以在更新边权的同时顺便更新点权,于是只要判断是否存在一个正环就可以。
正向环不是很好求。不过,负环可以用spfa判断。这样,我们就可以先二分答案,每次再通过spfa判负环。判的时候,把点权取负,边权与mid的乘积取整。(把分数规划函数ai-mid*bi取反)
#include <cstdio>#include <iostream>#include <string.h>#include <queue>#include <algorithm>#include <math.h>#include <cmath>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;typedef double db;const int maxn=1005,maxk=5005,inf=0x3f3f3f3f; const ll llinf=0x3f3f3f3f3f3f3f3f; const db eps = 1e-5;int head[maxn],t[maxn];bool inq[maxn];db a[maxn],d[maxn];int num;struct Edge {int from,to,pre;db d;};Edge e[maxk*2];void addedge(int from,int to,db d) {e[num].from=from,e[num].to=to,e[num].pre=head[from],e[num].d=d;head[from]=num++;}bool spfa(int n,db mid) {queue<int> q;mem0(t); mem0(inq);for (int i=1;i<=n;i++) d[i]=1e9;inq[1] = 1; q.push(1); d[1] = 0;while (!q.empty()) {int now = q.front(); q.pop();inq[now] = 0;for (int i = head[now]; i != -1; i = e[i].pre) {int to = e[i].to;if (d[now] + mid*e[i].d - a[now] < d[to]) {d[to] = d[now] + mid*e[i].d - a[now];if (!inq[to]) {inq[to] = 1; q.push(to); t[to]++;if (t[to] > n) return true;}}}}return false;}int main() {int n, m, i, j;db l, r, mid, ans;num = 0; memset(head, -1, sizeof(head));scanf("%d%d", &n, &m);for (i = 1; i <= n; i++) scanf("%lf", &a[i]);int x, y;for (i = 1; i <= m; i++) {scanf("%d%d%lf", &x, &y, &r);addedge(x, y, r);}l = 0; r = 1e4;while (true) {mid = (l + r) / 2.0;if (spfa(n,mid)) l = mid; else r = mid;if (fabs(r - l) < eps) break;}printf("%.2lf\n", mid);return 0;}
- POJ 3621 Sightseeing Cows 最优比率环
- POJ 3621 Sightseeing Cows 最优比率环
- poj 3621 Sightseeing Cows(最优比率环)
- poj 3621 Sightseeing Cows (最优比率环)
- POJ-3621: Sightseeing Cows【最优比率生成环】
- poj 3621 Sightseeing Cows (最优比率生成环 01分数规划问题 )
- POJ 3621 Sightseeing Cows(最优比率环/01分数规划)
- POJ3621 Sightseeing Cows(最优比率环)
- POJ3621 Sightseeing Cows 最短路求最优比率生成环
- 【分数规划】【最优比率环 & 最优比率生成树】poj3621 Sightseeing Cows && poj2728 Desert King
- poj 3621(最优比率环)
- POJ 3621 最优比率环
- poj 3621 Sightseeing Cows(最优比例生成环,01分数规划)
- poj 3621 Sightseeing Cows ( 求最优比例生成环/01分数规划 )
- poj 3621 Sightseeing Cows 【最优比例环】 【0-1分数规划 + SPFA判负环】
- POJ 3621 Sightseeing Cows
- poj 3621 Sightseeing Cows
- poj 3621 Sightseeing Cows
- BZOJ 1178 [Apio2009]CONVENTION会议中心【】
- Swing入门和布局管理
- uva 10137 The Trip
- 学习笔记——求解素数问题之Eratosthenes(埃拉托色尼)筛选法
- hdu 5901 n以内素数计数
- POJ 3621 Sightseeing Cows 最优比率环
- numpy生成随机数random模块
- 常见HTTP错误代码大全
- 欢迎使用CSDN-markdown编辑器
- python3 循环
- @Autowired 注释
- 安卓笔记(3)用户名密码输入框
- arraylist与linkedlist区别
- Unity 自制工具:Point。方便标记出生点或巡逻点等功能。