HDU

来源:互联网 发布:超融合软件dell 编辑:程序博客网 时间:2024/06/03 14:11
#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <algorithm>#include <vector>#define me(x) memset(x,0,sizeof(x));using namespace std;#define INF 0x3f3f3f3f#define mod 1000000009const int maxn = 333;const int maxm = 23333;//const int INF = 0x3f3f3f3f;struct E{    int to, next;    int cap, flow;}edge[maxm], bed[3000], tedge[maxm];int tol;int head[maxn], cur[maxn], d[maxn], thead[maxm];int P[maxn], GAP[maxn];int node, st, ed, begnum;void init(int A, int B,int C){    st = A, ed = B, node = C;    begnum = 0;    tol = 0;    memset(head, -1, sizeof(head));}void addedge(int U, int V, int W, int RW = 0){    edge[tol].to = V; edge[tol].cap = W; edge[tol].flow = 0;    edge[tol].next = head[U]; head[U] = tol ++;    edge[tol].to = U; edge[tol].cap = RW; edge[tol].flow = 0;    edge[tol].next = head[V]; head[V] = tol ++;}void BFS(int S,int T){    memset(d, -1, sizeof(d));    memset(GAP, 0, sizeof(GAP));    queue<int> QUE;    GAP[0] = 1;    d[T] = 0;    QUE.push(T);    while (!QUE.empty()){        int U = QUE.front(); QUE.pop();        for (int i = head[U]; i != -1;i = edge[i].next){            int V = edge[i].to;            if (d[V] == -1){                d[V] = d[U] + 1;                GAP[d[V]] ++;                QUE.push(V);            }        }    }}int STACK[maxn];int SAP(int S, int T, int N){    BFS(S, T);    if (d[S] == -1) return 0;    memcpy(cur, head, sizeof(head));    int top = 0;    int U = S;    int ans = 0;    while (d[S] < N){        if (U == T){            int MIN = INF;            int INSER;            for (int i = 0;i < top;i ++)                if (MIN > edge[STACK[i]].cap - edge[STACK[i]].flow){                    MIN = edge[STACK[i]].cap - edge[STACK[i]].flow;                    INSER = i;                }            for (int i = 0;i < top;i ++){                edge[STACK[i]].flow += MIN;                edge[STACK[i] ^ 1].flow -= MIN;            }            ans += MIN;            top = INSER;            U = edge[STACK[top] ^ 1].to;            continue;        }        bool FLAG = false;        int V;        for (int i = cur[U];i != -1; i = edge[i].next){            V = edge[i].to;            if (edge[i].cap - edge[i].flow > 0&& d[V] + 1 == d[U]){                FLAG = true;                cur[U] = i;                break;            }        }        if (FLAG){            STACK[top ++] = cur[U];            U = V;            continue;        }        int MIN = N;        for (int i = head[U]; i != -1;i = edge[i].next){            if (edge[i].cap - edge[i].flow > 0&& d[edge[i].to] < MIN){                MIN = d[edge[i].to];                cur[U] = i;            }        }        GAP[d[U]] --;        if (!GAP[d[U]]) return ans;        d[U] = MIN + 1;        GAP[d[U]] ++;        if (U != S) U = edge[STACK[--top] ^ 1].to;    }    return ans;}E Q[maxm];int f[maxn];int n,m;int main(){    int t, pi, si, ei;    //while(scanf("%d", &t)!=EOF)  //  {       // t=1;        scanf("%d", &t);        for (int cas = 1; cas <= t; ++cas)        {            scanf("%d%d",&n,&m);            init(1,n,n);            for(int i=1;i<=m;i++)            {                int x,y,z;                scanf("%d%d%d",&x,&y,&z);                addedge(x,y,z);            }            int cnt=SAP(1,n,n);            me(Q);me(f);            queue<int>q;            q.push(1);            f[1]=1;            while(!q.empty())            {                int now=q.front();               // cout<<now<<endl;                q.pop();                for (int x= head[now]; x != -1; x = edge[x].next)                {                    int d=edge[x].to;                   // cout<<d<<endl;                    if(edge[x].cap - edge[x].flow>0&&!f[d])                    {                        q.push(d);                        f[d]=1;                    }                }            }          //  for(int i=1;i<=n;i++)              //  cout<<f[i]<<" ";            //cout<<endl;          //  cout<<idx<<endl;            int val=0,ans;            int T=tol;          //  cout<<cnt<<endl;             for(int i=0;i<tol;i++)                Q[i]=edge[i];            for(int i=2;i<=n-1;i++)                if(f[i])                for(int j=2;j<=n-1;j++)            {                if(!f[j])                {                    int x=head[i],y=head[j];                  //  cout<<x<<"  "<<y<<endl;                    addedge(i,j,INF);                    ans=SAP(1,n,n);                    if(ans>val) val=ans;                    tol = T;                    head[i]=x;head[j]=y;                    for(int tt=0;tt<tol;tt++)                        edge[tt]=Q[tt];                }            }            printf("%d\n",cnt+val);        }   // }    return 0;}

原创粉丝点击