[题目链接]

http://poj.org/problem?id=3621

[算法]

01分数规划(最优比率环)

[代码]

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXL 1010
#define MAXP 5010
const double eps = 1e-;
const int T = 1e5;
const int INF = 2e9; struct edge
{
int to,w,nxt;
} e[MAXP]; int i,L,P,a,b,w,tot;
int f[MAXL],head[MAXL];
double l,r,mid,ans; inline void addedge(int u,int v,int w)
{
tot++;
e[tot] = (edge){v,w,head[u]};
head[u] = tot;
}
inline bool spfa(double mid)
{
int i,cur,v;
double w;
static int cnt[MAXL];
static bool inq[MAXL];
static double dist[MAXL];
queue< int > q;
memset(cnt,,sizeof(cnt));
memset(inq,false,sizeof(inq));
while (!q.empty()) q.pop();
for (i = ; i <= L; i++)
{
q.push(i);
cnt[i] = ;
inq[i] = true;
dist[i] = -INF;
}
while (!q.empty())
{
cur = q.front();
q.pop();
inq[cur] = false;
for (i = head[cur]; i; i = e[i].nxt)
{
v = e[i].to;
w = 1.0 * f[cur] - 1.0 * mid * e[i].w;
if (dist[cur] + w > dist[v])
{
dist[v] = dist[cur] + w;
if (!inq[v])
{
inq[v] = true;
cnt[v]++;
if (cnt[v] > L) return true;
q.push(v);
}
}
}
}
return false;
} int main()
{ scanf("%d%d",&L,&P);
for (i = ; i <= L; i++) scanf("%d",&f[i]);
for (i = ; i <= P; i++)
{
scanf("%d%d%d",&a,&b,&w);
addedge(a,b,w);
}
l = ; r = T;
while (r - l > eps)
{
mid = (l + r) / 2.0;
if (spfa(mid))
{
ans = mid;
l = mid;
} else r = mid;
}
printf("%.2f\n",ans); return ; }

[POJ 3621] Sightseeing Cows的更多相关文章

  1. POJ 3621 Sightseeing Cows 【01分数规划+spfa判正环】

    题目链接:http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total ...

  2. POJ 3621 Sightseeing Cows(最优比例环+SPFA检测)

    Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10306   Accepted: 3519 ...

  3. POJ 3621 Sightseeing Cows | 01分数规划

    题目: http://poj.org/problem?id=3621 题解: 二分答案,检查有没有负环 #include<cstdio> #include<algorithm> ...

  4. POJ 3621 Sightseeing Cows 01分数规划,最优比例环的问题

    http://www.cnblogs.com/wally/p/3228171.html 题解请戳上面 然后对于01规划的总结 1:对于一个表,求最优比例 这种就是每个点位有benefit和cost,这 ...

  5. POJ 3621 Sightseeing Cows [最优比率环]

    感觉去年9月的自己好$naive$ http://www.cnblogs.com/candy99/p/5868948.html 现在不也是嘛 裸题,具体看学习笔记 二分答案之后判负环就行了 $dfs$ ...

  6. POJ 3621 Sightseeing Cows (bellman-Ford + 01分数规划)

    题意:给出 n 个点 m 条有向边,要求选出一个环,使得这上面 点权和/边权和 最大. 析:同样转成是01分数规划的形式,F / L 要这个值最大,也就是 G(r) = F - L * r 这个值为0 ...

  7. POJ 3621 Sightseeing Cows (最优比率环 01分数划分)

    题意: 给定L个点, P条边的有向图, 每个点有一个价值, 但只在第一经过获得, 每条边有一个花费, 每次经过都要付出这个花费, 在图中找出一个环, 使得价值之和/花费之和 最大 分析: 这道题其实并 ...

  8. [POJ 3621] Sighting Cows

    01分数规划的基本裸题. 因为路线一定是个环,所以找个最优比率生成环即可 二分一个比值,check一下即可. #include <queue> #include <cstdio> ...

  9. POJ3621 Sightseeing Cows 最优比率环 二分法

    题目链接:http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total ...

随机推荐

  1. [转]浏览器缓存详解: expires, cache-control, last-modified, etag详细说明

    最近在对CDN进行优化,对浏览器缓存深入研究了一下,记录一下,方便后来者 画了一个草图: 每个状态的详细说明如下: 1.Last-Modified 在浏览器第一次请求某一个URL时,服务器端的返回状态 ...

  2. SqlSever2005 一千万条以上记录分页数据库优化经验总结【索引优化 + 代码优化】

    对普通开发人员来说经常能接触到上千万条数据优化的机会也不是很多,这里还是要感谢公司提供了这样的一个环境,而且公司让我来做优化工作.当数据库中的记录不超过10万条时,很难分辨出开发人员的水平有多高,当数 ...

  3. SQL Server对数据进行添加

    下面介绍一下SQL Server如何往数据库中加入数据. <!-- 添加主人信息 --> <form action="add_Admin.jsp" method= ...

  4. JavaScript的基本语法(一)

    一.常用的表单元素有: 文本框(text). 密码框(password). 多行文本框(<textarea>) 单选按钮(radio). 复选框(checkbox). 列表框(<se ...

  5. Caffe: Vs13添加CUDA支持

    1.  右键工程 点击:Building Dependency 右击:Build Customizations 点击选项:CUDA 7.5 2.添加C++依赖: cudart.lib kernel32 ...

  6. 安卓代码迁移:Make.exe: *** [libs/armabi-v7a/gdbserver] Error 1

    解决办法1:安装ndk和eclipse修改为x86操作系统 解决办法2:降低更换NDK版本

  7. 计算机图形学课件pdf版

    为方便大家学习,我将自己计算机图形学的课件分享. 下载链接:http://pan.baidu.com/s/1kV5BW8n 密码:eqg4 注:本课件与教材配套PPT有所不同.教材配套PPT是编写教材 ...

  8. POJ_3020_最小路径覆盖

    Antenna Placement Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8721   Accepted: 4330 ...

  9. 13、Next Item Recommendation with Self-Attention---自注意力+CML

    一.摘要: 自注意力机制------从用户历史交互中推断出项目-项目关系.学习每个项目的相对权重[用来学习用户的暂时兴趣表示] 二. 模型: 一部分是用于建模用户短期意图的自注意力机制,一部分是建模用 ...

  10. 【剑指Offer】12、数值的整数次方

      题目描述:   给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方.   解题思路:   本题看似比较简单,是一个简单的指数运算,但需要完 ...