[USACO07DEC]Sightseeing Cows
这题好像属于01分数规划问题,叫什么最优比率生成环。
题目概括一下,就是求一个环,满足∑v[i] / ∑c[i]最大。
我们可以堆上面的式子变个型:令 x = ∑v[i] / ∑c[i],则x * ∑c[i] = v[i] => ∑x * c[i] - v[i] = 0。于是对于任何能取到的x',满足∑x * c[i] - v[i] <= 0;对于不能取到的x', ∑x * c[i] - v[i] > 0。
于可以实数二分答案,用spfa判断负环。
然后实数二分又RE了……调了好就还是看了题解。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<stack>
#include<queue>
#include<vector>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-;
const int maxn = 1e3 + ;
inline ll read()
{
ll ans = ;
char ch = getchar(), las = ' ';
while(!isdigit(ch)) las = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << ) + (ans << ) + ch - '', ch = getchar();
if(las == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) putchar('-'), x = -x;
if(x >= ) write(x / );
putchar(x % + '');
} int n, m, val[maxn];
vector<int> v[maxn], c[maxn]; bool in[maxn];
db dis[maxn];
int cnt[maxn];
bool judge(db x)
{
Mem(in, ); Mem(cnt, ); Mem(dis, );
queue<int> q;
for(int i = ; i <= n; ++i) q.push(i);
while(!q.empty())
{
int now = q.front(); q.pop(); in[now] = ;
for(int i = ; i < (int)v[now].size(); ++i)
{
if(dis[v[now][i]] > x * c[now][i] - val[v[now][i]] + dis[now])
{
dis[v[now][i]] = x * c[now][i] - val[v[now][i]] + dis[now];
if(!in[v[now][i]])
{
if(++cnt[v[now][i]] == n - ) return ;
q.push(v[now][i]);
}
}
}
}
return ;
} int main()
{
n = read(); m = read();
for(int i = ; i <= n; ++i) val[i] = read();
for(int i = ; i <= m; ++i)
{
int x = read(), y = read(), co = read();
v[x].push_back(y); c[x].push_back(co);
}
db L = , R = 1e6;
while(R - L > eps)
{
db mid = (L + R) / ;
if(judge(mid)) L = mid;
else R = mid;
}
if(L < eps) write(), enter;
else printf("%.2lf\n", L);
return ;
}
[USACO07DEC]Sightseeing Cows的更多相关文章
- [USACO07DEC]Sightseeing Cows(负环,0/1分数规划)
[USACO07DEC]Sightseeing Cows Description Farmer John has decided to reward his cows for their hard w ...
- P2868 [USACO07DEC]Sightseeing Cows G
题意描述 Sightseeing Cows G 给定一张有向图,图中每个点都有点权 \(a_i\),每条边都有边权 \(e_i\). 求图中一个环,使 "环上个点权之和" 除以 & ...
- P2868 [USACO07DEC]观光奶牛Sightseeing Cows
P2868 [USACO07DEC]观光奶牛Sightseeing Cows [](https://www.cnblogs.com/images/cnblogs_com/Tony-Double-Sky ...
- 洛谷P2868 [USACO07DEC]观光奶牛Sightseeing Cows
P2868 [USACO07DEC]观光奶牛Sightseeing Cows 题目描述 Farmer John has decided to reward his cows for their har ...
- 【POJ3621】Sightseeing Cows
Sightseeing Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8331 Accepted: 2791 ...
- Sightseeing Cows(最优比率环)
Sightseeing Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8915 Accepted: 3000 ...
- 【POJ3621】Sightseeing Cows 分数规划
[POJ3621]Sightseeing Cows 题意:在给定的一个图上寻找一个环路,使得总欢乐值(经过的点权值之和)/ 总时间(经过的边权值之和)最大. 题解:显然是分数规划,二分答案ans,将每 ...
- 【POJ3621】【洛谷2868】Sightseeing Cows(分数规划)
[POJ3621][洛谷2868]Sightseeing Cows(分数规划) 题面 Vjudge 洛谷 大意: 在有向图图中选出一个环,使得这个环的点权\(/\)边权最大 题解 分数规划 二分答案之 ...
- POJ 3621 Sightseeing Cows 【01分数规划+spfa判正环】
题目链接:http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS Memory Limit: 65536K Total ...
随机推荐
- YII框架路由配置
首先要在服务器配置(httpd.conf)中开启重写模块: #开启重写模块,将其前面的#去掉 LoadModule rewrite_module modules/mod_rewrite.so #Dir ...
- nrm的使用
我们在开发过程中,经常会使用到 npm install ,但是有时候npm是不稳定的,这就大大的降低了我们的开发效率.nrm正好解决了我们的这一痛点,他可以在不同的镜像之间切换,非常的方便. 一.n ...
- CORS跨域请求C#版
1.什么是跨域请求: 当从A网站使用AJAX请求B网站时,就会出现跨域请求. 此时B网站能够接收到A网站发来的请求并返回相应的结果,但是浏览器拿到B网站返回的数据时检测到与当前网站的域名不同,出于安 ...
- 实例化geoserver wms服务
var vectorWmsJHdataLayer = new ol.layer.Tile({ source: new ol.source.TileWMS({ //地址 url:'http://loca ...
- 七 FileChannel
FileChannel是一个连接到文件的通道,可以通过文件通道读写文件 FileChannel无法设置为非阻塞模式,它总是运行在阻塞模式下. 打开FileChannel 在使用FileChannel之 ...
- 介绍一款小众的IDE
作为前端工程师的你们平时主要使用什么IDE,atom.webstorm.sublime还是vscode? 今天介绍一款比较小众的IDE,Adobe的开源项目Brackets,提供Windows和OS ...
- 学习stylus笔记
最近在研究v-cli3.0,发现了一种新的预处理器,于是花了一些时间去学习下. 学习网站 基本上这个网站上,讲的已经很详情.我下面把我在学习之中的笔记和觉得自己用的多方法贴出来. 1.缩排 使用缩排和 ...
- Makefile一 头文件及库搜索路径
头文件及库搜索路径 头文件的搜索路径: 头文件的搜索规则是:找到就使用,停止继续往下寻找 1: #include “mytest.h” 搜索的顺序为: (1)先搜索当前目录 (2)然后搜索编译时 -I ...
- 28_Future模式1
[Future模式] Future模式类似商品订单.比如在网购时,当看中一件商品时,就可以提交订单,当订单处理完成后,在家里等待商品送货上门即可.或者类似我们发送Ajax请求的时候,页面是异步的进行后 ...
- 【Python】猜数小游戏(文件操作)
人生苦短,我用Python 关键词 1.多用户 2.字典记录所有成绩 3.每次游戏轮数&总游戏次数&平均每次游戏需要多少轮 字典Dictionary.列表List.元组Tuple差异化 ...