BZOJ3265: 志愿者招募加强版&&1061: [Noi2008]志愿者招募

通过对偶转化式子
然后就直接上单纯行

3265:

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

double  A[10031][1031];

#define abs(a) (a<0?-a:a)

const
  double  eps=1e-5;
char c;
inline void read(int &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();
}
int n,m;
inline void change(int x,int y)
{
    for(int i=0;i<=m;i++)
     if((i^x)&&abs(A[i][y])>eps)
     {
        double  tp=A[i][y]/A[x][y];
        for(int j=0;j<=n;j++)
            A[i][j]-=tp*A[x][j];
        A[i][y]-=tp*A[x][y];
     }
}

const
 int INF=1e9;
inline double  simplex()
{
    while(true)
    {
        int x=0;
        for(int i=1;i<=n;i++)
         if(A[0][i]>eps&&(x==0||A[0][i]>A[0][x])){x=i;}
        if(x==0)return -A[0][0];
        double  Min=INF;
        int tp=0;
        for(int i=1;i<=m;i++)
            if(A[i][x]>eps&&Min>A[i][0]/A[i][x])Min=A[i][0]/A[i][x],tp=i;
        if(tp==0){A[0][x]=0;return -A[0][0];}
        change(tp,x);
    }
}


int main()
{

   read(n),read(m);
   int tp;
   for(int i=1;i<=n;i++)
      read(tp),A[0][i]=tp;
    for(int i=1;i<=m;i++)
    {
        int k,s,t,c;
        read(k);
        while(k--){read(s),read(t);for(int j=s;j<=t;j++)A[i][j]=1;}
        read(c);A[i][0]=c;
    }
    double  a=simplex();
    printf("%d\n",(int)(a+eps));
    return 0;
}

1061:

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

double  A[10031][1031];

#define abs(a) (a<0?-a:a)

const
  double  eps=1e-5;
char c;
inline void read(int &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();
}
int n,m;
inline void change(int x,int y)
{
    for(int i=0;i<=m;i++)
     if((i^x)&&abs(A[i][y])>eps)
     {
        double  tp=A[i][y]/A[x][y];
        for(int j=0;j<=n;j++)
            A[i][j]-=tp*A[x][j];
        A[i][y]-=tp*A[x][y];
     }
}

const
 int INF=1e9;
inline double  simplex()
{
    while(true)
    {
        int x=0;
        for(int i=1;i<=n;i++)
         if(A[0][i]>eps&&(x==0||A[0][i]>A[0][x])){x=i;}
        if(x==0)return -A[0][0];
        double  Min=INF;
        int tp=0;
        for(int i=1;i<=m;i++)
            if(A[i][x]>eps&&Min>A[i][0]/A[i][x])Min=A[i][0]/A[i][x],tp=i;
        if(tp==0){A[0][x]=0;return -A[0][0];}
        change(tp,x);
    }
}


int main()
{
    freopen("std.in","r",stdin);
    freopen("self.out","w",stdout);
   read(n),read(m);
   int tp;
   for(int i=1;i<=n;i++)
      read(tp),A[0][i]=tp;
    for(int i=1;i<=m;i++)
    {
        int k,s,t,c;
        read(s),read(t);for(int j=s;j<=t;j++)A[i][j]=1;
        read(c);A[i][0]=c;
    }
    double  a=simplex();
    printf("%d\n",(int)(a+eps));
    return 0;
}

你可能感兴趣的:(BZOJ3265: 志愿者招募加强版&&1061: [Noi2008]志愿者招募)