hdu 4833 离散化+dp ****

时间:2023-12-28 12:20:32

先离散化,然后逆着dp,求出每个点能取到的最大利益,然后看有没有钱,有钱就投

想法好复杂

 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
struct NN1
{
int d,e;
void input()
{
scanf("%d%d",&d,&e);
}
}node1[];
struct NN2
{
int start,finish;
int r;
void input()
{
scanf("%d%d%d",&start,&finish,&r);
}
}node2[];
int a[];
long long f[];
long long f2[];
int dp[]; vector<int>vec[];
vector<int>vec2[];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
int iCase = ;
int n,m;
scanf("%d",&T);
while(T--)
{
iCase++;
printf("Case #%d:\n",iCase);
scanf("%d%d",&n,&m);
int cnt = ;
memset(f,,sizeof(f));
for(int i = ;i < n;i++)
{
node1[i].input();
//a[cnt++] = node1[i].d;
f[node1[i].d] += node1[i].e;
}
for(int i = ;i <= ;i++)
f[i] += f[i-];
for(int i = ;i < m;i++)
{
node2[i].input();
a[cnt++] = node2[i].start;
a[cnt++] = node2[i].finish;
}
sort(a,a+cnt);
cnt = unique(a,a+cnt) - a;
map<int,int>mp;
for(int i = ;i < cnt;i++)
mp[a[i]] = i;
f2[] = f[a[]];
for(int i = ;i < cnt;i++)
f2[i] = f[a[i]] - f[a[i-]];
for(int i = ;i < cnt;i++)
{
vec[i].clear();
vec2[i].clear();
}
for(int i = ;i < m;i++)
{
node2[i].start = mp[node2[i].start];
node2[i].finish = mp[node2[i].finish];
vec[node2[i].start].push_back(node2[i].finish);
vec2[node2[i].start].push_back(node2[i].r);
}
memset(dp,,sizeof(dp));
for(int i = cnt-;i >= ;i--)
{
dp[i] = dp[i+];
int sz = vec[i].size();
for(int j = ;j < sz;j++)
dp[i] = max(dp[i],dp[vec[i][j]] + vec2[i][j]);
}
long long ans ;
//minCostMaxflow(cnt,cnt+1,ans);
ans = ;
for(int i = ;i < cnt;i++)
{
ans += (long long)dp[i]*f2[i];
}
printf("%.2lf\n",(double)ans/); }
return ;
}