1. 程式人生 > >bzoj4773: 負環

bzoj4773: 負環

clas freopen urn fine pla else memcpy eof div

題解:

為什麽這道題有跑100ms的代碼。。。

首先這題可以用倍增floyd

比較慢的就是二分+倍增floyd是n^3log^2n的

可以直接用找lca的思想,做到n^3logn

不太懂floyd的理論

兩個矩陣算起來的時候要用新矩陣去更新的

c[i][j]=min(c[i][j],a[i][k]+b[k][j])這樣做

然後卡了一下常(指針)

不過在bz上效果好像不是很明顯

自己測試還是很明顯得

不過windos下數組極慢,要用5.5s

linux下只用了2s,指針的win和linux下幾乎相同都是1s

代碼:

#include <bits/stdc++.h>
using
namespace std; #define rint register int #define IL inline #define rep(i,h,t) for (rint i=h;i<=t;i++) #define dep(i,t,h) for (rint i=t;i>=h;i--) const int INF=1e9; const int N=400; char ss[1<<24],*A=ss,*B=ss; IL char gc() { return (A==B&&(B=(A=ss)+fread(ss,1,1<<24
,stdin),A==B)?EOF:*A++); } template<class T>void read(T &x) { rint f=1,c; while (c=gc(),c<48||c>57) if (c==-) f=-1; x=c^48; while (c=gc(),47<c&&c<58) x=(x<<3)+(x<<1)+(c^48); x*=f; } int f[13][N][N],t[N][N],t1[N][N],n,m; int main() { freopen(
"1.in","r",stdin); freopen("1.out","w",stdout); read(n); read(m); rep(k,0,8) rep(i,1,n) rep(j,1,n) if (i!=j) f[k][i][j]=INF; rep(i,1,m) { int x,y,z; read(x); read(y); read(z); if (f[0][x][y]>z) f[0][x][y]=z; } rep(i,1,8) { rep(i1,1,n) rep(i2,1,n) { rint tmp=f[i-1][i2][i1]; rint *p1=f[i-1][i1]; rep(i3,1,n) { rint *p3=f[i][i2]; rint p4=p3[i3]; rint p2=tmp+p1[i3]; if (p4>p2) p3[i3]=p2; } } } rep(i,1,n) rep(j,1,n) t[i][j]=f[0][i][j]; int ans=1; dep(i,8,0) { rep(i1,1,n) rep(i2,1,n) t1[i1][i2]=INF; rep(i1,1,n) { rint (*g)=f[i][i1]; rep(i2,1,n) { rint tmp=t[i2][i1]; rint *p3=t1[i2]; rep(i3,1,n) { rint p1=p3[i3]; rint p2=tmp+g[i3]; if (p1>p2) p3[i3]=p2; } } } bool tt=0; rep(j,1,n) if (t1[j][j]<0) tt=1; if (!tt) { ans+=1<<i; memcpy(t,t1,sizeof(t1)); } } if (ans+1==513) cout<<0<<endl; else cout<<ans+1<<endl; return 0; }

bzoj4773: 負環