POJ 3621 Sightseeing Cows(最优比率环/01分数规划)
2017-07-06 13:29
375 查看
思路:
01分数规划的第三题。(前两题分别是poj 2976 2728)这道题能明显感觉到不适用于迭代法(Dinkelbach)
一开始写的时候本打算用的,然后写的过程中发现记录哪些点组成的环是一件很复杂的事情。而且,spfa的思想也是检测负环 && 最短路。
(其实要想记录的来一个pre【】数组记录修改此节点的前驱节点也可以,只是麻烦。。毕竟spfa已经很长了。。)
注意这里有一个小点:有关构不成环的情况,我们没有必要在spfa中判断,只需要不断的更新区间就好,最后发现ans == -1,于是判断不联通。(在spfa判断有没有环花费太大了。。)
对于01规划的题,我都是习惯于构造出大于等于0的函数,比如这题:rate(r)=∑r∗time−∑value≥0
#include <iostream> #include <cstdio> #include <algorithm> #include <math.h> #include <queue> #include <string.h> typedef long long int lli; using namespace std; struct edge{ int to,v,next; double newv;//构造的新的权值 }edg[5050]; int head[1010]; int cnte; void addedge(int x,int y,int v){ edg[++cnte].to = y; edg[cnte].v = v; edg[cnte].next = head[x]; head[x] = cnte; } int vis[1010];// int a[1010];//存value double dis[1010]; int cntn[1010]; int l,p; bool rate(double r){//spfa 只判断有无负权环 memset(vis,0,sizeof(vis)); memset(cntn,0,sizeof(cntn)); memset(dis,0,sizeof(dis)); for(int i = 1;i <= p;i++){ edg[i].newv = r*edg[i].v-a[edg[i].to]; } queue<int> q; for(int i = 1;i <= l;i++){//亮点!!这个操作避免了多次spfa q.push(i); } int po; while(!q.empty()){ po = q.front(); q.pop(); vis[po] = false; for(int i = head[po];i != -1;i = edg[i].next){ if(dis[edg[i].to] > dis[po] + edg[i].newv){ dis[edg[i].to] = dis[po] + edg[i].newv; if(vis[edg[i].to] == false){ q.push(edg[i].to); vis[edg[i].to] = true; cntn[edg[i].to]++; if(cntn[edg[i].to] >= l){ return true;//负权环 } } } } } return false; } int main(){ scanf("%d%d",&l,&p); for(int i = 1;i <= l;i++){ scanf("%d",a+i); } int x,y,z; memset(head,-1,sizeof(head)); for(int i = 1;i <= p;i++){ scanf("%d%d%d",&x,&y,&z); addedge(x,y,z); } double r = 10000000,l = 0,mid = 0; double ans = -1; bool fla; while(r-l > 1e-6){ mid = (l+r)/2; fla = rate(mid); if(fla == true){ l = mid; ans = mid; } else{ r = mid; } } if(fla != -1){ printf("%.2f\n",mid); } else{ printf("0\n"); } }
相关文章推荐
- poj 3621 Sightseeing Cows (最优比率生成环 01分数规划问题 )
- POJ-3621 Sightseeing Cows 01分数(参数搜索)规划问题-最优比率环
- POJ-3621 Sightseeing Cows 最优比率环、01分数规划
- poj 3621 Sightseeing Cows ( 求最优比例生成环/01分数规划 )
- poj 3621 Sightseeing Cows(最优比例生成环,01分数规划)
- POJ 3621 Sightseeing Cows 01分数规划,最优比例环的问题
- poj 3621 Sightseeing Cows 【最优比例环】 【0-1分数规划 + SPFA判负环】
- POJ 3621 Sightseeing Cows 最优比率环
- POJ-3621: Sightseeing Cows【最优比率生成环】
- 【分数规划】【最优比率环 & 最优比率生成树】poj3621 Sightseeing Cows && poj2728 Desert King
- poj-3621-Sightseeing Cows-01分数规划+spfa判负环
- POJ 3621-Sightseeing Cows-最优比率环|SPFA+二分
- 01分数规划 ——最有比率环 pku 3621 Sightseeing Cows
- POJ 3621 Sightseeing Cows(01分数规划+二分+spfa判负环)
- POJ 3621 Sightseeing Cows(01分数规划)
- POJ 3621(0/1分数规划,二分) Sightseeing Cows
- POJ 3621 Sightseeing Cows 01分数规划(参数搜索)+最短路
- poj 3621 Sightseeing Cows(最优比率环)
- POJ 3621 Sightseeing Cows 最大密度环 01分数规划
- poj 3621 Sightseeing Cows (最优比率环)