最优比率环问题。二分答案,对于每一个mid,把节点的happy值归类到边上。

对于每条边,用mid×weight减去happy值,如果不存在负环,说明还可以更大。

 /*--------------------------------------------------------------------------------------*/
// Helica's header
// Second Edition
// 2015.11.7
//
#include <algorithm>
#include <iostream>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <map> //debug function for a N*M array
#define debug_map(N,M,G) printf("\n");for(int i=0;i<(N);i++)\
{for(int j=;j<(M);j++){\
printf("%d",G[i][j]);}printf("\n");}
//debug function for int,float,double,etc.
#define debug_var(X) cout<<#X"="<<X<<endl;
/*--------------------------------------------------------------------------------------*/
using namespace std; const int INF = 0x3f3f3f3f;
const int maxn = 1e3+;
const double eps = 1e-;
int N,M;
int happy[maxn]; struct Edge
{
int to,next,w;
}edge[*maxn]; int head[maxn],cnt[maxn],tol;
double dist[maxn];
bool vis[maxn]; bool SPFA(double mid)
{
memset(vis,false,sizeof(vis));
memset(cnt,,sizeof(cnt));
for(int i=;i<=N;i++)dist[i]=INF;
dist[]=;
queue<int>Q;
Q.push();
while(!Q.empty()){
int u=Q.front();
Q.pop();
vis[u]=false;
cnt[u]++;
if(cnt[u]>N)return true;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].to,w=edge[i].w;
double tmp=mid*w-happy[v];
if(dist[u]+tmp<dist[v]){
dist[v]=dist[u]+tmp;
if(!vis[v]){ vis[v]=true;Q.push(v); }
}
}
}
return false;
}
void add_edge(int u,int v,int w)
{
edge[tol].to = v;
edge[tol].w = w;
edge[tol].next = head[u];
head[u] = tol++;
} int main()
{
//freopen("input.in","r",stdin);
while(~scanf("%d%d",&N,&M))
{
memset(head,-,sizeof head);
tol = ;
for(int i=;i<=N;i++) scanf("%d",&happy[i]);
for(int i=,u,v,w;i<M;i++)
{
scanf("%d%d%d",&u,&v,&w);
add_edge(u,v,w);
}
double L = ,R = 10000.0,mid; while(abs(L-R)>eps)
{
mid = (L+R)/2.0;
if(SPFA(mid)) L = mid;
else R = mid;
} printf("%.2f\n",mid);
}
}

POJ 3621-Sightseeing Cows-最优比率环|SPFA+二分的更多相关文章

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

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

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

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

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

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

  4. POJ3621 Sightseeing Cows(最优比率环)

    题目链接:id=3621">http://poj.org/problem?id=3621 在一个有向图中选一个环,使得环上的点权和除以边权和最大.求这个比值. 经典的分数规划问题,我认 ...

  5. [POJ 3621] Sightseeing Cows

    [题目链接] http://poj.org/problem?id=3621 [算法] 01分数规划(最优比率环) [代码] #include <algorithm> #include &l ...

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. Linux Namespace : Mount

    Mount namespace 为进程提供独立的文件系统视图.简单点说就是,mount namespace 用来隔离文件系统的挂载点,这样进程就只能看到自己的 mount namespace 中的文件 ...

  2. .NET-记一次架构优化实战与方案-底层服务优化

    目录 .NET-记一次架构优化实战与方案-梳理篇 .NET-记一次架构优化实战与方案-前端优化 .NET-记一次架构优化实战与方案-底层服务优化 前言 经过上一篇<.NET-记一次架构优化实战与 ...

  3. c++入门之再话内存和引用

    此处没有代码,仅仅讨论一些这样的问题:我们为何使用引用?在哪里使用引用? 首先从函数的角度思考?:函数进行一般参数传递的时候,是怎么样传递的?普通类型的参数传递,是将传递的实参复制一份,到另一个内存空 ...

  4. Five Dimensional Points CodeForces - 851C (计算几何+暴力)

      C. Five Dimensional Points time limit per test 2 seconds memory limit per test 256 megabytes input ...

  5. ios 后台下载,断点续传总结

    2018年12月05日 16:09:00 weixin_34101784 阅读数:5 https://blog.csdn.net/weixin_34101784/article/details/875 ...

  6. XManager&XShell如何保存登录用户和登录密码

    Xshell配置ssh免密码登录 - qingfeng2556的博客 - CSDN博客https://blog.csdn.net/wuhenzhangxing/article/details/7948 ...

  7. MySQL索引的设计、使用和优化

    原文:http://bbs.landingbj.com/t-0-243071-1.html MySQL索引概述 所有MySQL列类型可以被索引.对相关列使用索引是提高SELECT操作性能的最佳途径.根 ...

  8. python3 操作页面上各种元素的方法

    (1)       控制浏览器 ①控制浏览器窗口大小set_window_size(宽,高) 打开浏览器全屏maximize_window() ②控制浏览器后退back().前进forward() ③ ...

  9. 你不知道的JavaScript——第二章:this全面解析

    1调用位置 调用栈:为了到达当前执行位置所调用的所有函数. function baz(){ //当前调用栈:baz //因此,当前调用位置是全局作用域 console.log('baz'); bar( ...

  10. Flutter之Container详解

    1 基本内容1.1 继续关系Object > Diagnosticable > DiagnosticableTree > Widget > StatelessWidget &g ...