poj 2976+poj3621(01分数规划

本文深入解析了分数规划问题,通过两个实例:一是求在去除指定数量课程后平均成绩的最大值;二是寻找点权之和与边权之和比率最大的环。文章详细介绍了如何将求值问题转化为判定问题,并运用二分法进行求解,同时提供了C++代码实现。

题目:每门课有实际成绩a和总成绩b,要求去掉k门课后能得到的平均成绩的最大值。

思路:裸的分数规划题。一个非常经典的解决问题的思路就是把求值变为判定问题,然后进行2分,分数规划就是这类思想的一个应用。、

具体讲解参考此文章:http://blog.youkuaiyun.com/hhaile/article/details/8883652

/*
* @author:  Cwind
* http://www.cnblogs.com/Cw-trip/
* 蒟蒻只能做几个水题。。
*/
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <map>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <set>
#include <cmath>
using namespace std;
#define IOS std::ios::sync_with_stdio (false);std::cin.tie(0)
#define pb push_back
#define PB pop_back
#define bk back()
#define fs first
#define se second
#define sq(x) (x)*(x)
#define eps (1e-3)
#define IINF (1<<29)
#define LINF (1ll<<59)
#define FINF (1e100)
#define INF 1000000000
const double pi=acos(-1.0);
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> P;

const int maxn=1005;
int n,k;
double a[maxn],b[maxn];
double c[maxn];
bool check(double p){
    for(int i=0;i<n;i++){
        c[i]=a[i]-p*b[i];
    }
    sort(c,c+n,greater<double>());
    double sum=0;
    for(int i=0;i<n-k;i++){
        sum+=c[i];
    }
    if(sum<0) return 0;
    else return 1;
}
int main(){
    freopen("/home/files/CppFiles/in","r",stdin);
    while(cin>>n>>k){
        if(n==0&&k==0) break;
        for(int i=0;i<n;i++){
            scanf("%lf",&a[i]);
            a[i]*=100;
        }
        for(int i=0;i<n;i++){
            scanf("%lf",&b[i]);
        }
        double l=0,r=100;
        while(r-l>eps){
            double mid=(r+l)/2;
            if(check(mid)){
                l=mid;
            }else{
                r=mid;
            }
        }
        printf("%.0f\n",l);
    }
    return 0;
}
View Code

 poj3621(最优比率环

题目:求一个环使得环上的点权之和比边权之和最大。

思路:把点权转移到边上,01分数规划求解。

这里用到了一个黑科技:dfs加速的spfa求找负圈,主要是先把所有dis初始化为0,然后dfs,如果遇到之前栈内的点,就是找到负圈,此算法容易实现,效率很高。

/*
* @author:  Cwind
* http://www.cnblogs.com/Cw-trip/
* 蒟蒻只能做几个水题。。
*/
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <map>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <set>
#include <cmath>
using namespace std;
#define IOS std::ios::sync_with_stdio (false);std::cin.tie(0)
#define pb push_back
#define PB pop_back
#define bk back()
#define fs first
#define se second
#define sq(x) (x)*(x)
#define eps (1e-3)
#define IINF (1<<29)
#define LINF (1ll<<59)
#define INF 1000000000
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> P;

const int maxn=1005;
struct EDGE{
    int to;
    double d;
    EDGE(int to,double d):to(to),d(d){}
};
vector<EDGE> G[maxn];
int L,M;
double F[maxn];
int st;
int vis[maxn];
double dis[maxn];
bool dfs(int v,double p){
    vis[v]=st;
    for(int i=0;i<G[v].size();i++){
        EDGE &e=G[v][i];
        if(dis[e.to]>dis[v]-F[v]+e.d*p){
            dis[e.to]=dis[v]-F[v]+e.d*p;
            if(vis[e.to]==st) return 1;
            else if(dfs(e.to,p)) return 1;
        }
    }
    vis[v]=0;
    return 0;
}
bool check(double p){
    memset(vis,0,sizeof vis);
    memset(dis,0,sizeof dis);
    for(st=1;st<=L;st++){
        if(dfs(st,p)) return 1;
    }
    return 0;
}
int main(){
    ///freopen("/home/files/CppFiles/in","r",stdin);
    //freopen("defense.in","r",stdin);
    //freopen("defense.out","w",stdout);
    cin>>L>>M;
    for(int i=1;i<=L;i++)
        scanf("%lf",&F[i]);
    for(int i=0;i<M;i++){
        int a,b;
        double c;
        scanf("%d%d%lf",&a,&b,&c);
        G[a].pb(EDGE(b,c));
    }
    double l=0,r=1e9;
    while(r-l>eps){
        double mid=(r+l)/2;
        if(check(mid)){
            l=mid;
        }else{
            r=mid;
        }
    }
    printf("%.2f\n",l);
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/Cw-trip/p/4798465.html

根据原作 https://pan.quark.cn/s/459657bcfd45 的源码改编 Classic-ML-Methods-Algo 引言 建立这个项目,是为了梳理和总结传统机器学习(Machine Learning)方法(methods)或者算法(algo),和各位同仁相互学习交流. 现在的深度学习本质上来自于传统的神经网络模型,很大程度上是传统机器学习的延续,同时也在不少时候需要结合传统方法来实现. 任何机器学习方法基本的流程结构都是通用的;使用的评价方法也基本通用;使用的一些数学知识也是通用的. 本文在梳理传统机器学习方法算法的同时也会顺便补充这些流程,数学上的知识以供参考. 机器学习 机器学习是人工智能(Artificial Intelligence)的一个分支,也是实现人工智能最重要的手段.区别于传统的基于规则(rule-based)的算法,机器学习可以从数据中获取知识,从而实现规定的任务[Ian Goodfellow and Yoshua Bengio and Aaron Courville的Deep Learning].这些知识可以分为四种: 总结(summarization) 预测(prediction) 估计(estimation) 假想验证(hypothesis testing) 机器学习主要关心的是预测[Varian在Big Data : New Tricks for Econometrics],预测的可以是连续性的输出变量,分类,聚类或者物品之间的有趣关联. 机器学习分类 根据数据配置(setting,是否有标,可以是连续的也可以是离散的)和任务目标,我们可以将机器学习方法分为四种: 无监督(unsupervised) 训练数据没有给定...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值