最大密度环 01分数规划
首先的一个结论就是,不会存在环套环的问题,即最优的方案一定是一个单独的环,而不是大环套着小环的形式。这个的证明其实非常的简单,大家可以自己想一下(提示,将大环上的收益和记为x1,花费为y1,小环上的为x2,y2。重叠部分的花费为S。表示出来分类讨论即可)。有了这个结论,我们就可以将花费和收益都转移到边上来了,因为答案最终一定是一个环,所以我们将每一条边的收益规定为其终点的收益,这样一个环上所有的花费和收益都能够被正确的统计。
解决了蛋疼的问题之后,就是01分数规划的部分了,我们只需要计算出D数组后找找有没有正权环即可,不过这样不太好,不是我们熟悉的问题,将D数组全部取反之后,问题转换为查找有没有负权环,用spfa或是bellman_ford都可以。这道题目就是典型的不适合用Dinkelbach,记录一个负权环还是比较麻烦的,所以二分搞定。
Sightseeing Cows
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 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 ≤ The cows also know about the cowpaths. Cowpath i connects landmark L1i to L2i (in the direction L1i -> L2i ) and requires time Ti (1 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 Help the cows find the maximum fun value per unit time that they can achieve. Input * Line 1: Two space-separated integers: L and P * Lines 2..L+1: Line i+1 contains a single one integer: Fi * Lines L+2..L+P+1: Line L+i+1 describes cow path i with three space-separated integers: L1i , L2i , and Ti Output * Line 1: A single number given to two decimal places (do not perform explicit rounding), the maximum possible average fun per unit time, or 0 if the cows cannot plan any trip at all in accordance with the above rules. Sample Input 5 7 30 10 10 5 10 1 2 3 2 3 2 3 4 5 3 5 2 4 5 5 5 1 3 5 2 2 Sample Output 6.00 Source |
[Submit] [Go Back] [Status]
[Discuss]
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <cmath> using namespace std; const int maxn=5500; const double eps=1e-7; int n,m; double fun[maxn/5]; struct Edge { int to,next; double w; }edge[maxn*2]; int Adj[maxn/5],Size; void init() { memset(Adj,-1,sizeof(Adj));Size=0; } void add_edge(int u,int v,double w) { edge[Size].to=v; edge[Size].w=w; edge[Size].next=Adj[u]; Adj[u]=Size++; } double dist[maxn/5]; int cq[maxn/5]; bool inq[maxn/5]; bool spfa(double r) { for(int i=0;i<=n+1;i++) dist[i]=1e30; memset(cq,0,sizeof(cq)); memset(inq,false,sizeof(inq)); dist[1]=0; queue<int> q; inq[1]=true; q.push(1); cq[1]=1; while(!q.empty()) { int u=q.front(); q.pop(); inq[u]=false; for(int i=Adj[u];~i;i=edge[i].next) { int v=edge[i].to; double W=edge[i].w*r-fun[v]; if(dist[v]>dist[u]+W) { dist[v]=dist[u]+W; if(!inq[v]) { inq[v]=true; cq[v]++; if(cq[v]>=n) return true; q.push(v); } } } } return false; } int main() { while(scanf("%d%d",&n,&m)!=EOF) { init(); for(int i=1;i<=n;i++) scanf("%lf",fun+i); for(int i=0;i<m;i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); add_edge(a,b,c); } double low=0.,high=1000.,mid,ans; while(fabs(low-high)>eps) { mid=(low+high)/2.; if(spfa(mid)) { ans=low=mid; } else high=mid; } printf("%.2lf\n",ans); } return 0; }