【题目链接】

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

【题意】

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

【思路一】

Byvoid的题解 click here

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

【代码】

 #include<set>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define trav(u,i) for(int i=front[u];i;i=e[i].nxt)
#define FOR(a,b,c) for(int a=(b);a<=(c);a++)
using namespace std; typedef long long ll;
const int N = 2e3+;
const int inf = 1e9; ll read() {
char c=getchar();
ll f=,x=;
while(!isdigit(c)) {
if(c=='-') f=-; c=getchar();
}
while(isdigit(c))
x=x*+c-'',c=getchar();
return x*f;
} struct Edge {
int u,v,cap,flow,cost;
};
struct MCMF {
int n,m,s,t;
int a[N],p[N],inq[N],d[N];
vector<Edge> es;
vector<int> g[N];
queue<int> q;
void init(int n) {
this->n=n;
es.clear();
FOR(i,,n) g[i].clear();
}
void AddEdge(int u,int v,int w,int c) {
es.push_back((Edge){u,v,w,,c});
es.push_back((Edge){v,u,,,-c});
int m=es.size();
g[u].push_back(m-);
g[v].push_back(m-);
}
int spfa(int s,int t,int& flow,int& cost) {
memset(inq,,sizeof(inq));
FOR(i,,n) d[i]=inf;
inq[s]=; d[s]=; a[s]=inf; p[s]=;
q.push(s);
while(!q.empty()) {
int u=q.front(); q.pop(); inq[u]=;
FOR(i,,(int)g[u].size()-) {
Edge& e=es[g[u][i]];
int v=e.v;
if(d[v]>d[u]+e.cost && e.cap>e.flow) {
d[v]=d[u]+e.cost;
a[v]=min(a[u],e.cap-e.flow);
p[v]=g[u][i];
if(!inq[v])
inq[v]=,q.push(v);
}
}
}
if(d[t]==inf) return ;
flow+=a[t]; cost+=a[t]*d[t];
for(int x=t;x!=s;x=es[p[x]].u) {
es[p[x]].flow+=a[t];
es[p[x]^].flow-=a[t];
}
return ;
}
void mcmf(int s,int t,int&flow,int&cost) {
flow=cost=;
while(spfa(s,t,flow,cost)) ;
}
} mc; int n,m,x[N]; int main()
{
n=read(),m=read();
mc.init(n+);
int S=,T=n+;
mc.AddEdge(S,,inf,);
FOR(i,,n) {
x[i]=read();
mc.AddEdge(i,i+,inf,);
}
FOR(i,,m) {
int l=read(),r=read(),c=read();
mc.AddEdge(l,r+,inf,c);
}
FOR(i,,n+) {
int c=x[i]-x[i-];
if(c>) mc.AddEdge(S,i,c,);
if(c<) mc.AddEdge(i,T,-c,);
}
int flow,cost;
mc.mcmf(S,T,flow,cost);
printf("%d",cost);
return ;
}

CODE 1

【思路二】

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

【代码】

 #include<set>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define trav(u,i) for(int i=front[u];i;i=e[i].nxt)
#define FOR(a,b,c) for(int a=(b);a<=(c);a++)
using namespace std; typedef long long ll;
const int N = 2e3+;
const int inf = 1e9; ll read() {
char c=getchar();
ll f=,x=;
while(!isdigit(c)) {
if(c=='-') f=-; c=getchar();
}
while(isdigit(c))
x=x*+c-'',c=getchar();
return x*f;
} struct Edge {
int u,v,cap,flow,cost;
};
struct MCMF {
int n,m,s,t;
int a[N],p[N],inq[N],d[N];
vector<Edge> es;
vector<int> g[N];
queue<int> q;
void init(int n) {
this->n=n;
es.clear();
FOR(i,,n) g[i].clear();
}
void AddEdge(int u,int v,int w,int c) {
es.push_back((Edge){u,v,w,,c});
es.push_back((Edge){v,u,,,-c});
int m=es.size();
g[u].push_back(m-);
g[v].push_back(m-);
}
int spfa(int s,int t,int& flow,int& cost) {
memset(inq,,sizeof(inq));
FOR(i,,n) d[i]=inf;
inq[s]=; d[s]=; a[s]=inf; p[s]=;
q.push(s);
while(!q.empty()) {
int u=q.front(); q.pop(); inq[u]=;
FOR(i,,(int)g[u].size()-) {
Edge& e=es[g[u][i]];
int v=e.v;
if(d[v]>d[u]+e.cost && e.cap>e.flow) {
d[v]=d[u]+e.cost;
a[v]=min(a[u],e.cap-e.flow);
p[v]=g[u][i];
if(!inq[v])
inq[v]=,q.push(v);
}
}
}
if(d[t]==inf) return ;
flow+=a[t]; cost+=a[t]*d[t];
for(int x=t;x!=s;x=es[p[x]].u) {
es[p[x]].flow+=a[t];
es[p[x]^].flow-=a[t];
}
return ;
}
void mcmf(int s,int t,int&flow,int&cost) {
flow=cost=;
while(spfa(s,t,flow,cost)) ;
}
} mc; int n,m; int main()
{
n=read(),m=read();
mc.init(n+);
int S=,T=n+;
mc.AddEdge(S,,inf,);
FOR(i,,n) {
int c=read();
mc.AddEdge(i,i+,inf-c,);
}
FOR(i,,m) {
int l=read(),r=read(),c=read();
mc.AddEdge(l,r+,inf,c);
}
int flow,cost;
mc.mcmf(S,T,flow,cost);
printf("%d",cost);
return ;
}

CODE 2

05-07 15:55