Test instructions
There is a happy value for each point in the graph, and the edge has the edge right, which requires a loop to make the loop happy and/or the path and maximum.
Analysis:
Two-parameter, judge whether there is negative right, here SPFA negative circle useless, use is a high efficiency method.
Code:
POJ 3621//sep9#include <iostream> #include <cmath>using namespace std;const int Maxn=1024;const int maxm= 5012;int n,m,e;struct edge{int v,nxt;double c;} Edge[maxm];int head[maxn],vis[maxn],cur;double val[maxn],d[maxm],l,r,mid;bool dfs (int u) {vis[u]=cur;for (int i=head[ U];I!=-1;I=EDGE[I].NXT) {int v=edge[i].v;if (D[v]>d[u]+mid*edge[i].c-val[u]) {d[v]=d[u]+mid*edge[i].c-val[u];if (vis[v]==cur) return True;else if (Dfs (v)) return true;}} Vis[u]=0;return false;} BOOL Neg_circle () {for (int i=1;i<=n;++i) d[i]=0.0;memset (vis,0,sizeof (VIS)), for (cur=1;cur<=n;++cur) if (DFS ( cur)) return True;return false;} int main () {while (scanf ("%d%d", &n,&m) ==2) {E=0;memset (head,-1,sizeof (head)), and for (int i=1;i<=n;++i) scanf ( "%lf", &val[i]); for (int i=1;i<=m;++i) {int a,b;double c;scanf ("%d%d%lf", &a,&b,&c); edge[e].v=b; edge[e].c=c;edge[e].nxt=head[a];head[a]=e++;} L=0.0,r=1000.0;while (Fabs (r-l) >1e-4) {mid= (l+r)/2;if (Neg_circle ()) L=mid;elser=mid;} printf ("%.2lf\n", mid);} return 0;}
POJ 3621 Sightseeing cows negative ring detection parametric search