[USACO07DEC]Sightseeing Cows G

2024-01-29 19:38
文章标签 sightseeing cows usaco07dec

本文主要是介绍[USACO07DEC]Sightseeing Cows G,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目描述

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 L1i to L2i (in the direction L1i -> L2i ) 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.

输入格式

  • 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: L1i , L2i , and Ti

输出格式

  • 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.

输入输出样例

输入 #1
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
输出 #1
6.00

.
.
.
.
.
.
分析
题意:一张n个点m条边的有向图,边有花费,点有价值,点可以多次经过但价值不叠加,边花费叠加
求一个环满足收益和/花费和最大
如果知道01分数规划的话,可以一眼发现就是这种题
在这里插入图片描述

二分答案,我们把每条边的权变为f[i]-ans*t[i],这样一旦答案过大,会出现负环,答案过小就是正环

.
.
.
.
.
.
程序:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;int n,m,tot=0,f[100000],head[100000];
bool vis[100000];
double w[100000],dis[100000];struct edge
{int to,d,next;
}e[100000];void add(int x,int y,int z)
{e[++tot].to=y;e[tot].d=z;e[tot].next=head[x];head[x]=tot;
}int read() {int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9') {if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();}return x*f;
}bool spfa(int x)
{vis[x]=true;for (int i=head[x];i;i=e[i].next){int t=e[i].to;if (dis[t]>dis[x]+w[i]){dis[t]=dis[x]+w[i];if (vis[t]||spfa(t)){vis[x]=false;return true;}}}vis[x]=false;return false;
}bool judge()
{for (int i=1;i<=n;i++)if (spfa(i)) return true;return false;
}int main()
{n=read();m=read();for (int i=1;i<=n;i++)f[i]=read();for (int i=1;i<=m;i++){int x,y,z;x=read();y=read();z=read();add(x,y,z);}double l=0,r=20000;while (r-l>0.0000001){double mid=(l+r)/2;for (int i=1;i<=tot;i++)w[i]=(double)mid*e[i].d-f[e[i].to];if (judge()) l=mid; else r=mid;}printf("%.2lf",l);return 0;
}

这篇关于[USACO07DEC]Sightseeing Cows G的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/657964

相关文章

usaco 1.2 Milking Cows(类hash表)

第一种思路被卡了时间 到第二种思路的时候就觉得第一种思路太坑爹了 代码又长又臭还超时!! 第一种思路:我不知道为什么最后一组数据会被卡 超时超了0.2s左右 大概想法是 快排加一个遍历 先将开始时间按升序排好 然后开始遍历比较 1 若 下一个开始beg[i] 小于 tem_end 则说明本组数据与上组数据是在连续的一个区间 取max( ed[i],tem_end ) 2 反之 这个

P2858 [USACO06FEB] Treats for the Cows G/S 题解

P2858 题意 给一个数组。每天把最左或者最右的东西卖掉,第 i i i个东西,第 d a y day day天卖出的价格是 a [ i ] ∗ d a y a[i]*day a[i]∗day。 记忆化搜索 void dfs(int l,int r,int day,ll sum){if(v[l][r]>=sum)return;v[l][r]=sum;if(l>r)//这就是dp答案{

ZOJ 1992 Sightseeing Tour

题意: 判断混合图欧拉回路 思路: (欧拉回路整理 参考 http://zhyu.me/acm/zoj-1992-and-poj-1637.html 和 http://blog.csdn.net/zxy_snow/article/details/6230223) 1.无向图:图连通,且图中均为偶度顶点。 2.有向图:图连通,且图中所有顶点出入度相等。 3.混合图:混合图欧拉回

2181:Jumping Cows

网址如下: OpenJudge - 2181:Jumping Cows (这是我拿去翻译的版本) 简单来说,题目要求我们求出一个子串,在奇数位的数加,偶数位的数减,求总的最大值 用贪心就可以做了 在波峰的时候加,波谷的时候减 代码如下: #include<cstdio>const int maxP = 150000;int P, val[maxP], ans;int

poj 2186 Popular Cows(tarjan + 强连通分量 + 缩点)

http://poj.org/problem?id=2186 题意:有n头牛,m个膜拜关系,膜拜关系是不可逆的而且是单向传递的,比如A膜拜B,B膜拜C,那么A也膜拜C,但B不一定膜拜A。最后问有多少头牛满足条件:除了它自己,其他所有的牛都膜拜它。 思路: 问题可以抽象为:给定一个有向图,n个顶点,m条有向边,有多少个顶点满足:其他所有的点都能到达该点。 首先假如图G是一个有向树

poj 3463 Sightseeing(最短路+次短路)

http://poj.org/problem?id=3463 大致题意:给出一个有向图,从起点到终点求出最短路和次短路的条数之和。 解法: 用到的数组:dis[i][0]:i到起点的最短路,dis[i][1]:i到起点的严格次短路 vis[i][0],vis[i][1]:同一维的vis数组,标记距离是否已确定 sum[i][0]:i到起点的最短路条数,sum[i][1]:i到

POJ 2186 Popular Cows (强连通分量)

题目地址:POJ 2186 先用强连通分量缩点,然后形成一棵树。我第一次用的判定条件是入度为分量数-1。虽然这种情况下确实正确。但是在树中也是有间接关系的。这个条件并不是充分必要条件。正确的做法是逆序建树,然后找根结点。而且根结点有且只有一个才可以。所以转化成了找出度为0的分量。 代码如下: #include <iostream>#include <string.h>#include

UVA10491 - Cows and Cars(概率)

UVA10491 - Cows and Cars(概率) 题目链接 题目大意:给你n个门后面藏着牛,m个门后面藏着车,然后再给你k个提示,在你作出选择后告诉你有多少个门后面是有牛的,现在问你作出决定后,根据提示改变你的选择能够成功的概率。 解题思路:简单的概率题,题目意思懂了应该没什么问题。 代码: #include <cstdio>#include <cstring>int

Bulls and Cows问题及解法

问题描述: You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide

POJ-2186 Popular Cows 强连通 + 缩点

http://poj.org/problem?id=2186 我们求强连通分量时,给每个顶点做一个标记,标记该顶点属于哪个强联通分量,然后属于同一个强连通分量的点就可以看作同一个点了。这就是所谓的“缩点”   此题用了个定理 :有向无环图(DAG)中,从任意一个点出发,必定可以到达某一个出度为0的点。   这个不用证明,直观想一下就行了。  因为无环,所以从一个点出发,必