NOI2008 志愿者招募

题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1061
这可能那年NOI最简单的题了
乍一看是费用流,可是不会连边怎么办
先空着

/**************************************************************
    Problem: 1061
    User: Gvolv
    Language: C++
    Result: Accepted
    Time:1392 ms
    Memory:4792 kb
****************************************************************/

#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define INF 1000000000
#define eps 1e-10
#define sqr(x) (x)*(x)
#define pa pair
#define cyc(i,x,y) for(i=(x);i<=(y);i++)
#define cy2(i,x,y) for(i=(x);i>=(y);i--)
using namespace std;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
    return x*f;
}
#define N 100030
int tot=1,n,m,s,t,a[2],head[N],from[N];
int d[N],v[N];
struct Node{
    int from,go,next,v,c;
}e[N];
void addedge(int x,int y,int z,int c)
{
    e[++tot]=(Node){x,y,head[x],z,c};head[x]=tot;
    e[++tot]=(Node){y,x,head[y],0,-c};head[y]=tot;
}
queue<int> q;
int spfa()
{
    for(int i=s;i<=t;i++) d[i]=INF,v[i]=0;
    d[s]=0;v[s]=1;q.push(s);
    while(!q.empty())
    {
        int x=q.front();q.pop();v[x]=0;
        for(int i=head[x],y;i;i=e[i].next)
         if(e[i].v&&d[x]+e[i].cif(!v[y]) {v[y]=1;q.push(y);}
         }
    }
    return d[t]!=INF;
}
int mcf()
{
    int mincost=0;
    while(spfa())
    {
        int tmp=INF;
        for(int i=from[t];i;i=from[e[i].from]) tmp=min(tmp,e[i].v);
        mincost+=tmp*d[t];
        for(int i=from[t];i;i=from[e[i].from])e[i].v-=tmp,e[i^1].v+=tmp;
    }
    return mincost;
}
int main()
{
//  freopen("input.in","r",stdin);
//  freopen("output.out","w",stdout);
    int i,x,y,z,o=0;
    n=read(),m=read();
    s=0,t=n+2;
    cyc(i,1,n) 
    {
       a[o^=1]=read();
       if(a[o]-a[o^1]>=0) addedge(s,i,a[o]-a[o^1],0);
       else addedge(i,t,a[o^1]-a[o],0);
       if(i!=1) addedge(i,i-1,INF,0);
    }  
    addedge(n+1,t,a[o],0);
    addedge(n+1,n,INF,0);
    cyc(i,1,m) 
    {
        x=read(),y=read(),z=read();
        addedge(x,y+1,INF,z);
    }
    printf("%d\n",mcf());
    return 0;
}

你可能感兴趣的:(noip)