洛谷 P2868 [USACO07DEC]观光奶牛Sightseeing Cows 题解
这道题是一道标准的01分数规划:
但是有一些细节可以优化:
不难想到要二分一个mid然后判定图上是否存在一个环S,该环是否满足∑i=1t(Fun[vi]−mid∗Tim[ei])>0
但是上面的算法并不好实现,所以可以将两边同时乘上-1,使式子变为∑i=1t(mid∗Tim[ei]−Fun[vi])<0
那么该问题就转化成了在每一个图中跑一边SPFA来寻找是否存在负环,若存在则l=mid,否则r=mid;
#include <bits/stdc++.h>
#define inc(a,b,c) for(register int i=a;i<=b;i+=c)
#define ini 20010
using namespace std;
int n,m;
struct littlestar{
int from;
int to;
int nxt;
double w;
}star[ini],star2[ini];
int head[ini],cnt,head2[ini];
void add(int u,int v,int w)
{
star[++cnt].to=v;
star[cnt].nxt=head[u];
star[cnt].from=u;
star[cnt].w=w;
head[u]=cnt;
}
int c[ini];
queue<int> q;
double dis[ini];
int vis[ini];
int SPFA()
{
int tot=;
for(int i=;i<=n;i++){
q.push(i);
dis[i]=;
vis[i]=;
}
while(q.size()){
int u=q.front();
q.pop();
vis[u]=;
for(int i=head2[u];i;i=star2[i].nxt){
int v=star2[i].to;
if(dis[v]>dis[u]+star2[i].w){
dis[v]=dis[u]+star2[i].w;
if(vis[v]==){
vis[v]=;
q.push(v);
++tot;
if(tot>*(n+m)) return ;
}
}
}
}
return ;
}
int check(double x)
{
inc(,cnt,){
star2[i]=star[i];
star2[i].w=(double)star[i].w*(double)x-(double)c[star[i].from];
}
inc(,n,){
head2[i]=head[i];
}
if(SPFA()){
return ;
}
else{
return ;
}
}
int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-; ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-''; ch=getchar();}
return x*f;
}
int main()
{
n=read();
m=read();
inc(,n,) c[i]=read();
inc(,m,){
int u,v,w;
u=read();v=read();w=read();
add(u,v,w);
}
double l=0.00,r=1000010.000,mid;
while(r-l>1e-){
mid=(l+r)/;
if(check(mid)){
l=mid;
}
else{
r=mid;
}
}
printf("%.2lf",l);
}
洛谷 P2868 [USACO07DEC]观光奶牛Sightseeing Cows 题解的更多相关文章
- 洛谷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\),我们需要检查\(\ ...
- POJ3621或洛谷2868 [USACO07DEC]观光奶牛Sightseeing Cows
一道\(0/1\)分数规划+负环 POJ原题链接 洛谷原题链接 显然是\(0/1\)分数规划问题. 二分答案,设二分值为\(mid\). 然后对二分进行判断,我们建立新图,没有点权,设当前有向边为\( ...
- 洛谷 2868 [USACO07DEC]观光奶牛Sightseeing Cows
题目戳这里 一句话题意 L个点,P条有向边,求图中最大比率环(权值(Fun)与长度(Tim)的比率最大的环). Solution 巨说这是0/1分数规划. 话说 0/1分数规划 是真的难,但貌似有一些 ...
- P2868 [USACO07DEC]观光奶牛Sightseeing Cows
P2868 [USACO07DEC]观光奶牛Sightseeing Cows [](https://www.cnblogs.com/images/cnblogs_com/Tony-Double-Sky ...
- 洛谷 P3088 [USACO13NOV]挤奶牛Crowded Cows 题解
P3088 [USACO13NOV]挤奶牛Crowded Cows 题目描述 Farmer John's N cows (1 <= N <= 50,000) are grazing alo ...
- [USACO07DEC]观光奶牛Sightseeing Cows 二分答案+判断负环
题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...
随机推荐
- linux中more命令如何使用
more命令,功能类似 cat ,cat命令是整个文件的内容从上到下显示在屏幕上.兄弟连Linux培训教程() more会以一页一页的显示方便使用者逐页阅读,而最基本的指令就是按空白键(sp ...
- 【Python之路】特别篇--Python线程池
版本一: #!/usr/bin/env python # -*- coding:utf-8 -*- import Queue import threading class ThreadPool(obj ...
- 2018 计蒜之道 初赛 第五场 A 贝壳找房搬家
贝壳找房换了一个全新的办公室,每位员工的物品都已经通过搬家公司打包成了箱子,搬进了新的办公室了,所有的箱子堆放在一间屋子里(这里所有的箱子都是相同的正方体),我们可以把这堆箱子看成一个 x*y*z 的 ...
- 【杂题】[CodeForces 1172D] Nauuo and Portals【构造】
Description 有一个n*n的网格,你需要在上面设置一些传送门,传送门由两个配对的格子组成,从一个进入会立刻从另一个同一方向出来. 现在有n个人从第1列出发向右走,位于(i,1)的人要走到(r ...
- k8s删除节点后再重新添加进去(踩坑)
开启本地集群,发现一台节点出问题了,想删除再换一台节点,结果就踩坑了,还好本地有好几套环境. 再master节点执行以下命令 [root@k8s-master ~]# kubectl drain k8 ...
- HashMap、Hashtable、HashSet三种hash集合的区别
转载:http://www.cnblogs.com/lzrabbit/p/3721067.html#h1 HashMap和Hashtable的区别 两者最主要的区别在于Hashtable是线程安全,而 ...
- 程序代码运行结果是(abdcbdcb)
public class Test { public static boolean show(char ch) { System.out.print(ch); return true; } publi ...
- _parameter:解决There is no getter for property named in class java.lang.String
我们知道在mybatis的映射中传参数,只能传入一个.通过#{参数名} 即可获取传入的值. Mapper接口文件: public int delete(int id) throws Exception ...
- 【CentOS】yum安装教训
前言:本来想安装sl在新安装的centos7上,网上搜了教程,很多都是先要你yum -y update,如下: 1.更新yum源: yum -y update 2.依赖安装: wget http:// ...
- shiro 注解不生效
springboot集成shiro时,使用@RequiresRoles注解不生效. shiro @RequiresRoles底层使用springaop技术: 1.@RequiresRoles修饰ser ...