关闭

【DP】 (ASC 44) Braess's Paradox

标签: DP
344人阅读 评论(0) 收藏 举报
分类:

DP。。先预处理i到j的距离,可以用前缀和列出两个方程。。。然后n^2DP可以解出最大最小值。。

#include <iostream>
#include <queue> 
#include <stack> 
#include <map> 
#include <set> 
#include <bitset> 
#include <cstdio> 
#include <algorithm> 
#include <cstring> 
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 5005
#define maxm 400005
#define eps 1e-10
#define mod 1000000007
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid 
#define rson o<<1 | 1, mid+1, R
#define pii pair<int, int>
#pragma comment(linker, "/STACK:16777216")
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
// head

double dp[2][maxn];
double a[maxn], b[maxn], c[maxn], d[maxn];
int n;

double calc(int t1, int t2)
{
	t1--;
	double aa = a[t2] - a[t1];
	double bb = b[t2] - b[t1];
	double cc = c[t2] - c[t1];
	double dd = d[t2] - d[t1];
	if(fabs(aa + cc) < eps) {
		return min(bb, dd);
	}
	double x = (cc + dd - bb) / (aa + cc);
	if(x > 1) return aa + bb;
	else if(x < 0) return cc + dd;
	else return aa * x + bb;
}

void work()
{
	for(int i = 1; i <= n; i++) {
		scanf("%lf%lf%lf%lf", &a[i], &b[i], &c[i], &d[i]);
		a[i] += a[i-1];
		b[i] += b[i-1];
		c[i] += c[i-1];
		d[i] += d[i-1];
	}
	printf("%.11f\n", calc(1, n));
	double ans = 0;
	for(int i = 1; i <= n; i++) {
		ans += calc(i, i);
	}
	printf("%.11f\n", ans);
	for(int i = 1; i <= n; i++) dp[0][i] = 1e16, dp[1][i] = 0;
	dp[0][0] = dp[1][0] = 0;
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= i; j++) {
			dp[0][i] = min(dp[0][i], dp[0][j-1] + calc(j, i));
			dp[1][i] = max(dp[1][i], dp[1][j-1] + calc(j, i));
		}
	}
	printf("%.11f\n", dp[0][n]);
	printf("%.11f\n", dp[1][n]);

}

int main()
{
	freopen("braess.in", "r", stdin);
	freopen("braess.out", "w", stdout);
	while(scanf("%d", &n)!=EOF) {
		work();
	}
	
	return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:203257次
    • 积分:7560
    • 等级:
    • 排名:第3021名
    • 原创:587篇
    • 转载:9篇
    • 译文:0篇
    • 评论:17条
    最新评论