Solving the puzzle: divide and conquer the minimum cut.
"L......R" in any of the two as S, T (may wish to set S as the point on the L position, T set to the point on the R position) for the minimum cut, two for the Loop enumeration modified MAP[I][J] that is the minimum cut value between two points.
Then part of the S set, part of the T set, divide and conquer "l,l", "R,r", each time the minimum cut value is the global modification.
Finally, every time you ask for violence, you don't need any optimization to be able to AC.
Code:
#include <queue> #include <cstdio> #include <cstring> #include <iostream> #include < algorithm> #define N 200#define M 7000#define inf 0x3f3f3f3f#define ci crs[i] #define CJ crs[j]using namespace Std;struct Ksd{int V,len,next,flow;} E[m];int head[n],cnt;inline void Add (int u,int v,int len) {cnt++;e[cnt].v=v;e[cnt].flow=e[cnt].len=len;e[cnt].next= head[u];head[u]=cnt;} inline void Resume () {for (int i=2;i<=cnt;i++) E[i].len=e[i].flow;} int Crs[n],tcrs[n];bool flag[n];int map[n][n];int d[n],s,t;int n,m;queue<int>q;bool bfs () {int I,u,v;memset (d,0, sizeof (d)), while (!q.empty ()) Q.pop (), Q.push (s), D[s]=1;while (!q.empty ()) {U=q.front (), Q.pop (); for (i=head[u];i;i=e [I].next) {v=e[i].v;if (E[i].len&&!d[v]) {d[v]=d[u]+1;if (v==t) return 1;q.push (v);}} return 0;} int dinic (int x,int flow) {if (x==t) return flow;int remain=flow,i,v,k;for (i=head[x];i&&remain;i=e[i].next) {if (D[v=e[i].v]==d[x]+1&&e[i].len) {K=dinic (V,min (Remain,e[i].len)); if (!k) d[v]=0;e[i].len-=k,e[i^1].len+=k;remain-=k;}} return flow-remain;} void Dfs (int x) {int i,v;flag[x]=1;for (i=head[x];i;i=e[i].next) if (!flag[v=e[i].v]&&e[i].len) Dfs (v);} void Dar (int l,int R)//divide and rule divide {int i,j,maxflow=0;s=crs[l],t=crs[r];resume (); while (BFS ())) Maxflow+=dinic (S, INF), memset (flag,0,sizeof (flag)), DFS (s), for (i=1;i<=n;i++) for (j=1;j<=n;j++) if (FLAG[CI]^FLAG[CJ]) map[ci][ Cj]=min (map[ci][cj],maxflow); int l=l-1,r=r+1;for (i=l;i<=r;i++) (Flag[ci]?tcrs[++l]:tcrs[--r]) =ci;for (i=l;i <=r;i++) crs[i]=tcrs[i];if (l<l) Dar (l,l), if (R<r) Dar (r,r);} int main () {int I,j,k,g;int a,b,c;for (scanf ("%d", &g), g--;) {cnt=1;memset (head,0,sizeof (head)); Memset (map,0x3f, sizeof (map)), for (scanf ("%d%d", &n,&m), cnt=1;m--;) {scanf ("%d%d%d", &a,&b,&c), add (A,b,c), add (b , a,c);} for (i=1;i<=n;i++) Crs[i]=i;dar (1,n), for (scanf ("%d", &m), m--;) {scanf ("%d", &k); int Ans=0;for (i=1;i<=n ; i++) for (j=1;j<=n;j++) if (map[i][j]<=k) ans++;p rintf ("%d\n", ans>>1);} Puts ("");} return 0;}
So I'm going to put a code on the wrong idea.
: SW algorithm for global minimum cut, and then on both sides of any point between the minimum cut value is definitely this value, and then the two sides divided.
SW Algorithm Related blog (click this is link): Say I learn SW is to use to solve this 233 ...
This is a very correct appearance, why hang it?
Let's leave it to the reader to think about it, this is really a question (LI) (related to the SW algorithm implementation process and some values after the partition)
Code (WA):
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 160# Define INF 0x3f3f3f3fusing namespace Std;int n,m,q,crs[n],tcrs[n],cnt;struct ans{int x,sum; ANS (int _x=0,int _sum=0): X (_x), sum (_sum) {}bool operator < (const ANS &a) Const{return x<a.x;}} Ans[n];int Map[n][n]; int Dis[n],f[n];bool vis[n],flag[n];int next[n],final[n];int Find (int x) {return f[x]==x?x:f[x]=find (f[x]);} void Stoer_wagner (int l,int r) {int ret=inf;int i,j,_n,rnd;int u,v,temp,lastu=0;for (i=l;i<=r;i++) f[i]=next[i]= Final[i]=i,flag[i]=0;for (_n=r-l+1;_n>1;_n--) {for (i=l;i<=r;i++) dis[i]=vis[i]=0;for (rnd=1;rnd<=_n;rnd++ {lastu=u;temp=-1;for (i=l;i<=r;i++) if (F[i]==i&&!vis[i]) {if (temp<dis[i]) temp=dis[i],u=i;} Vis[u]=1;bool flag=0;for (j=u;; J=next[j]) {if (J==final[u]) flag=1;for (i=l;i<=r;i++) if (Map[crs[u]][crs[i]]) {v=find (i); if (v!=u) Dis[v]+=map[crs [U]] [Crs[i];} if (flag) break;}} if (Ret>=dis[u]) {for (i=l;i<=r;i++) flag[i]= (f[i]!=i| | I==U); Ret=diS[u];} F[u]=lastu;next[final[lastu]]=u;final[lastu]=final[u];} int L=l-1,r=r+1;for (i=l;i<=r;i++) {if (Flag[i]) tcrs[++l]=crs[i];else tcrs[--r]=crs[i];} for (i=l;i<=r;i++) Crs[i]=tcrs[i];ans[++cnt]=ans (ret, (r-l) * (r-l)), if (r-l>1) Stoer_wagner (l,l); if (r-l>1) Stoer_wagner (r,r); return;} int Sum[n];int Main () {//Freopen ("Test.in", "R", stdin), int i,k,g;int a,b,c;for (scanf ("%d", &g), g--;) {memset (map,0 , sizeof (map)), scanf ("%d%d", &n,&m), for (i=1;i<=n;i++) crs[i]=i;for (i=1;i<=m;i++) {scanf ("%d%d%d", &A,&B,&C); if (a!=b) map[a][b]+=c,map[b][a]+=c;} Stoer_wagner (1,n); sort (ans+1,ans+cnt+1); for (i=1;i<=cnt;i++) sum[i]=sum[i-1]+ans[i].sum; scanf ("%d", &q), while (q--) {scanf ("%d", &k), int l=1,r=cnt,mid,ret;while (L<R) {if (r-l<=3) {ret=0;for (i= l;i<=r;i++) if (ans[i].x<=k) ret=i;break;} Mid=l+r>>1;if (ans[i].x<=k) L=mid;else r=mid-1;} printf ("%d\n", Sum[ret]);}} return 0;}
"BZOJ2229" "ZJOI2011" minimum cut {no mistake, the algorithm with the title cap is the same!!! }