1. 程式人生 > >101908F Music Festival —— dp,二進位制列舉

101908F Music Festival —— dp,二進位制列舉

Music festivals should be fun, but some of them become so big that cause headache for the goers. The problem is that there are so many good attractions playing in so many stages that the simple task of choosing which shows to watch becomes too complex.

To help goers of such festivals, Fulano decided to create an app that, after evaluating the songs heard on the users’ favorite streaming services, suggests which shows to watch so that there is no other combination of shows that is better according to the criteria described below:

To enjoy the experience as much as possible it is important to watch each of the selected shows from start to end; Going to the festival and not seeing one of the stages is out of the question; To ensure that the selection of artists is compatible with the user, the app counts how many songs from each artist the user had previously listened to on streaming services. The total number of known songs from chosen artists should be the largest possible. Unfortunately the beta version of app received criticism, because users were able to think of better selections than those suggested. Your task in this problem is to help Fulano and write a program that, given the descriptions of the shows happening in each stage, calculates the ideal list of artists to the user.

The displacement time between the stages is ignored; therefore, as long as there is no intersection between the time ranges of any two chosen shows, it is considered that it is possible to watch both of them. In particular, if a show ends exactly when another one begins, you can watch both of them.

Input The first line contains an integer 1≤N≤10 representing the number of stages. The following N lines describe the shows happening in each stage. The i-th of which consists of an integer Mi≥1, representing the number of shows scheduled for the i-th stage followed by Mi show descriptions. Each show description contains 3 integers ij,fj,oj (1≤ij<fj≤86400; 1≤oj≤1000), representing respectively the start and end time of the show and the number of songs of the singer performing that were previously heard by the user. The sum of the Mi shall not exceed 1000.

Output Your program must produce a single line with an integer representing the total number of songs previously heard from the chosen artist, or −1 if there is no valid solution.

Examples Input 3 4 1 10 100 20 30 90 40 50 95 80 100 90 1 40 50 13 2 9 29 231 30 40 525 Output 859 Input 3 2 13 17 99 18 19 99 2 13 14 99 15 20 99 2 13 15 99 18 20 99 Output -1

題意:

有n個舞臺,每個舞臺有m個劇,每個劇有起始結束時間和觀看人數,找出一種方法使得每個舞臺都有人看並且觀看人數最多,不存在則輸出-1

題解:

列舉開始時間,往後轉移,因為只有10個數,所以可以用二進位制列舉那些舞臺,首先把dp陣列賦成-1,因為可能有狀態並不存在,比如有第一個舞臺的m=0,那111就不可能從110轉過來。之後就是揹包一樣的東西。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll dp[86405][(1<<10)+5];
struct node
{
    int fin,num;
    ll val;
    node(){}
    node(int fin,int num,ll val):fin(fin),num(num),val(val){}
};
vector<node>vec[86405];
int main()
{
    memset(dp,-1,sizeof(dp));
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        int t;
        scanf("%d",&t);
        int sta,fin;
        ll val;
        while(t--)
        scanf("%d%d%lld",&sta,&fin,&val),vec[sta].push_back(node(fin,i,val));
    }
    int t=1<<n;
    dp[0][0]=0;
    for(int i=0;i<=86400;i++)
    {
        for(int j=0;j<t;j++)
            dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
        for(int j=0;j<vec[i].size();j++)
            for(int k=0;k<t;k++)
                if(dp[i][k]!=-1)
                    dp[vec[i][j].fin][k|(1<<vec[i][j].num)]=max(dp[vec[i][j].fin][k|(1<<vec[i][j].num)],dp[i][k]+vec[i][j].val);
    }
    printf("%lld\n",dp[86400][t-1]);
    return 0;
}