"Creative Cup" Shandong University of Science and Technology, the nineth Session of the ACM Program Design Competition D. Crazy Blue Thinking the shortest path on +dag map and its development

Source: Internet
Author: User

Crazy Blue. Time limit:1000ms Memory limit:65536kb Submit statistic Problem Description

It is known that the mysterious ACM Laboratory has an epic-level title monster, named Yue--blue. Blue has been showing an evil smile recently, and it turns out that Blue has a list for the school match.

From the beginning of the school game there are N hours, due to a variety of strange reasons for the topic can be set the time is not fixed, roughly can be divided into m time period. The number of topics that can be used for each time period may vary. At the same time because the issue is a hard work, so every time the end of the topic, the kind of blue will let everyone rest R (1≤r≤n) hours, in order to continue to fight for the next topic.

In order to prepare as many questions as possible for the school match, Blue needs to plan how the N-hour arrangement is, and, of course, as the only elder, Blue is planning the time period of how to arrange the topic.

Now BLue wants to test you in his perfect arrangement under the question group can give up how many questions. Input

Test data has multiple groups, input until end of file

The first line enters three number N (1≤n≤1,000,000), M (1≤m≤1,000), R (1≤r≤n).

Next there is the M line input, each line input three number Si (0≤si < N), Ei (Si < ei≤n), Vi (1≤vi≤1,000,000), (0 < I <= M), respectively, as the beginning of the first time period , the end time of paragraph I, the number of topics that can be given in the first time period. Output

The maximum number of questions that can be produced for each set of data output. Example Input

5 3
1 4 5 6 9 4 3 5 2 7 8 11 15 2
Example Output
13
Hint

Assuming that the group has finished a period of time in the 5th hour, they need to rest for 3 hours (R = 3), then they can continue to start the question in the 8th hour. Author Ninaye

Ideas:


If a time period "l,r" is working, and then work we need r time to rest, because the time is increasing will not look back, so we find a way here if you can build a reasonable model diagram, it must be a DAG diagram.

At this point we may wish to change the interval "l,r" into "l,r+r", that is to build the Edge (L,R+R,W);

Then the next interval can be followed by the end time of the interval.

Direct map run topology sorting to solve is to tle, (since want to card tle why write 1e6 points so that I do not need to disperse the Meng ... Not happy ... )

So we're sort of discretization of three points in each interval. (L,R,R+R);

Then we have a new label for all points at this point.

then establish (i,i+1,0) the edge can be. So that time can be pushed down.


Finally, after the whole building up, run a dag on the longest road can be.


Also note that when you build the map, do not build out of the ring, or the topology sort run.


AC Code:

#include <stdio.h> #include <queue> #include <iostream> #include <algorithm> #include <
String.h> #include <map> using namespace std; struct Node2 {int x,y,w;}
A[15000]; struct node {int from,to,w,next;}
E[6000000];
map<int, int >s;
int n,m,r;
int cont,cnt;
int head[20500];
int dist[20500];
int degree[20000];
int b[20000];
    void Add (int from,int to,int W) {if (from==to) return;
    degree[to]++;
    E[cont].to=to;
    E[cont].w=w;
    E[cont].next=head[from];
head[from]=cont++;
    } void Slove () {Queue<int >q;
    memset (dist,0,sizeof (Dist));
    for (int i=1;i<=cnt;i++) {if (degree[i]==0) Q.push (i);
        while (!q.empty ()) {int U=q.front ();
        Q.pop ();
            for (int i=head[u];i!=-1;i=e[i].next) {int v=e[i].to;
            int W=E[I].W;
            if (dist[v]<dist[u]+w) {dist[v]=dist[u]+w;
          } degree[v]--;  if (degree[v]==0) Q.push (v);
    } int output=0;
    for (int i=1;i<=cnt;i++) Output=max (Output,dist[i]);
printf ("%d\n", output);
        int main () {while (~scanf ("%d%d%d", &n,&m,&r)) {int tot=0;
        cnt=0;
        cont=0;
        S.clear ();
        memset (degree,0,sizeof (degree));
        Memset (head) (head,-1,sizeof);
            for (int i=0;i<m;i++) {scanf ("%d%d%d", &AMP;A[I].X,&AMP;A[I].Y,&AMP;A[I].W);
            b[tot++]=a[i].x;
            B[TOT++]=A[I].Y;
        B[tot++]=a[i].y+r;

        Sort (b,b+tot);
        for (int i=0;i<tot;i++) {s[b[i]]=++cnt;
        for (int i=1;i<cnt;i++) {Add (i,i+1,0);
        for (int i=0;i<m;i++) Add (S[A[I].X],S[A[I].Y+R],A[I].W);
    Slove (); }
}







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.