負のリングを見つけるSPFA ----------観光牛

LポイントとPエッジを持つ有向グラフが与えられた場合、各ポイントには重みf [i]があり、各エッジには重みt [i]があります。
「リング上の点の重みの合計」を「リングの側面の重みの合計」で割った値が最大になるように、グラフでリングを見つけます。
この最大値が出力されます。
注:データは、少なくとも1つのリングがあることを保証します。
入力フォーマット
最初の行には2つの整数LとPが含まれています。
次のL行の各整数はf [i]を表します。
次に、ラインPでは、各ラインの3つの整数a、b、およびt [i]は、ポイントaとbの間にエッジがあり、エッジの重みがt [i]であることを示します。
出力形式
結果を示す数値を出力し、小数点第2位を保持します。
データ範囲
2≤L≤10002≤L≤1000、

2≤P≤50002≤P≤5000、

1≤f[i]、t [i]≤10001≤f[i]、t [i]≤1000
入力サンプル:
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

出力例:
6.00

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1010, M = 5010;
int n, m;
int wf[N];
int h[N], e[M], wl[M], ne[M], idx;
double dist[N];
int q[N], cnt[N];
bool st[N];
void add(int a, int b, int c){
 e[idx] = b, wl[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}
bool check(double mid){
 memset(dist, 0, sizeof dist);
 memset(cnt, 0, sizeof cnt);
 memset(st, 0, sizeof st);
  int hh = 0, tt = 0;
 for (int i = 1; i <= n; i ++){
  q[tt ++] = i;
  st[i] = true;
 }
  while(hh != tt){
  int t = q[hh ++];
  if (hh == N)   hh = 0;
  st[t] = false;
   for (int i = h[t]; ~i; i = ne[i]){
   int j = e[i];
   if (dist[j] < dist[t] + wf[t] - mid * wl[i]){
       dist[j] = dist[t] + wf[t] - mid * wl[i];
   cnt[j] = cnt[t] + 1;
   if (cnt[j] >= n)   return true;
   if (!st[j]){
    q[tt ++] = j;
    if (tt == N)   tt = 0;
    st[j] = true;
   }
  }
 }
 }
 return false;
}
int main(){
 cin >> n >> m;
 for (int i = 1; i <= n; i ++)   scanf("%d", &wf[i]);
  memset(h, -1, sizeof h);
 for (int j = 0; j < m; j ++){
  int a, b, c;
  cin >> a >> b >> c;
  add(a, b, c);
 }
  double l = 0, r = 1010;
 while(r - l > 1e-4){
  double mid = (l + r) / 2;
  if (check(mid))   l = mid;
  else              r = mid;
 }
  printf("%.2lf\n", l);
  return 0;
}

164の元の記事が公開されました いいね112 訪問6770

おすすめ

転載: blog.csdn.net/qq_45772483/article/details/105471828