1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <bitset> #define inf 0x3FFFFFFF using namespace std; struct Node { int to,next,v; }e[17005],mp[17005]; int s,t,n,m,h[855],f[855][855],d[855],gap[855],fa[855],cnt=1; bool vis[855]; inline void Addedge(int x,int y,int v) { mp[++cnt]=(Node){y,h[x],v};h[x]=cnt;return; } inline int dfs(int x,int maxf) { int i,y,ret=0,delta; if(x==t)return maxf; for(i=h[x];i;i=e[i].next) { y=e[i].to; if(e[i].v&&d[x]==d[y]+1) { delta=dfs(y,min(maxf,e[i].v)); e[i].v-=delta; e[i^1].v+=delta; ret+=delta; maxf-=delta; if(!maxf||d[x]==n)return ret; } } if(!(--gap[d[x]]))d[s]=n; ++gap[++d[x]]; return ret; } inline void dfs(int x) { int i,y; vis[x]=true; for(i=h[x];i;i=e[i].next) { y=e[i].to; if(e[i].v&&!vis[y])dfs(y); } return; } inline void Gusfield() { int i,j,ans; memset(f,0x3F,sizeof(f)); for(i=2;i<=n;++i)fa[i]=1; for(i=2;i<=n;++i) { memcpy(e,mp,sizeof(mp)); memset(gap,0,sizeof(gap)); memset(d,0,sizeof(d)); memset(vis,0,sizeof(vis)); s=fa[i];t=i;ans=0; gap[0]=n; while(d[s]<n)ans+=dfs(s,inf); dfs(s); for(j=i+1;j<=n;++j) { if(!vis[j]&&fa[j]==fa[i])fa[j]=i; } for(j=1;j<i;++j)f[i][j]=f[j][i]=min(f[fa[i]][j],ans); } return; } bitset<23333333>H; int main(void) { int i,j,x,y,v,ans=0; scanf("%d%d",&n,&m); for(i=1;i<=m;++i) { scanf("%d%d%d",&x,&y,&v); Addedge(x,y,v); Addedge(y,x,v); } Gusfield(); for(i=1;i<=n;++i) { for(j=1;j<=n;++j) { if(i==j)continue; if(!H[f[i][j]]){++ans;H[f[i][j]]=true;} } } printf("%d\n",ans); return 0; }
|