按照题目要求建矩阵就好了→_→,其实是为了存模板。
单纯形做法:
#include <cstdio> #include <cmath> const double eps = 1e-7; const double inf = 1e30; #define FOR(i,j,k) for(i=j;i<=k;i++) int read() { int s=0,f=1;char ch=getchar(); for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1; for(;'0'<=ch&&ch<='9';ch=getchar())s=s*10+ch-'0'; return s*f; } template<int M, int N> void pivot(double (&a)[M][N], double (&b)[M], double (&c)[N], int m, int n, int l, int e, int &v) { int i, j, k; b[l] /= a[l][e]; FOR(i,1,n) if (i!=e) a[l][i]/=a[l][e]; a[l][e]=1/a[l][e]; FOR(i,1,m)if(i!=l&&fabs(a[i][e])>eps) { b[i]-=a[i][e]*b[l]; FOR(j,1,n)if(j!=e) a[i][j]-=a[i][e]*a[l][j]; a[i][e]=-a[i][e]*a[l][e]; } v+=c[e]*b[l]; FOR(i,1,n)if(i!=e)c[i]-=c[e]*a[l][i]; c[e]=-c[e]*a[l][e]; } template<int M, int N> double simplex(double (&a)[M][N], double (&b)[M], double (&c)[N], int m, int n, int v) { int i,e; while (1) { FOR(i,1,n) if (c[i]>eps) break; if (i==n+1) return v; double t=inf,l;e=i; FOR(i,1,m) if(a[i][e]>eps&&t>b[i]/a[i][e]) t=b[i]/a[i][e],l=i; if(t==inf) return inf; pivot(a, b, c, m, n, l, e, v); } return inf; } int main() { int i,j,x,y,z; const int M = 10001, N = 1001; static double c[N], a[M][N], b[M]; int n=read(),m=read(); FOR(i,1,n) c[i]=read(); FOR(i,1,m) { x=read(),y=read(),z=read(); FOR(j,x,y) a[i][j]=1; b[i]=z; } printf("%d",(int)(simplex(a,b,c,m,n,0)+0.5)); return 0; }
费用流做法:
#include <cstdio> #include <cstring> #define FOR(i,j,k) for(i=j;i<=k;i++) #define min(i,j) ((i)>(j)?(j):(i)) const int inf = 2147483647; int read() { int s=0,f=1;char ch=getchar(); for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1; for(;'0'<=ch&&ch<='9';ch=getchar())s=s*10+ch-'0'; return s*f; } template<int N, int M> class EdmondsKarp { private: int h[N], v[M], w[M], p[M], c[M], cnt; int n, m, ans, s, t; int q[N*64], pre[N], dis[N]; bool vis[M]; bool spfa() { int i, f = 0, r = 0; for(i = 0; i <= n; i ++) { dis[i] = inf; vis[i] = 0; } dis[s] = 0, q[r++] = s, vis[s] = 1; pre[s] = pre[t] = -1; while (f < r) { int u = q[f++]; for(i = h[u]; i != -1; i = p[i]) { if(w[i] && dis[v[i]] > dis[u] + c[i]){ dis[v[i]] = dis[u] + c[i]; pre[v[i]] = i^1; if(!vis[v[i]]) { vis[v[i]] = 1; q[r++] = v[i]; } } } vis[u] = 0; } if(dis[t] == inf) return 0; return 1; } void end() { int u, sum = inf; for(u = pre[t]; u >= 0; u = pre[v[u]]) { sum = min(sum, w[u^1]); } for(u = pre[t]; u >= 0; u = pre[v[u]]) { w[u] += sum; w[u^1] -= sum; ans += sum * c[u^1]; } } public: void init(int o, int a, int b) { n = o, s = a, t = b, cnt = 0; memset(h,-1,sizeof(h)); } void add(int x, int y, int ca, int co) { v[cnt] = y, w[cnt] = ca, c[cnt] = co, p[cnt] = h[x], h[x] = cnt++; v[cnt] = x, w[cnt] = 0, c[cnt] = -co, p[cnt] = h[y], h[y] = cnt++; } int run() { ans = 0; while(spfa()) end(); return ans; } }; int main() { const int N = 4001; static EdmondsKarp<N,40001> g; static int c[N]; int n=read(),m=read(),s=0,t=n+2,i; FOR(i,1,n)c[i]=read(); g.init(t,s,t); FOR(i,1,m) { int x=read(),y=read(),z=read(); g.add(x,y+1,inf,z); } FOR(i,1,n+1) { int d=c[i]-c[i-1]; if(d>=0)g.add(s,i,d,0); else g.add(i,t,-d,0); if(i>1)g.add(i,i-1,inf,0); } printf("%d",g.run()); return 0; }