最优比率环问题。二分答案,对于每一个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. 读写分离子系统 - C# SQL分发子系统 - Entity Framework支持

    A2D Framework增加了EF支持,加上原先支持ADO.NET: 支持EF方式 支持ADO.NET方式 这次来讲如何让Entity Framework变成nb的读写分离 1. 先设计EF模型, ...

  2. 撒花!中文翻译仓库链接已加入 ML.NET 官方示例网站首页

    从2018年12月02日决定开始做ML.NET 示例中文版https://github.com/feiyun0112/machinelearning-samples.zh-cn,然后以每天一篇的速度进 ...

  3. mybatis百科-列映射类ResultMapping

    目录 1 成员变量 2 构造函数 3 其他函数 3.1 setter 和 getter 函数 3.2 equals 和 hashCode 函数 3.3 toString 函数 4 内部类 Builde ...

  4. 10分钟开发 GPS 应用,了解一下

    1 前言 在导师公司实习了半个月,参加的是尾气遥测项目,我的任务是开发GPS 的相关事情,从零到有的开发出了 GPS 的 Winform 应用,在这里记录一下开发过程和简要的描述一下将 GPS 数据提 ...

  5. Wechart 饼图

    预览 Preview | Usage Source | Pie Source | Tutorial Wechart by Cax Cax 众所周知 Cax 既能开发游戏.又能开发图表.本文将从饼图开始 ...

  6. 解决win7 win10上网卡慢问题

    在管理员身份运行CMD命令行,然后输入netsh interface tcp set global autotuninglevel=disable 即可.

  7. 初次使用git上传代码到github远程仓库

    https://blog.csdn.net/loner_fang/article/details/80488385 2018年05月28日 21:02:31 蒲公英上的尘埃 阅读数:697 因为最近在 ...

  8. java.lang.LinkageError: JAXB 2.0 API is being loaded from the bootstrap classloader

    我的解决办法:    1.如果是application工程,则在程序中打印出         system.out.println(System.getProperty("java.endo ...

  9. HDU 2001 计算两点间的距离

    http://acm.hdu.edu.cn/showproblem.php?pid=2001 Problem Description 输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离 ...

  10. SpringMvc的Controller singleton synchronized

    SpringMvc的controller是singleton的(非线程安全的) - lvyuanj的专栏 - CSDN博客 https://blog.csdn.net/lvyuanj/article/ ...