BZOJ3118: Orz the MST

论一早上调试SB错误是什么感觉…
最后发现数组开小(摔
转对偶问题然后上单纯行

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<queue>
using namespace std;
char c;
#define ll int
inline void read(ll &a)
{
    a=0;do c=getchar();while(c<'0'||c>'9');
    while(c<='9'&&c>='0')a=(a<<3)+(a<<1)+c-'0',c=getchar();
}

ll n,m,num;
ll A[1301][30001];
ll nxt[5001];
ll last=-1;ll Cnt;
void Change(ll x,ll y)
{
    ll i,j,last=-1;
    for(i=0;i<=Cnt;i++)
        if(A[x][i])nxt[i]=last,last=i;
    for(i=0;i<=num;i++)
    if(i!=x&&A[i][y])
    {
      ll tp=A[i][y];
      for(j=last;j!=-1;j=nxt[j])
          A[i][j]-=tp*A[x][j];
      A[i][y]-=tp*A[x][y];
   }
}

const
 ll INF=1<<29ll;
ll simplex()
{
  while(true)
  {
    last=0;
    for(ll i=1;i<=Cnt;i++)
      if(A[0][i]>0&&(last==0||A[0][last]<A[0][i])){last=i;}
    if(last==0)return -A[0][0];
    ll Min=INF;
    ll tp;
    for(ll i=1;i<=num;i++)
        if(A[i][last]>0&&A[i][0]<Min)Min=A[i][0],tp=i;
    Change(tp,last);
   } 
}

struct Chain
{
   ll u,w,Val,no;
   Chain *next;
}*Head[301];
struct Ca
{
  ll v,u,w,Val,no;
}Cache[10001];
ll Cur;
inline void Add(ll u,ll v,ll w,ll Val)
{
   Chain *tp=new Chain;
   tp->next=Head[u];
   tp->u=v,tp->w=w,tp->Val=Val;
   tp->no=Cur;
   Head[u]=tp;
}



bool C[301];
Chain* Side[301];
ll pre[301];
queue<int>Q;
inline void Build(ll u,ll v,ll w,ll Val,ll p)
{
    Q.push(u);
    memset(C,0,sizeof(C));
    C[u]=true;
    while(true)
    {
        ll y=Q.front();
        for(Chain*tp=Head[y];tp;tp=tp->next)
          if(!C[tp->u])
             Q.push(tp->u),C[tp->u]=true,Side[tp->u]=tp,pre[tp->u]=y;
       if(C[v])break;     
      Q.pop();
    }
    ll j=v;
    while(!Q.empty())Q.pop();
    for(j;j!=u;j=pre[j])
    {
         ++Cnt;
         A[0][Cnt]=Side[j]->Val-Val;
         A[Side[j]->no][Cnt]=A[p][Cnt]=1;
    }

}

int main()
{

    read(n),read(m);num=m;
    ll R=0,AA,B,C,D,E,F;
    while(m--)
    {
        read(AA),read(B),read(C),read(D),read(E),read(F);
        A[num-m][0]=D?F:E;
        Cur++;
        if(D){Add(AA,B,F,C),Add(B,AA,F,C);}
          else Cache[++R]=(Ca){AA,B,E,C,Cur};
    }
    for(ll i=1;i<=R;i++)
        Build(Cache[i].u,Cache[i].v,Cache[i].w,Cache[i].Val,Cache[i].no);
    ll ans=simplex();
    printf("%d\n",ans);
    return 0;
}

你可能感兴趣的:(BZOJ3118: Orz the MST)