poj3621:Sightseeing Cows(0/1分数规划、SPFA判负环)

最优城市游览路线
本文介绍了一种使用0/1分数规划解决寻找最优环形路线的问题,目标是在给定的城市地图中找到平均乐趣值最高的游览路线。文章详细解释了算法原理,并提供了一个具体的实现代码示例。

Description

Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big city! The cows must decide how best to spend their free time.

Fortunately, they have a detailed city map showing the L (2 ≤ L ≤ 1000) major landmarks (conveniently numbered 1.. L) and the P (2 ≤ P ≤ 5000) unidirectional cow paths that join them. Farmer John will drive the cows to a starting landmark of their choice, from which they will walk along the cow paths to a series of other landmarks, ending back at their starting landmark where Farmer John will pick them up and take them back to the farm. Because space in the city is at a premium, the cow paths are very narrow and so travel along each cow path is only allowed in one fixed direction.

While the cows may spend as much time as they like in the city, they do tend to get bored easily. Visiting each new landmark is fun, but walking between them takes time. The cows know the exact fun values Fi (1 ≤ Fi ≤ 1000) for each landmark i.

The cows also know about the cowpaths. Cowpath i connects landmark L1i to L2i (in the direction L1i -> L2i ) and requires time Ti (1 ≤ Ti ≤ 1000) to traverse.

In order to have the best possible day off, the cows want to maximize the average fun value per unit time of their trip. Of course, the landmarks are only fun the first time they are visited; the cows may pass through the landmark more than once, but they do not perceive its fun value again. Furthermore, Farmer John is making the cows visit at least two landmarks, so that they get some exercise during their day off.

Help the cows find the maximum fun value per unit time that they can achieve.

题意

给n个点,m条有向边。每个点有对应点值a,每条边有对应权值b。
求一个环,使得 λ=ab 有最大值。并求出这个最大值。

题解

0/1分数规划。

详细解释请看这: poj2976:dropping tests(0/1分数规划)

显然的0/1分数规划方程。

枚举 λ ,并判断是否可行。
注意可行的条件必须是个环,这个环上的边(x->y)权值v=cost[x->y]- λ *val[x]。

因为要求最大值,所以应该找出一条正环,若存在,则猜测值偏小,否则猜测值偏大。判断环可以用SPFA,因为判断负环更方便,所以边权取相反数,即边(x->y)权值v= λ *val[x]-cost[x->y]。

spfa判负环

根据spfa 广搜时的性质可得,第一次更新的点上边数为1,第二次更新的点上边数为2,只有在边数为1的点处理完并弹出后才会处理边数为二的点。

因此,若没有负环,一个点最多入队n次,因为最短边数不可能超过n。

所以只需记录每个点的入队次数并判断即可。

Code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;

const int Maxn=1e3+50,Maxm=5e3+50;
const double eps=1e-4;
const int INF=0x3f3f3f3f;

inline int read()
{
    char ch=getchar();int i=0,f=1;
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){i=(i<<3)+(i<<1)+ch-'0';ch=getchar();}
    return i*f;
}

int n,m;
double l,r;
double val[Maxn],dis[Maxn];
int cnt[Maxn],que[Maxn*Maxn],head,tail;
vector< pair<int,double> >edge[Maxn];
bool exi[Maxn];

inline bool check(double k)
{
    fill(dis+1,dis+Maxn,INF);
    memset(cnt,0,sizeof(cnt));
    memset(exi,0,sizeof(exi));
    head=0,tail=1;
    que[tail]=1;
    cnt[1]=1;
    exi[1]=1;
    dis[1]=0;
    while(head<tail)
    {
        int now=que[++head];
        for(int e=edge[now].size()-1;e>=0;e--)
        {
            int v=edge[now][e].first;
            double w=k*edge[now][e].second-val[now];
            if(dis[now]+w<dis[v])
            {
                dis[v]=dis[now]+w;
                if(!exi[v])
                {
                    exi[v]=1;
                    que[++tail]=v;
                    cnt[v]++;
                    if(cnt[v]>=n)return 1;
                }
            }
        }
        exi[now]=0;
    }
    return 0;
}

int main()
{
    n=read(),m=read();
    for(int i=1;i<=n;i++)
    {
        val[i]=read();
        r+=val[i];
    }
    for(int i=1;i<=m;i++)
    {
        int x=read(),y=read();
        double z=read();
        edge[x].push_back(make_pair(y,z));
    }
    while(r-l>eps)
    {
        double mid=(r+l)/2.0;
        if(check(mid))l=mid;
        else r=mid;
    }
    printf("%.2f",r);
}
1)普通用户端(全平台) 音乐播放核心体验: 个性化首页:基于 “听歌历史 + 收藏偏好” 展示 “推荐歌单(每日 30 首)、新歌速递、相似曲风推荐”,支持按 “场景(通勤 / 学习 / 运动)” 切换推荐维度。 播放页功能:支持 “无损音质切换、倍速播放(0.5x-2.0x)、定时关闭、歌词逐句滚动”,提供 “沉浸式全屏模式”(隐藏冗余控件,突出歌词与专辑封面)。 多端同步:自动同步 “播放进度、收藏列表、歌单” 至所有登录设备(如手机暂停后,电脑端打开可继续播放)。 音乐发现与管理: 智能搜索:支持 “歌曲名 / 歌手 / 歌词片段” 搜索,提供 “模糊匹配(如输入‘晴天’联想‘周杰伦 - 晴天’)、热门搜索词推荐”,结果按 “热度 / 匹配度” 排序。 歌单管理:创建 “公开 / 私有 / 加密” 歌单,支持 “批量添加歌曲、拖拽排序、一键分享到社交平台”,系统自动生成 “歌单封面(基于歌曲风格配色)”。 音乐分类浏览:按 “曲风(流行 / 摇滚 / 古典)、语言(国语 / 英语 / 日语)、年代(80 后经典 / 2023 新歌)” 分层浏览,每个分类页展示 “TOP50 榜单”。 社交互动功能: 动态广场:查看 “关注的用户 / 音乐人发布的动态(如‘分享新歌感受’)、好友正在听的歌曲”,支持 “点赞 / 评论 / 转发”,可直接点击动态中的歌曲播放。 听歌排行:个人页展示 “本周听歌 TOP10、累计听歌时长”,平台定期生成 “全球 / 好友榜”(如 “好友中你本周听歌时长排名第 3”)。 音乐圈:加入 “特定曲风圈子(如‘古典音乐爱好者’)”,参与 “话题讨论(如‘你心中最经典的钢琴曲’)、线上歌单共创”。 (2)音乐人端(创作者中心) 作品管理: 音乐上传:支持 “无损音频(FLAC/WAV)+ 歌词文件(LRC)+ 专辑封面” 上传,填写 “歌曲信息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值