POJ 3621 最优比率生成环

题意:
     让你求出一个最优比率生成环。
思路:
     又是一个01分化基础题目,直接在jude的时候找出一个sigma(d[i] * x[i])大于等于0的环就行了,我是用SPFA跑最长路判断的环,这里注意一点就是刚开始的时候吧每个点都入队,还有提醒一个盲区,就是有的人认为SPFA处理不了等于0的环,其实我们不用担心这个问题,因为最外层我们用的是二分,二分永远是找接近值,就算有等于0的时候,spfa虽然找不到0.0000000 但是他可以找到0.0000000123 保留两位不还是0.00吗。

自己总结的01分数规划:
http://blog.csdn.net/u013761036/article/details/26666261

#include<stdio.h>
#include<string.h>
#include<queue>

#define N_node 1000 + 10
#define N_edge 5000 + 50
#define INF 1000000000
#define eps 0.000001

using namespace std;

typedef struct
{
   int to ,next;
   double cost;
}STAR;

STAR E[N_edge];
int list[N_node] ,tot;
double s_x[N_node];
double hap[N_node];


void add(int a ,int b ,double c)
{
   E[++tot].to = b;
   E[tot].cost = c;
   E[tot].next = list[a];
   list[a] = tot;
}

bool SPFA(int n ,double L)
{
   int mark[N_node];
   int in[N_node];
   queue<int>q;
   for(int i = 1 ;i <= n ;i ++)
   {
      mark[i] = in[i] = 1;
      s_x[i] = 0;
      q.push(i);
   }
   while(!q.empty())
   {
      int xin ,tou;
      tou = q.front();
      q.pop();
      mark[tou] = 0;
      for(int k = list[tou] ;k ;k = E[k].next)
      {
         xin = E[k].to;
         double cost = hap[tou] - L * E[k].cost;
         if(s_x[xin] < s_x[tou] + cost)
         {
            s_x[xin] = s_x[tou] + cost;
            if(++in[xin] > n) return 1;
            if(!mark[xin])
            {
               mark[xin] = 1;
               q.push(xin);
            }
         }
      }
   }
   return 0;
}


int main ()
{
   int n ,m ,i;
   int a ,b;
   double c;
   while(~scanf("%d %d" ,&n ,&m))
   {
      for(i = 1 ;i <= n ;i ++)
      scanf("%lf" ,&hap[i]);
      memset(list ,0 ,sizeof(list));
      tot = 1;
      for(i = 1 ;i <= m ;i ++)
      {
         scanf("%d %d %lf" ,&a ,&b ,&c);
         add(a ,b ,c);
      }
      double low ,mid ,up ,ans;
      ans = low = 0;
      up = INF;
      while(up - low >= eps)
      {
         mid = (low + up) / 2;
         if(SPFA(n ,mid))
         ans = low = mid;
         else up = mid;
      }
      printf("%.2f
" ,ans);
   }
   return 0;
}
      
      
      
      
   
   

原文地址:https://www.cnblogs.com/csnd/p/12063091.html