bzoj 1061 [Noi2008]志愿者招募(数学模型,MCMF)

 

【题目链接】

 

    http://www.lydsy.com/JudgeOnline/problem.php?id=1061

 

【题意】

 

    雇人满足每天至少需要的人数。

 

【思路一】

 

    Byvoid的题解 click here

    任意一个变量在两个方程组中且一正一负,根据流量守恒的原理构图。正变量看作流入量,负变量看作流出量,正负常数看作与源汇点的流量。

 

【代码】

 

  1 #include<set>
  2 #include<cmath>
  3 #include<queue>
  4 #include<vector>
  5 #include<cstdio>
  6 #include<cstring>
  7 #include<iostream>
  8 #include<algorithm>
  9 #define trav(u,i) for(int i=front[u];i;i=e[i].nxt)
 10 #define FOR(a,b,c) for(int a=(b);a<=(c);a++)
 11 using namespace std;
 12 
 13 typedef long long ll;
 14 const int N = 2e3+10;
 15 const int inf = 1e9;
 16 
 17 ll read() {
 18     char c=getchar();
 19     ll f=1,x=0;
 20     while(!isdigit(c)) {
 21         if(c=='-') f=-1; c=getchar();
 22     }
 23     while(isdigit(c))
 24         x=x*10+c-'0',c=getchar();
 25     return x*f;
 26 }
 27 
 28 struct Edge {
 29     int u,v,cap,flow,cost;
 30 };
 31 struct MCMF {
 32     int n,m,s,t;
 33     int a[N],p[N],inq[N],d[N];
 34     vector<Edge> es;
 35     vector<int> g[N];
 36     queue<int> q;
 37     void init(int n) {
 38         this->n=n;
 39         es.clear();
 40         FOR(i,0,n) g[i].clear();
 41     }
 42     void AddEdge(int u,int v,int w,int c) {
 43         es.push_back((Edge){u,v,w,0,c});
 44         es.push_back((Edge){v,u,0,0,-c});
 45         int m=es.size();
 46         g[u].push_back(m-2);
 47         g[v].push_back(m-1);
 48     }
 49     int spfa(int s,int t,int& flow,int& cost) {
 50         memset(inq,0,sizeof(inq));
 51         FOR(i,0,n) d[i]=inf;
 52         inq[s]=1; d[s]=0; a[s]=inf; p[s]=0; 
 53         q.push(s);
 54         while(!q.empty()) {
 55             int u=q.front(); q.pop(); inq[u]=0;
 56             FOR(i,0,(int)g[u].size()-1) {
 57                 Edge& e=es[g[u][i]];
 58                 int v=e.v;
 59                 if(d[v]>d[u]+e.cost && e.cap>e.flow) {
 60                     d[v]=d[u]+e.cost;
 61                     a[v]=min(a[u],e.cap-e.flow);
 62                     p[v]=g[u][i];
 63                     if(!inq[v])
 64                         inq[v]=1,q.push(v);
 65                 }
 66             }
 67         }
 68         if(d[t]==inf) return 0;
 69         flow+=a[t]; cost+=a[t]*d[t];
 70         for(int x=t;x!=s;x=es[p[x]].u) {
 71             es[p[x]].flow+=a[t];
 72             es[p[x]^1].flow-=a[t];
 73         }
 74         return 1;
 75     }
 76     void mcmf(int s,int t,int&flow,int&cost) {
 77         flow=cost=0;
 78         while(spfa(s,t,flow,cost)) ;
 79     }
 80 } mc;
 81 
 82 int n,m,x[N];
 83 
 84 int main()
 85 {
 86     n=read(),m=read();
 87     mc.init(n+2);
 88     int S=0,T=n+1;
 89     mc.AddEdge(S,1,inf,0);
 90     FOR(i,1,n) {
 91         x[i]=read();
 92         mc.AddEdge(i,i+1,inf,0);
 93     }
 94     FOR(i,1,m) {
 95         int l=read(),r=read(),c=read();
 96         mc.AddEdge(l,r+1,inf,c);
 97     }
 98     FOR(i,1,n+1) {
 99         int c=x[i]-x[i-1];
100         if(c>0) mc.AddEdge(S,i,c,0);
101         if(c<0) mc.AddEdge(i,T,-c,0);
102     }
103     int flow,cost;
104     mc.mcmf(S,T,flow,cost);
105     printf("%d",cost);
106     return 0;
107 }
CODE 1

 

