POJ 1201 Intervals (differential constraint, shortest path)

Source: Internet
Author: User

Test instructions: Suppose there is a natural number interval [0,50000], to pick out some of the natural numbers out, but now do not know the whole range to pick out how many, only know part of the closed interval [A, a] at least how many, known to have n closed interval. How many do I have to pick out at least?

Ideas:

For the given interval cnt[b-a]>=k this can guarantee that the number of the interval is not less than K. But because the two sides are closed intervals, so to change cnt[b-(A-1)]>=k, representing the number of B to a. That is, the transformation into an equation is B (A-1) >=k, transform for (A-1)-b<=-k, to meet the common formula B-a<=k, you can build a side B point (A-1), the weight is-K.

But are there any other constraints? If we just build the map, that many points will not have a connection ah, such as [3,7]>=2 and [5,9]>=4, Jian Bian for 2->7 and 4>9, completely not on the edge Ah! There must be other connections, can there be edges between the two adjacent numbers? Yes, you can at least meet a A-(A-1) >=0, right? Indicates that a can only be selected and not selected. What's more? Each point can only be selected once? There is a A-(A-1) <=1.

A combination of 3 formulas can be deformed to get:

(1) (A-1)-b<=-k

(2) (A-1)-a<=0

(3) A-(A-1) <=1

All meet the common formula. Then we can build the side. There are at least two edges for each of the two adjacent numbers.

What are we asking for? Meet the requirements, the minimum number of the whole interval. Starting from the maximum number, the minimum number of points will usually get a negative weight path and cost[minimum]= negative value after the shortest circuit is obtained. and the cost[minimum] is the answer to the opposite number. Remember to set the cost[starting point to 0 before starting. Probably the idea is this, but the interval is [0,50000], so there will be a negative subscript at 0-1, you can shift the entire interval right one into [1,50001].

1 //#include <bits/stdc++.h>2#include <cstdio>3#include <cstring>4#include <map>5#include <iostream>6#include <deque>7#include <vector>8 #defineINF 0x7f7f7f7f9 #definePII pair<int,int>Ten #defineLL unsigned long Long One using namespacestd; A Const intn=51010; - structnode - { the     int  from, To,cost; - node () {}; -Nodeint  from,intTo,intCost): from( from), to and cost (cost) {}; -}edge[n*4]; +vector<int>Vect[n]; - intedge_cnt; +  A voidAdd_node (int  from,intTo,intCost ) at { -Edge[edge_cnt]=node ( from, to,cost); -vect[ from].push_back (edge_cnt++); - } -  - BOOLInq[n]; in intCost[n]; - intSPFA (intSmallintbig) to { +memset (INQ,0,sizeof(INQ)); -memset (Cost,0x7f,sizeof(cost)); thecost[big]=0; *deque<int> Que (1, big); $ Panax Notoginseng      while(!que.empty ()) -     { the         intx=Que.front (); Que.pop_front (); +inq[x]=0; A  the          for(intI=0; I<vect[x].size (); i++) +         { -Node e=Edge[vect[x][i]]; $             if(cost[e.to]>cost[x]+e.cost) $             { -cost[e.to]=cost[x]+E.cost; -                 if(!inq[e.to]) the                 { -inq[e.to]=1;Wuyi                     if(!que.empty () && cost[e.to]<Cost[que.front ()]) Que.push_front (e.to); the                     ElseQue.push_back (e.to); -                 } Wu             } -         } About     } $     return-Cost[small]; - } -  - intMain () A { +     //freopen ("Input.txt", "R", stdin); the     intN, M, A, B, C; -      while(~SCANF ("%d",&N)) $     { theEdge_cnt=0; theMemset (Edge,0,sizeof(Edge)); the          for(intI=0; i<n; i++) vect[i].clear (); the         intSmall=inf, big=0; -  in          for(intI=0; i<n; i++) the         { thescanf"%d%d%d",&a,&b,&c); AboutAdd_node (b +1, a,-c);//moved 1-bit right. the  theSmall=min (small, A +1); theBig=max (big, B +1); +         } -         //between each of the two points the          for(intI=small; i<=big; i++)Bayi         { theAdd_node (I-1I1); theAdd_node (i, I-1,0); -         } -COUT&LT;&LT;SPFA (small-1, big) <<Endl; the     } the     return 0; the}
AC Code

POJ 1201 Intervals (differential constraint, shortest path)

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.