poj3621 Sightseeing Cows

01分数规划

二分+spfa负环(SLF优化)

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

int n,m;
double p[1100];
struct edge{int x,y;double d;}e[5100];
struct node
{
    int x,y,next;double d;
}a[11000];int len,last[1100];
void ins(int x,int y,double d)
{
    len++;
    a[len].x=x;a[len].y=y;a[len].d=d;
    a[len].next=last[x];last[x]=len;
}

double d[1100];
int list[1100000],inq[1100];
bool v[1100];
bool check(double mid)
{
    len=0;memset(last,0,sizeof(last));
    for(int i=1;i<=m;i++)
        ins(e[i].x,e[i].y,mid*e[i].d-p[e[i].x]);

    memset(d,0,sizeof(0));
    memset(v,true,sizeof(v));
    memset(inq,0,sizeof(inq));
    int head=500000,tail=500000;
    for(int i=1;i<=n;i++)list[tail++]=i;
    while(head!=tail)
    {
        int x=list[head];head++;
        for(int k=last[x];k;k=a[k].next)
        {
            int y=a[k].y;
            if(d[y]>d[x]+a[k].d)
            {
                d[y]=d[x]+a[k].d;
                inq[y]++;if(inq[y]==n)return true;
                if(v[y]==false)
                {
                    v[y]=true;
                    if(d[y]>d[list[head]])list[tail++]=y;
                    else list[--head]=y;
                }
            }
        }
        v[x]=false;
    }
    return false;
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)scanf("%lf",&p[i]);
    for(int i=1;i<=m;i++)
        scanf("%d%d%lf",&e[i].x,&e[i].y,&e[i].d);

    double l=0,r=100000.0;
    while(r-l>1e-5)
    {
        double mid=(l+r)/2;
        if(check(mid))l=mid;
        else r=mid;
    }
    printf("%.2lf\n",l);
    return 0;
}

原文地址:https://www.cnblogs.com/AKCqhzdy/p/9540297.html

时间: 2024-10-14 14:35:01

poj3621 Sightseeing Cows的相关文章

poj3621 Sightseeing Cows --- 01分数规划

典型的求最优比例环问题 参考资料: http://blog.csdn.net/hhaile/article/details/8883652 此题中,给出每个点和每条边的权值,求一个环使 ans=∑点权/∑边权 最大. 因为题目要求一个环,而且必然是首尾相接的一个我们理解的纯粹的环,不可能是其他样子的环, 所以我们可以把一条边和指向的点看做整体处理. 上面方程可以化为:ans×e[i]-p[i]=0 以它为边权二分答案,spfa求负环,有负环则该ans可行,增大下界. 若一直不可行,则无解. #i

01分数规划+spfa判负环 POJ3621 Sightseeing Cows

Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10348   Accepted: 3539 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

POJ3621 Sightseeing Cows 最优比率环 二分法

题目链接:http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10552   Accepted: 3613 Description Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big ci

POJ3621 Sightseeing Cows【最短路】

题目大意:在一个无向图里找一个环,是的点权和除以边权和最大 思路:UVA11090姊妹题 事实上当这题点权和都为1时就是上一题TUT #include <stdio.h> #include <iostream> #include<queue> #include <string.h> #include <algorithm> #define maxn 10009 #define maxm 10001 #define esp 0.001 using

【POJ3621】Sightseeing Cows 分数规划

[POJ3621]Sightseeing Cows 题意:在给定的一个图上寻找一个环路,使得总欢乐值(经过的点权值之和)/ 总时间(经过的边权值之和)最大. 题解:显然是分数规划,二分答案ans,将每条边的权值变成(ans*边权-2*起始点点权),然后我们希望找出一个环,使得环上的总边权<0 (一开始我把题意理解错了,题中给的是单向边,我把它当成是双向边+每条边只能走一次了~,想出一堆做法都接连pass掉) 然后就直接用SPFA判负环就好了嘛!由于原图不一定联通,所以一开始就把所有点都入队就完事

【POJ3621】Sightseeing Cows

Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8331   Accepted: 2791 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

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

Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10306   Accepted: 3519 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

[USACO07DEC]观光奶牛Sightseeing Cows

[USACO07DEC]观光奶牛Sightseeing Cows 题目描述 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

POJ 题目3621 Sightseeing Cows(SPFA)

Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8220   Accepted: 2757 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