【思路二】

 

    一种比较神奇的构图方式 click here

 

【代码】

 

  1 #include<set>
  2 #include<cmath>
  3 #include<queue>
  4 #include<vector>
  5 #include<cstdio>
  6 #include<cstring>
  7 #include<iostream>
  8 #include<algorithm>
  9 #define trav(u,i) for(int i=front[u];i;i=e[i].nxt)
 10 #define FOR(a,b,c) for(int a=(b);a<=(c);a++)
 11 using namespace std;
 12 
 13 typedef long long ll;
 14 const int N = 2e3+10;
 15 const int inf = 1e9;
 16 
 17 ll read() {
 18     char c=getchar();
 19     ll f=1,x=0;
 20     while(!isdigit(c)) {
 21         if(c=='-') f=-1; c=getchar();
 22     }
 23     while(isdigit(c))
 24         x=x*10+c-'0',c=getchar();
 25     return x*f;
 26 }
 27 
 28 struct Edge {
 29     int u,v,cap,flow,cost;
 30 };
 31 struct MCMF {
 32     int n,m,s,t;
 33     int a[N],p[N],inq[N],d[N];
 34     vector<Edge> es;
 35     vector<int> g[N];
 36     queue<int> q;
 37     void init(int n) {
 38         this->n=n;
 39         es.clear();
 40         FOR(i,0,n) g[i].clear();
 41     }
 42     void AddEdge(int u,int v,int w,int c) {
 43         es.push_back((Edge){u,v,w,0,c});
 44         es.push_back((Edge){v,u,0,0,-c});
 45         int m=es.size();
 46         g[u].push_back(m-2);
 47         g[v].push_back(m-1);
 48     }
 49     int spfa(int s,int t,int& flow,int& cost) {
 50         memset(inq,0,sizeof(inq));
 51         FOR(i,0,n) d[i]=inf;
 52         inq[s]=1; d[s]=0; a[s]=inf; p[s]=0; 
 53         q.push(s);
 54         while(!q.empty()) {
 55             int u=q.front(); q.pop(); inq[u]=0;
 56             FOR(i,0,(int)g[u].size()-1) {
 57                 Edge& e=es[g[u][i]];
 58                 int v=e.v;
 59                 if(d[v]>d[u]+e.cost && e.cap>e.flow) {
 60                     d[v]=d[u]+e.cost;
 61                     a[v]=min(a[u],e.cap-e.flow);
 62                     p[v]=g[u][i];
 63                     if(!inq[v])
 64                         inq[v]=1,q.push(v);
 65                 }
 66             }
 67         }
 68         if(d[t]==inf) return 0;
 69         flow+=a[t]; cost+=a[t]*d[t];
 70         for(int x=t;x!=s;x=es[p[x]].u) {
 71             es[p[x]].flow+=a[t];
 72             es[p[x]^1].flow-=a[t];
 73         }
 74         return 1;
 75     }
 76     void mcmf(int s,int t,int&flow,int&cost) {
 77         flow=cost=0;
 78         while(spfa(s,t,flow,cost)) ;
 79     }
 80 } mc;
 81 
 82 int n,m;
 83 
 84 int main()
 85 {
 86     n=read(),m=read();
 87     mc.init(n+2);
 88     int S=0,T=n+1;
 89     mc.AddEdge(S,1,inf,0);
 90     FOR(i,1,n) {
 91         int c=read();
 92         mc.AddEdge(i,i+1,inf-c,0);
 93     }
 94     FOR(i,1,m) {
 95         int l=read(),r=read(),c=read();
 96         mc.AddEdge(l,r+1,inf,c);
 97     }
 98     int flow,cost;
 99     mc.mcmf(S,T,flow,cost);
100     printf("%d",cost);
101     return 0;
102 }
CODE 2

 

你可能感兴趣的:(bzoj 1061 [Noi2008]志愿者招募(数学模型,MCMF))