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 ...
随机推荐
- JAVA&PYTHON
- 微信小程序商品筛选,侧方弹出动画选择页面
https://blog.csdn.net/qq_36538012/article/details/85110641
- Brainwashing
[Brainwashing] 1.教育和媒体都是“国家意识机器”,他们维持并复制国家领导者的思想. 2.洗脑.情感.意识. 3.洗脑所产生的各种影响具有如下特征:全用暴力,或者进行欺骗,或二者兼有. ...
- java正则验证
String regex ="[A-Z]{4}[0-9]{7}"; List<String> non= new ArrayList<String>(); f ...
- JMeter学习(二十四)HTTP属性管理器HTTP Cookie Manager、HTTP Request Defaults(转载)
转载自 http://www.cnblogs.com/yangxia-test Test Plan的配置元件中有一些和HTTP属性相关的元件:HTTP Cache Manager.HTTP Autho ...
- 四、API使用参考
官方文档:https://docs.blender.org/api/blender_python_api_current/info_api_reference.html Blender有很多互连数据类 ...
- Google、微软软件测试之道
扫码时备注或说明中留下邮箱 付款后如未回复请至https://shop135452397.taobao.com/ 联系店主
- 我的tensorflow学习1
1.神经元被分成了多层,层与层之间的神经元有连接,而层内之间的神经元没有连接.最左边的层叫做输入层,这层负责接收输入数据:最右边的层叫输出层,我们可以从这层获取神经网络输出数据.输入层和输出层之间的层 ...
- 修改bootstrap-table中的分页样式
使用bootstrap-table时,使用$("")选择器没办法选中下方的分页button按钮,可能跟它是动态生成的有关吧. 最终找到与之对应的js(bootstrap-table ...
- 【nlp】中文分词基础原则及正向最大匹配法、逆向最大匹配法、双向最大匹配法的分析
分词算法设计中的几个基本原则: 1.颗粒度越大越好:用于进行语义分析的文本分词,要求分词结果的颗粒度越大,即单词的字数越多,所能表示的含义越确切,如:“公安局长”可以分为“公安 局长”.“公安局 长” ...