zoukankan      html  css  js  c++  java
  • BZOJ3112 [Zjoi2013]防守战线 【单纯形】

    题目链接

    BZOJ3112

    题解

    同志愿者招募
    费用流神题
    单纯形裸题
    (BZOJ)可过
    洛谷被卡。。

    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdlib>
    #include<cstdio>
    #include<ctime>
    #include<cmath>
    #include<map>
    #define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
    #define REP(i,n) for (int i = 1; i <= (n); i++)
    #define mp(a,b) make_pair<int,int>(a,b)
    #define cls(s) memset(s,0,sizeof(s))
    #define cp pair<int,int>
    #define LL long long int
    using namespace std;
    const int maxn = 1005,maxm = 10005;
    const double eps = 1e-8,INF = 1e15;
    inline int read(){
    	int out = 0,flag = 1; char c = getchar();
    	while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
    	while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
    	return out * flag;
    }
    int n,m;
    double a[maxm][maxn];
    void Pivot(int l,int e){
    	double t = a[l][e]; a[l][e] = 1;
    	for (int j = 0; j <= n; j++) a[l][j] /= t;
    	for (int i = 0; i <= m; i++) if (i != l && fabs(a[i][e]) > eps){
    		t = a[i][e]; a[i][e] = 0;
    		for (int j = 0; j <= n; j++)
    			a[i][j] -= a[l][j] * t;
    	}
    }
    void init(){
    	while (true){
    		int l = 0,e = 0;
    		for (int i = 1; i <= m; i++) if (a[i][0] < -eps && (!l || (rand() & 1))) l = i;
    		if (!l) break;
    		for (int j = 1; j <= n; j++) if (a[l][j] < -eps && (!e || (rand() & 1))) e = j;
    		Pivot(l,e);
    	}
    }
    void simplex(){
    	while (true){
    		int l = 0,e = 0; double mn = INF;
    		for (int j = 1; j <= n; j++) if (a[0][j] > eps){e = j; break;}
    		if (!e) break;
    		for (int i = 1; i <= m; i++) if (a[i][e] > eps && a[i][0] / a[i][e] < mn)
    			mn= a[i][0] / a[i][e],l = i;
    		Pivot(l,e);
    	}
    }
    int main(){
    	srand(time(NULL));
    	n = read(); m = read(); int L,R;
    	REP(j,n) a[0][j] = -read();
    	REP(i,m){
    		L = read(); R = read();
    		for (int j = L; j <= R; j++)
    			a[i][j] = -1;
    		a[i][0] = -read();
    	}
    	init(); simplex();
    	printf("%lld
    ",(LL)(a[0][0] + 0.5));
    	return 0;
    }
    
    
  • 相关阅读:
    软工实践个人总结
    第02组 Beta版本演示
    第02组 Beta冲刺(5/5)
    第02组 Beta冲刺(4/5)
    第02组 Beta冲刺(3/5)
    第02组 Beta冲刺(2/5)
    第02组 Beta冲刺(1/5)
    第02组 Alpha事后诸葛亮
    第02组 Alpha冲刺(6/6)
    第02组 Alpha冲刺(5/6)
  • 原文地址:https://www.cnblogs.com/Mychael/p/9249404.html
Copyright © 2011-2022 走看看