And something's wrong. Special topic of graph theory (iii): Optimization of SPFA algorithm

Source: Internet
Author: User
Tags cmath

1.bzoj1489->

It's a new routine.

We want to find the smallest x, then we can divide x and then determine if the average of the Benquan is less than or equal to X.

The Benquan of the ring are sequentially w1,w2,w3,...,wk, the average is P,

Then there are p= (W1+W2+W3+...+WK)/k,

Can be launched P*K=W1+W2+W3+...+WK,

This will have (w1-p) + (w2-p) +...+ (wk-p) = 0,

When P≤x, there will be (w1-x) + (w2-x) +...+ (wk-x) ≤0.

In this way, the Benquan of all sides can be changed to W-x, and then by judging the negative ring, the average value of the Benquan of the circle is less than or equal to X.

Code, I don't know why the constant is great:

 #include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring > #include <iomanip> #include <iostream> #include <map> #include <queue> #include <stack > #include <vector> #define MAXN 3010#define maxm 10010#define eps 1e-10using namespace std;inline int Read () {in    T x=0,f=1;    Char Ch=getchar ();    while (IsDigit (CH) ==0 && ch!= '-') Ch=getchar ();    if (ch== '-') F=-1,ch=getchar ();    while (IsDigit (CH)) x= (x<<3) + (x<<1) +ch-' 0 ', Ch=getchar (); return x*f;}    inline void Write (int x) {int F=0;char ch[20];    if (!x) {puts ("0"); return;}    if (x<0) {Putchar ('-'); x=-x;}    while (x) ch[++f]=x%10+ ' 0 ', x/=10;    while (f) putchar (ch[f--]); Putchar (' \ n ');} int fir[maxn],nxt[maxm],v[maxm],cnt,n,m,inf[5],vis[maxn],yes;double mid,ans,l,r,dis[maxn],w[maxm];void ade (int u1, int v1,double W1) {v[cnt]=v1,w[cnt]=w1,nxt[cnt]=fir[u1],fir[u1]=cnt++;}    void Dfs (int u) {if (yes) return;  for (int k=fir[u];k!=-1;k=nxt[k])  {if (dis[v[k]]>dis[u]+ (W[k]-mid)) {dis[v[k]]=dis[u]+ (w[k]-mid);            if (Vis[v[k]]) {Yes=1;return;}        Vis[v[k]]=1,dfs (V[k]), vis[v[k]]=0; } else if (dis[v[k]]==dis[u]+ (W[k]-mid)) {if (Vis[v[k]) {Yes=1;return;}    Vis[v[k]]=1,dfs (V[k]), vis[v[k]]=0;}    }}int Check () {yes=0;        for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) dis[j]=0.0,vis[j]=0;        DFS (i);    if (yes) break; } return yes;    int main () {memset (inf,31,sizeof (INF));    memset (fir,-1,sizeof (FIR)); N=read (), M=read ();    l=10000000.0,r=-10000000.0; for (int i=1;i<=m;i++) {int x=read (), Y=read ();d ouble z;scanf ("%lf", &z); R=max (Z,r), L=min (z,l); Ade (x, y, z);}    Ans=r;        while (Fabs (r-l) >eps) {mid= (l+r)/2.0;int F=check ();        cout<<l<< "" <<mid<< "" <<R<<endl;        if (f) ans=ans<mid?ans:mid,r=mid-eps;    else l=mid+eps;     } printf ("%.8lf", ans); return 0;}

2.bzoj1715->

The negative ring is OK.

A tip: Start by setting the distance of all points to 0, not the positive infinity, so that the negative numbers are updated.

Another tip: changing BFS to DFS may be better when it comes to negative loops, but it's possible to get stuck, like the previous question.

#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring > #include <iomanip> #include <iostream> #include <map> #include <queue> #include <stack    > #include <vector> #define MAXN 505#define maxm 6010using namespace Std;inline int read () {int x=0,f=1;    Char Ch=getchar ();    while (IsDigit (CH) ==0 && ch!= '-') Ch=getchar ();    if (ch== '-') F=-1,ch=getchar ();    while (IsDigit (CH)) x= (x<<3) + (x<<1) +ch-' 0 ', Ch=getchar (); return x*f;}    inline void Write (int x) {int F=0;char ch[20];    if (!x) {puts ("0"); return;}    if (x<0) {Putchar ('-'); x=-x;}    while (x) ch[++f]=x%10+ ' 0 ', x/=10;    while (f) putchar (ch[f--]); Putchar (' \ n ');} int t,n,m,ww,fir[maxn],dis[maxn],nxt[maxm],v[maxm],w[maxm],vis[maxn],cnt,yes;void ade (int u1,int v1,int W1) {v[cnt]= v1,w[cnt]=w1,nxt[cnt]=fir[u1],fir[u1]=cnt++;} void Reset () {memset (fir,-1,sizeof (FIR)), memset (vis,0,sizeof (Vis)); cnt=yes=0;}    void Dfs (int u) {if (yes) return; For (int k=fir[u];k!=-1;k=nxt[k]) {if (Dis[v[k]]>dis[u]+w[k]) {//cout<<dis[v[k]]<<            "<<dis[u]<<" "<<u<<" "<<v[k]<<endl;            DIS[V[K]]=DIS[U]+W[K];            if (Vis[v[k]]) {yes=1;/*cout<<v[k]<<endl;*/break;}            Vis[v[k]]=1;            DFS (V[k]);        vis[v[k]]=0;    }}}int Main () {t=read ();        while (t--) {reset ();        N=read (), M=read (), Ww=read ();        for (int i=1;i<=m;i++) {int x=read (), Y=read (), Z=read (), Ade (x, Y, z), Ade (Y,X,Z);}        for (int i=1;i<=ww;i++) {int x=read (), Y=read (), Z=read (); Ade (x,y,-z);}        for (int i=1;i<=n&&!yes;i++) {memset (dis,0,sizeof (dis)); Vis[i]=1;dfs (i); vis[i]=0;} Puts (yes? ")    YES ":" NO "); } return 0;}

3.vijos1053->

Board problem, the code is not affixed.

Next SLF optimization: If there is a point where the distance is updated, less than the current line of the queue, then put it to the head of the team.

It is based on the fact that there is no negative right side of the case, the first element of the team is larger than it, the updated point will not be better than it, that is, like Dijkstra greedy.

However, this optimization becomes metaphysical when there is a negative right side. For example, when x is in the queue, the first y of the team is greater than X, and x is placed on the team head. But there is a negative right side of the y->x, in the calculation of Y and updated the distance x, let x extra team.

Today's hyperlink map is a silver dragon.

And something's wrong. Special topic of graph theory (iii): Optimization of SPFA algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.