POJ3621或洛谷2868 [USACO07DEC]观光奶牛Sightseeing Cows
一道\(0/1\)分数规划+负环
POJ原题链接
洛谷原题链接
显然是\(0/1\)分数规划问题。
二分答案,设二分值为\(mid\)。
然后对二分进行判断,我们建立新图,没有点权,设当前有向边为\(z=(x,y)\),\(time\)为原边权,\(fun\)为原点权,则将该边权换成\(mid\times time[z]+fun[x]\),然后在上面跑\(SPFA\)。
如果有一个环使得\(\sum\{mid\times time[z]+fun[x]\}<0\),则说明\(mid\)小了,而式子表示的意义就是原图存在负环;如果有环使得该式\(\geqslant0\),那么说明答案不超过\(mid\),这时\(SPFA\)就会正常结束。
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 1010;
const int M = 5010;
int fi[N], di[M], ne[M], da[M], fun[N], q[N << 10], cnt[N], l, n;
bool v[N];
double dis[N];
inline int re()
{
int x = 0;
char c = getchar();
bool p = 0;
for (; c<'0' || c>'9'; c = getchar())
p |= c == '-';
for (; c >= '0'&&c <= '9'; c = getchar())
x = x * 10 + (c - '0');
return p ? -x : x;
}
inline void add(int x, int y, int z)
{
di[++l] = y;
da[l] = z;
ne[l] = fi[x];
fi[x] = l;
}
bool judge(double mid)
{
int head = 0, tail = 1, i, x, y;
double z;
bool p = 1;
memset(dis, 66, sizeof(dis));
memset(v, 0, sizeof(v));
memset(cnt, 0, sizeof(cnt));
dis[1] = 0;
q[1] = 1;
while (head != tail && p)
{
x = q[++head];
v[x] = 0;
for (i = fi[x]; i; i = ne[i])
{
y = di[i];
z = mid * da[i] - fun[x];
if (dis[y] > dis[x] + z)
{
dis[y] = dis[x] + z;
cnt[y] = cnt[x] + 1;
if (cnt[y] >= n)
{
p = 0;
break;
}
if (!v[y])
{
q[++tail] = y;
v[y] = 1;
}
}
}
}
if (p)
return false;
return true;
}
int main()
{
int i, m, x, y, z;
double l = 0, r = 0, mid;
n = re();
m = re();
for (i = 1; i <= n; i++)
fun[i] = re();
for (i = 1; i <= m; i++)
{
x = re();
y = re();
z = re();
r += z;
add(x, y, z);
}
r /= 2;
while (l + 1e-3 < r)
{
mid = (l + r) / 2;
if (judge(mid))
l = mid;
else
r = mid;
}
printf("%.2f", l);
return 0;
}
POJ3621或洛谷2868 [USACO07DEC]观光奶牛Sightseeing Cows的更多相关文章
- 洛谷 2868 [USACO07DEC]观光奶牛Sightseeing Cows
题目戳这里 一句话题意 L个点,P条有向边,求图中最大比率环(权值(Fun)与长度(Tim)的比率最大的环). Solution 巨说这是0/1分数规划. 话说 0/1分数规划 是真的难,但貌似有一些 ...
- 洛谷P2868 [USACO07DEC]观光奶牛Sightseeing Cows
P2868 [USACO07DEC]观光奶牛Sightseeing Cows 题目描述 Farmer John has decided to reward his cows for their har ...
- 洛谷P2868 [USACO07DEC]观光奶牛 Sightseeing Cows
题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...
- 洛谷 P2868 [USACO07DEC]观光奶牛Sightseeing Cows
题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...
- 洛谷P2868 [USACO07DEC]观光奶牛Sightseeing Cows(01分数规划)
题意 题目链接 Sol 复习一下01分数规划 设\(a_i\)为点权,\(b_i\)为边权,我们要最大化\(\sum \frac{a_i}{b_i}\).可以二分一个答案\(k\),我们需要检查\(\ ...
- 洛谷 P2868 [USACO07DEC]观光奶牛Sightseeing Cows 题解
题面 这道题是一道标准的01分数规划: 但是有一些细节可以优化: 不难想到要二分一个mid然后判定图上是否存在一个环S,该环是否满足∑i=1t(Fun[vi]−mid∗Tim[ei])>0 但是 ...
- Luogu 2868 [USACO07DEC]观光奶牛Sightseeing Cows
01分数规划复习. 这东西有一个名字叫做最优比率环. 首先这个答案具有单调性,我们考虑如何检验. 设$\frac{\sum_{i = 1}^{n}F_i}{\sum_{i = 1}^{n}T_i} = ...
- P2868 [USACO07DEC]观光奶牛Sightseeing Cows
P2868 [USACO07DEC]观光奶牛Sightseeing Cows [](https://www.cnblogs.com/images/cnblogs_com/Tony-Double-Sky ...
- [USACO07DEC]观光奶牛Sightseeing Cows 二分答案+判断负环
题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...
随机推荐
- django中使用mysql数据库的事务
django中怎么使用mysql数据库的事务 Mysql数据库事务: 在进行后端业务开始操作修改数据库时,可能会涉及到多张表的数据修改,对这些数据的修改应该是一个整体事务,即要么一起成功,要么一起 ...
- php导出excel不知道列数 php26进制函数
function num2Letter($num) { $num = intval($num); if ($num <= 0) return false; $letterArr = array( ...
- com.google.gson.stream.MalformedJsonException
今天下午项目更新后,调用接口时,提示“请求失败”,发现项目网络框架解析时,报错如下:com.google.gson.stream.MalformedJsonException: Unterminate ...
- CSS中的通用字体
往往设计师在设计时一厢情愿地去思考问题,那样会带来很多潜在的麻烦.事实上你可以为网页设计任意字体,包括艺术字体.也许你的计算机中已经安装了很多字体,但是不能够保证所有的浏览者都已经安装了相同的字体,因 ...
- Could not load conf for core new_core 解決方法
new_core: org.apache.solr.common.SolrException:org.apache.solr.common.SolrException: Could not load ...
- 【scrapy_redis】调试后爬取了部分数据,然后重新调试时,直接被去重机制过滤掉无法重头开始爬取
这2天遇到一个问题,之前调试的时候爬取了一些数据,结果第二天重新调试的时候发现爬虫很快结束,而且还没有报错.后来从日志里看到这个: no more duplicates will be shown ( ...
- HTTP协议原理(长连接,短连接/ get,post区别等等)
HTTP简介 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的传送 ...
- 155. Min Stack (stack)
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- 24 【python入门指南】class
一.类 1.1,构造函数,析构函数 #!/bin/python class dog(): def __init__(self, age, name): self.age = age self.name ...
- UVa 10054 The Necklace(无向图欧拉回路)
My little sister had a beautiful necklace made of colorful beads. Two successive beads in the neckla ...