POJ 3621 Sightseeing Cows (bellman-Ford + 01分数规划)

Posted dwtfukgv

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 3621 Sightseeing Cows (bellman-Ford + 01分数规划)相关的知识,希望对你有一定的参考价值。

题意:给出 n 个点 m 条有向边,要求选出一个环,使得这上面 点权和/边权和 最大。

析:同样转成是01分数规划的形式,F / L 要这个值最大,也就是 G(r) = F - L * r 这个值为0时,r 的值,然后对于 F > 0,很明显是 r 太小,但是不好判断,把这个值取反,这样的话就能用Bellan-Ford 来判是不是有负环了,也可以用spfa。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n)  for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LL, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-3;
const int maxn = 1e3 + 10;
const int maxm = 1e6 + 5;
const int mod = 1000000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
  return r >= 0 && r < n && c >= 0 && c < m;
}

struct Edge{
  int to, val, next;
};
Edge edges[maxn*5];
int head[maxn], cnt;

void addEdge(int u, int v, int c){
  edges[cnt].to = v;
  edges[cnt].val = c;
  edges[cnt].next = head[u];
  head[u] = cnt++;
}
int val[maxn];
bool inq[maxn];
int num[maxn];
double d[maxn];

bool judge(double m){
  queue<int> q;  q.push(1);
  ms(inq, 0);  ms(num, 0);
  for(int i = 0; i <= n; ++i)  d[i] = inf;
  d[1] = 0;  inq[1] = true;

  while(!q.empty()){
    int u = q.front();  q.pop();
    inq[u] = 0;
    for(int i = head[u]; ~i; i = edges[i].next){
      int v = edges[i].to;
      if(d[v] > -val[v] + edges[i].val * m + d[u]){
        d[v] = -val[v] + edges[i].val * m + d[u];
        if(!inq[v]){ inq[v] = 1;  q.push(v);  if(++num[v] > n)  return true; }
      }
    }
  }
  return false;
}

int main(){
  while(scanf("%d %d", &n, &m) == 2){
    for(int i = 1; i <= n; ++i)  scanf("%d", val + i);
    ms(head, -1);  cnt = 0;
    while(m--){
      int u, v, c;
      scanf("%d %d %d", &u, &v, &c);
      addEdge(u, v, c);
    }
    double l = 0.0, r = 1e3;
    while(r - l > eps){
      double m = (l + r) / 2.0;
      judge(m) ? l = m : r = m;
    }
    printf("%.2f\n", l);
  }
  return 0;
}

  

以上是关于POJ 3621 Sightseeing Cows (bellman-Ford + 01分数规划)的主要内容,如果未能解决你的问题,请参考以下文章

POJ3621Sightseeing Cows

POJ 3621 Sightseeing Cows | 01分数规划

poj3621 Sightseeing Cows

POJ 3621 Sightseeing Cows 01分数规划+spfa判正环

POJ3621 Sightseeing Cows 最优比率环 二分法

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