[USACO07DEC]Sightseeing Cows(负环,0/1分数规划)
[USACO07DEC]Sightseeing Cows
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 L_1_i to L_2_i (in the direction L_1_i -> L_2_i ) 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: L_1_i , L_2_i , 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
题意概述:
给定一张有向图,每个点求出一个权值\(fun[i]\),每条边有一个权值\(time[i]\)。求图中的一个环,使得“环上各点的权值和”/“环上各边的权值和”最大。输出这个最大值。
显然的0/1分数规划。
每次二分出最大值,重新建边。由一般的0/1分数规划思路,我们需要确定环上是否有一个环是正环,但是这样不容易判断。所以我们把环的权值取反,这样就可以通过判断负环来\(check\)了。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int read()
{
int x=0,w=1;char ch=getchar();
while(ch>'9'||ch<'0') {if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return x*w;
}
int n,m,cnt,flag;
double inf=2000000000;
int head[1010],x[50010],y[50010],vis[1010];
double a[1010],z[50010],dis[1010];
struct node{
int to,next;double v;
}edge[10010];
void add(int x,int y,double z)
{
cnt++;edge[cnt].to=y;edge[cnt].v=z;edge[cnt].next=head[x];head[x]=cnt;
}
void spfa(int k)
{
vis[k]=1;
for(int i=head[k];i;i=edge[i].next)
{
int v=edge[i].to;
if(dis[v]>dis[k]+edge[i].v)
{
if(vis[v]) {flag=1;return;}
dis[v]=dis[k]+edge[i].v;
spfa(v);if(flag) return;
}
}
vis[k]=0;
}
bool check(double k)
{
cnt=0;flag=0;for(int i=1;i<=n;i++)head[i]=vis[i]=0,dis[i]=inf;
for(int i=1;i<=m;i++) add(x[i],y[i],k*z[i]-a[x[i]]);
for(int i=1;i<=n;i++){spfa(i);if(flag)return true;}
return false;
}
int main()
{
n=read();m=read();double l=0,r=0,mid;
for(int i=1;i<=n;i++) scanf("%lf",&a[i]),r+=a[i];
for(int i=1;i<=m;i++) x[i]=read(),y[i]=read(),scanf("%lf",&z[i]);
while(r-l>1e-4)
{
mid=(l+r)/2;
if(check(mid)) l=mid;
else r=mid;
}
printf("%.2lf",r);
}
[USACO07DEC]Sightseeing Cows(负环,0/1分数规划)的更多相关文章
- Contest20140710 loop bellman-ford求负环&&0/1分数规划
loop|loop.in|loop.out 题目描述: 给出一个有向带权图,权为边权,求一个简单回路,使其平均边权最小. 简单回路指不多次经过同一个点的回路. 输入格式: 第一行两个整数,表示图的点数 ...
- P2868 [USACO07DEC]Sightseeing Cows G
题意描述 Sightseeing Cows G 给定一张有向图,图中每个点都有点权 \(a_i\),每条边都有边权 \(e_i\). 求图中一个环,使 "环上个点权之和" 除以 & ...
- bzoj3232圈地游戏——0/1分数规划+差分建模+判环
Description DZY家的后院有一块地,由N行M列的方格组成,格子内种的菜有一定的价值,并且每一条单位长度的格线有一定的费用. DZY喜欢在地里散步.他总是从任意一个格点出发,沿着格线行走直到 ...
- Bzoj1486/洛谷P3199 最小圈(0/1分数规划+spfa)/(动态规划+结论)
题面 Bzoj 洛谷 题解(0/1分数规划+spfa) 考虑\(0/1\)分数规划,设当前枚举到的答案为\(ans\) 则我们要使(其中\(\forall b_i=1\)) \[ \frac{\sum ...
- poj2728 Desert King【最优比率生成树】【Prim】【0/1分数规划】
含[最小生成树Prim]模板. Prim复杂度为$O(n^2),适用于稠密图,特别是完全图的最小生成树的求解. Desert King Time Limit: 3000MS Memory Li ...
- bzoj 3232 圈地游戏——0/1分数规划(或网络流)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3232 当然是0/1分数规划.但加的东西和减的东西不在一起,怎么办? 考虑把它们合在一起.因为 ...
- poj 2976 Dropping tests 0/1分数规划
0/1分数规划问题,用二分解决!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> # ...
- bzoj 3597: [Scoi2014]方伯伯运椰子 0/1分数规划
3597: [Scoi2014]方伯伯运椰子 Time Limit: 30 Sec Memory Limit: 64 MBSubmit: 144 Solved: 78[Submit][Status ...
- LOJ 3089 「BJOI2019」奥术神杖——AC自动机DP+0/1分数规划
题目:https://loj.ac/problem/3089 没想到把根号之类的求对数变成算数平均值.写了个只能得15分的暴力. #include<cstdio> #include< ...
随机推荐
- TCP服务器并发编程构架:完成端口IOCP模式
windows下socket网络编程模式:IOCP 完成端口 1)IOCP异步事件的获取放到操作系统的网络驱动层来处理,实际上反而是降低了编程难度, 2)同时对于多线程的并发调度,也放到操作系统级别来 ...
- ARC093 F - Dark Horse
https://atcoder.jp/contests/arc093/tasks/arc093_d 题解 先钦定\(1\)号站在第一个位置上,那么他第一轮要和\((2)\)打,第二轮要和\((3,4) ...
- vue-router 2.0 跳转之router.push()
router.push(location) 除了使用 创建 a 标签来定义导航链接,我们还可以借助 router 的实例方法,通过编写代码来实现. router.push(location) 想要导航 ...
- Window7 系统下重新建立一个新分区
为了方便使用,准备在原来分区上再分割出一个分区,步骤如下 首先右击计算机,选择管理打开计算机管理窗口,选择磁盘管理,当前窗口右侧会出现当前计算机所有已存在的分区列表. 选择要进行分区的磁盘,右击选择压 ...
- C# 防火墙操作之特定程序
将特定程序加入防火墙组,与将特定端口加入防火墙流程类似.详情见“C# 防火墙操作之特定端口”.其主要代码为: /// <summary> /// 允许应用程序通过防火墙 /// </ ...
- 架构-层-BLL:BLL
ylbtech-架构-层-BLL:BLL 业务逻辑层(Business Logic Layer)无疑是系统架构中体现核心价值的部分.它的关注点主要集中在业务规则的制定.业务流程的实现等与业务需求有关的 ...
- 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_04 数据结构_4_数据结构_链表
查询慢,增删快. 绿色代表一条链 红色是另外一条链 .查询是从头开始查所以慢. 在300和55之间添加一个元素
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_01 File类_8_File类遍历(文件夹)目录功能
遍历这个目录下的文件 遍历一个文件就会报错’ 不存在的路径,也会报空指针异常 遍历可以获取到隐藏的文件夹和文件.常见一个隐藏的文件和文件夹 ListFiles私有类型的数组
- curl发json
linux 模拟post请求 curl -X POST \ -H "Content-Type: application/json" \ -H "token:GXJP1cl ...
- 测开之路一百一十三:bootstrap媒体对象
实现效果,左边是图片或者其他媒体,右边是对应的描述 引入bootstrap和jquery标签 class="media" 数量多一些看着就会很规整 <!DOCTYPE htm ...