最优比率环问题。二分答案,对于每一个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. NO NEWS IS GOOD NEWS

    从客户那传来一个噩耗:要求每个表单在保存之后,要在页面上弹一个 “ 保存成功 ” 的对话框. 客户代表志得意满地说这样用户体验更好,略带谴责意味地傲娇道,“你们早该想到的”.呵呵…… 可不是嘛,我刚入 ...

  2. 如何查看IntelliJ IDEA的版本信息

    作者:13 GitHub:https://github.com/ZHENFENG13 版权声明:本文为原创文章,未经允许不得转载. IDEA的版本信息问题 额,说实在的,这篇文章十三一开始也不是很想整 ...

  3. en

    发音,这个应该算是学习英语的头等大事,如果没有机会和条件练好发音,也可以先将就着,不过后面你就会感觉到你说的人家可能会听不懂,我自己也曾经深受其害. 基本常用单词积累(大概2000~4000左右的词汇 ...

  4. xadmin后台页面的自定制

    01-自定制页面 注:最近找到了更好的解决办法:重写钩子函数版  https://www.cnblogs.com/pgxpython/p/10593507.html 需求背景:根据要实现的功能需求,x ...

  5. POJ - 3264 线段树模板题 询问区间最大最小值

    这是线段树的一个模板题,给出一串数字,然后询问区间的最大最小值. 这个其实很好办,只需把线段树的节点给出两个权值,一个是区间的最小值,一个是区间的最大值,初始化为负无穷和正无穷,然后通过不断地输入节点 ...

  6. Streaming Principal Component Analysis in Noisy Settings

    论文背景: 面对来袭的数据,连续样本不一定是不相关的,甚至不是同分布的. 当前,大部分在线PCA都只关注准确性,而忽视时效性! 噪声?数据缺失,观测有偏,重大异常? 论文内容: Section 2 O ...

  7. Stack Sorting CodeForces - 911E (思维+单调栈思想)

    Let's suppose you have an array a, a stack s (initially empty) and an array b (also initially empty) ...

  8. Masonry练习详解

    添加约束的方式: 1.通过使用NSLayoutConstraints添加约束到约束数组中,之前必须设置translatesAutoresizingMaskIntoConstraints = NO,即取 ...

  9. 使用junit测试

    package creeper; import java.util.Scanner; public class size { private static int intercePosition = ...

  10. [2017BUAA软工助教]团队开发阶段CheckList

    alpha阶段流程与相关节点 以下流程与团队项目中个人的得分点是一一对应的,详见QA文档中"个人在团队项目的得分部分" http://www.cnblogs.com/Childis ...