POJ 3621 最优比率生成环
生活随笔
收集整理的這篇文章主要介紹了
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.000001using 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\n" ,ans);}return 0;
}
總結
以上是生活随笔為你收集整理的POJ 3621 最优比率生成环的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: POJ 2976 01分数规划基础题目
- 下一篇: POJ3757 01分数规划