[BZOJ] 1520: [POI2006]Szk-Schools

费用流解决。

abs内传不了int..CE一次

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
 
using namespace std;
 
inline int rd(){
  int ret=0,f=1;char c;
  while(c=getchar(),!isdigit(c))f=c=='-'?-1:1;
  while(isdigit(c))ret=ret*10+c-'0',c=getchar();
  return ret*f;
}
 
const int MAXN=256<<1;
const int M=MAXN*MAXN;
 
struct Edge{
  int next,to,f,w;
}e[M<<1];
int ecnt=1,head[MAXN];
inline void add(int x,int y,int f,int w){
  e[++ecnt].next = head[x];
  e[ecnt].to = y;
  e[ecnt].f = f;
  e[ecnt].w = w;
  head[x] = ecnt;
}
 
int n,S,T;
 
int dis[MAXN],pre[MAXN],flow[MAXN],inq[MAXN];
queue<int> Q;
bool spfa(int s,int t){
  memset(dis,0x3f,sizeof(dis));
  Q.push(s);dis[s]=0;flow[s]=1<<30;
  while(!Q.empty()){
    int x=Q.front();Q.pop();inq[x]=0;
    for(int i=head[x];i;i=e[i].next){
      int v=e[i].to;
      if(!e[i].f||dis[v]<=dis[x]+e[i].w) continue;
      flow[v]=min(flow[x],e[i].f);
      pre[v]=i;dis[v]=dis[x]+e[i].w;
      if(!inq[v]) Q.push(v),inq[v]=1;
    }
  }
  return dis[t]!=0x3f3f3f3f;
}
 
int mxflow,mncost;
void update(int s,int t){
  int cur=t,mx=flow[t];
  while(cur!=s){
    int i=pre[cur];
    e[i].f-=mx;e[i^1].f+=mx;
    cur=e[i^1].to;
  }
  mxflow+=mx;mncost+=dis[t]*mx;
}
 
void EK(int s,int t){while(spfa(s,t))update(s,t);}
 
int main(){
  n=rd();S=n+n+1;T=n+n+2;
  int x,y,z,w;
  for(int i=1;i<=n;i++){
    x=rd();y=rd();z=rd();w=rd();
    for(int j=y;j<=z;j++){
      int v=w*abs(1.0*j-1.0*x);
      add(i,j+n,1,v);add(j+n,i,0,-v);
    }
  }
  for(int i=1;i<=n;i++){
    add(S,i,1,0);add(i,S,0,0);
    add(i+n,T,1,0);add(T,i+n,0,0);
  }
  EK(S,T);
  if(mxflow<n) return puts("NIE"),0;
  printf("%d\n",mncost);
  return 0;
}

 

posted @ 2018-08-18 20:34  GhostCai  阅读(92)  评论(0编辑  收藏  举报