6424 Russian Dolls
Maybe you know the famous russian souvenir Russian Dolls. It looks like a set of nested wooden dolls.
A doll with a smaller size is placed inside a bigger one. Let’s consider all dolls taken apart. Each
doll has an outer volume outi which is the volume it occupies in space and an inner volume ini — the
volume of the empty space inside the doll. You may assume that you can put one doll inside another
if the outer volume of the first doll is strictly less than the inner volume of the second one. If two or
more dolls are inside another one they can’t lie one near the other, they must be nested.
For each doll the cost of unit of empty space — costi
is known. You must pay exactly costi for each
unit of empty space which directly belongs to the i-th doll (but not to ones inside it). You may arrange
the dolls the way you want as long as you are not contradicting the rules. The objective is to find an
arrangement of nesting the dolls (not necessarily all of them) such that the overall cost you have to pay
is minimized.
Input
The input file contains several test cases, each of them as described below.
First line contains an integer N (1 ≤ N ≤ 1000) which is the number of dolls you have. The i-th
of the next N lines contains three integers outi
, ini
, costi (1 ≤ ini < outi ≤ 1000, 1 ≤ costi ≤ 1000),
which are the outer volume, inner volume and the empty space cost of the i-th doll.
Sample Output
For each test case, write to the output a single integer P which is the minimum possible cost you should
pay on a line by itself.
Sample Input
3
5 4 1
4 2 2
3 2 1
Sample Output
7
/*
题解:总花费的公式可以写成:sum(cost(i)*(in(i)-out( i ')) ,1<=i<=n )。out(i')为放在第i个玩偶内部的玩偶的体积,如果 i!=j ,那么i'!=j' 。
我们还可以把公式展开成这种形式:sum(cost(i)*in(i))-sum(cost(i)*in(i')) 。前面一部分为定值,实际上我们要让后面一部分sum(cost(i)*in(i'))最大。容易想到这是个带权匹配问题,可以用费用流解决,但是复杂度显然太高。对于第i个玩偶来说,我们要让cost(i)*in(i') 最大的话,一定是贪心找满足条件的最大的 in(i‘) ,设i'=x。但是目前只是让第i个玩偶的值最大,有可能存在一个玩偶j,x能放在j里面,并且cost(j)>cost(i),那么cost(j)*in(x)>cost(i)*in(x),把x放在j更优。所以若cost(i)是所有玩偶中最大的,那么前面的贪心策略就一定是正确的。
*/
#include <stdio.h>
#include <string>
#include <cstring>
#include <queue>
#include <algorithm>
#include <functional>
#include <vector>
#include <sstream>
#include <iomanip>
#include <math.h>
#include <iostream>
#include <sstream>
#include <time.h>
#include <stack>
#include <set>
#include <map>
#include <time.h>
#include <bitset>
using namespace std;
const int MAX=1005;
struct Doll
{
int out,in,cost;
bool operator < (const Doll & a) const
{
if (cost==a.cost)
return in>a.in;
return cost>a.cost;
}
};
Doll Dolls[MAX];
int N;
bool used[MAX];
int main()
{
cin.sync_with_stdio(false);
while (cin>>N)
{
for (int i=0;i<N;i++)
cin>>Dolls[i].out>>Dolls[i].in>>Dolls[i].cost;
sort(Dolls,Dolls+N);
memset(used,false,sizeof(used));
int Ans=0;
for (int i=0;i<N;i++)
{
int id=-1,mx=0;
for (int j=0;j<N;j++)
if (Dolls[j].out<Dolls[i].in&&!used[j]&&Dolls[j].out>mx)
mx=Dolls[j].out,id=j;
if (id!=-1)
used[id]=true;
Ans+=(Dolls[i].in-Dolls[id].out)*Dolls[i].cost;
}
cout<<Ans<<endl;
}
return 0;
}