gusfield

BZOJ2229

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

  int cnt,nd[151],dep[151],sor,tar,dl[151],cur[151],sta[10001],mark[151],a[151],n,ans[151][151],tmp[151],m;

  struct edge{
      int next,des,cap,fr;
  }sid[10001];

  void addedge2(int u,int v,int cap){
      sid[cnt].next=nd[u];sid[cnt].des=v;sid[cnt].cap=cap;nd[u]=cnt;sid[cnt].fr=u;cnt++;
      sid[cnt].next=nd[v];sid[cnt].des=u;sid[cnt].cap=cap;nd[v]=cnt;sid[cnt].fr=v;cnt++;
  }

   int bfs(){
      memset(dep,-1,sizeof(dep));

      int head=1,tail=1;
      dep[sor]=0;dl[head]=sor;
      while (head<=tail){
        for (int p=nd[dl[head]];p!=-1;p=sid[p].next)
          if ((dep[sid[p].des]==-1)&&(sid[p].cap)) dep[sid[p].des]=dep[dl[head]]+1,dl[++tail]=sid[p].des;
        head++;
    }

    if (dep[tar]==-1) return(0);else return(1);
   }

    int dinic(){
      int maxflow=0;
      while (bfs()){
          for (int i=1;i<=n;i++) cur[i]=nd[i];

          int u=sor,top=0;
            while(1){
              if (u==tar){
                  int mi=1e9,last;
                  for (int i=1;i<=top;i++)
                    if (sid[sta[i]].cap<mi)
                    {mi=sid[sta[i]].cap;last=i;}

                  for (int i=1;i<=top;i++)
                  sid[sta[i]].cap-=mi,sid[sta[i]^1].cap+=mi;
                u=sid[sta[last]].fr;cur[u]=sid[cur[u]].next;top=last-1;
                maxflow+=mi;
                continue;
            }

            while((cur[u]!=-1)&&((sid[cur[u]].cap==0)||(dep[sid[cur[u]].des]!=dep[u]+1)))
              cur[u]=sid[cur[u]].next;

            if (cur[u]!=-1){sta[++top]=cur[u];u=sid[cur[u]].des;continue;}
            else{
                if (u==sor) break;
                dep[u]=-1;
                u=sid[sta[top--]].fr;
                continue;
            }
          }
      }
      return(maxflow);
  }

  void restore(){
      for (int i=0;i<=cnt;i+=2)
      sid[i].cap=sid[i^1].cap=(sid[i].cap+sid[i^1].cap)>>1;
  }

  void dfs(int po){
      mark[po]=1;
      for (int p=nd[po];p!=-1;p=sid[p].next)
        if (sid[p].cap&&!mark[sid[p].des])
          dfs(sid[p].des);
  }

  void solve(int l,int r){
      if (l==r) return;
      restore();
      sor=a[l];tar=a[r];
      int flow=dinic();

      for (int i=1;i<=n;i++) mark[i]=0;
      dfs(sor);
      for (int i=1;i<=n;i++) if (mark[i])
        for (int j=1;j<=n;j++) if (!mark[j])
          ans[i][j]=ans[j][i]=min(ans[i][j],flow);
      int pol=l-1,por=r+1;
    for (int i=l;i<=r;i++)
      if (mark[a[i]]) tmp[++pol]=a[i];else tmp[--por]=a[i];
    for (int i=l;i<=r;i++) a[i]=tmp[i];
    solve(l,pol);solve(por,r);
  }

  int main(){
      int T;
      scanf("%d",&T);
      while (T--){
        scanf("%d%d",&n,&m);cnt=0;
      for (int i=1;i<=n;i++) nd[i]=-1,a[i]=i;
      for (int i=1;i<=n;i++) for (int j=1;j<=n;j++)
        ans[i][j]=1e9;
      for (int i=1;i<=m;i++){
          int t1,t2,t3;
          scanf("%d%d%d",&t1,&t2,&t3);
          addedge2(t1,t2,t3);
      }
      solve(1,n);
      int q;
      scanf("%d",&q);
      while (q--){
          int lim,tans=0;
          scanf("%d",&lim);
          for (int i=1;i<=n;i++) for (int j=i+1;j<=n;j++)
            if (ans[i][j]<=lim) tans++;
          printf("%d",tans);if (q!=0||T!=0) printf("\n");
      }
      if (T!=0) printf("\n");
    }
  }
时间: 2024-11-25 21:36:26

gusfield的相关文章

【BZOJ-2229】最小割 最小割树(最大流+分治)

2229: [Zjoi2011]最小割 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1565  Solved: 560[Submit][Status][Discuss] Description 小白在图论课上学到了一个新的概念——最小割,下课后小白在笔记本上写下了如下这段话: “对于一个图,某个对图中结点的划分将图中所有结点分成两个部分,如果结点s,t不在同一个部分中,则称这个划分是关于s,t的割. 对于带权图来说,将所有顶点处在不同部分的边的

BZOJ 2229 最小割

谁会像我一样把INF设成0x3f? GH Tree. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #define maxv 250 #define maxe 50050 #define inf 0x7fffffff using namespace std; struct edge { int v,f,nxt; }

高级数据结构

数据结构清单 Binomial Heap Leftist Tree:左倾堆 ? 重型网络教程 1.纸上谈兵:算法与数据结构 2.CS 598 JGE:Advanced Data Structures(Fall 2015) 3.COP 5536 Advanced Data Structures(Florida) 4.wikibooks Data Structures 5.Geeksforgeeks(very much) 6.COMP 5408:Advanced Data Struestures 7

(转)Awesome Courses

Awesome Courses  Introduction There is a lot of hidden treasure lying within university pages scattered across the internet. This list is an attempt to bring to light those awesome courses which make their high-quality material i.e. assignments, lect

BZOJ 4519 不同的最小割

GH树...好像无向图上跑DINIC要慢一点. 反正能过就是了. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #define maxe 200050 #define maxv 1050 const int inf=0x7fffffff/2; using namespace std; struct edge { i