Bzoj 1672 usaco 2005 Dec cleaning shifts dynamic plan for clearing Cowshed

Source: Internet
Author: User

Some cows need to be cleaned. There are n (n <= 10000) cows willing to clean, from time s to time t, it takes some money. Ask how much it will cost if you want to clean cows all the time.


Idea: The data volume of 1 W is not very large. In addition, the time limit is 5 s, so we will return with N ^ 2.

Sort the ox by the start of the time period.

Set f [I] to the minimum time spent on cleaning the first ox.

Then f [I] = min (F [I], F [J] + cost [I]) (F [I]. st <= f [J]. ED + 1)

The last is the initial value and the answer. As the question says that there are cows cleaning every moment, the initial value of F is the maximum value. The initial value of the ox started before the requirement is the cost of the ox.

Update the answer at the time of return. If the ox is still working at the end of the request, use the ox to update the answer. Finally, output the answer.


Code:


#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define MAX 10010#define INF 0x3f3f3f3fusing namespace std;struct Complex{    int x,y;    int cost;    bool operator <(const Complex &a)const {        if(x != a.x)            return x < a.x;        return y < a.y;    }    void Read() {        scanf("%d%d%d",&x,&y,&cost);        x++,y++;    }}src[MAX];int cnt,st,ed;int f[MAX];int main(){    cin >> cnt >> st >> ed;    st++,ed++;    for(int i = 1;i <= cnt; ++i)        src[i].Read();    sort(src + 1,src + cnt + 1);    memset(f,0x3f,sizeof(f));    int ans = INF;    for(int i = 1;i <= cnt; ++i)        if(src[i].x <= st)            f[i] = src[i].cost;    for(int i = 1;i <= cnt; ++i)        for(int j = 0;j < i; ++j)            if(src[i].x <= src[j].y + 1) {                f[i] = min(f[i],f[j] + src[i].cost);                if(src[i].y >= ed)  ans = min(ans,f[i]);            }    if(ans == INF)  ans = -1;    cout << ans << endl;    return 0;}


Bzoj 1672 usaco 2005 Dec cleaning shifts dynamic plan for clearing Cowshed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.