Uvalive 5532 King (differential constraint, SPFA)

Source: Internet
Author: User

Test instructions: Suppose that a sequence s has n elements and now has a bunch of constraints that constrain the sum of some contiguous subsequence, with symbols > and <, respectively. Does the question sequence s exist? (Look at test instructions for half an hour!) )

Note the given form is (A,B,C,D), indicating: the sum of the intervals: Sum[a,a+b]<d or sum[a,a+b]>d. And C is a two-character composition, judging the first 1 characters is sufficient.

Ideas:

First consider the point to indicate what, you can see the given is the interval, that is, the first and the end of the position, can make sum (a) to denote sequence a[1...a] and, then the expression is probably sum (a+b)-sum (A-1) <k, due to the existence of less than the number, the number of less than equals, So the expression changes to sum (a+b)-sum (A-1) <=k-1 on the line. The same is true of the >. The M limit given is converted to a side.

However, it seems that after the completion of the diagram, there are some pieces of the total no contact AH (ie, not connected)? For example a[1...7] There is a limit, a[4...9] There is a limit, but these 4 points is two gang! There is no relationship, ha, if there is no intersection, there will be no conflict, such as SUM (a[1...7]) is not associated with sum (a[4...9]), because their intersection of SUM (a[4...7]) size regardless of value, As long as the other part of the appropriate value (can be negative), will not conflict, such as SUM (a[4....7]) = 10086, and sum (a[1...7]) = 0, then sum (a[1...3]) =-10086. It can also be said that the sub-range as long as there is a number is not at the same time limited, there will be no conflict in any case.

Again, for example, a[1...7] and a[4...9] and a[1...9] These 3 restrictions are finally connected! Draw yourself, it's too hard to explain. They are still unable to cause conflict.

1#include <bits/stdc++.h>2 #defineINF 0x7f7f7f7f3 #definePII pair<int,int>4 #defineLL unsigned long Long5 using namespacestd;6 Const intn= Max;7 structnode8 {9     int  from, to, cost;Ten node () {}; OneNodeint  from,intTo,intCost): from( from), to and cost (cost) {}; A}edge[n*n*2]; -vector<int>Vect[n]; - intedge_cnt; the  - voidAdd_node (int  from,intTo,intCost ) - { -Edge[edge_cnt]=node ( from, to,cost); +vect[ from].push_back (edge_cnt++); - } +  A Set<int>sett; at intCost[n], cnt[n]; - BOOLInq[n]; - intSPFA (intUp ) - { -memset (INQ,1,sizeof(INQ)); -memset (CNT,0,sizeof(CNT)); inmemset (Cost,0,sizeof(cost)); -deque<int>que; to      for(Set<int>::iterator It=sett.begin (); It!=sett.end (); it++) Que.push_back (*it);//All in!  +  -      while(!que.empty ()) the     { *         intx=Que.front (); Que.pop_front (); $inq[x]=0;Panax Notoginseng          for(intI=0; I<vect[x].size (); i++) -         { theNode e=Edge[vect[x][i]]; +             if(cost[e.to]>cost[x]+e.cost) A             { thecost[e.to]=cost[x]+E.cost; +                 if(!inq[e.to]) -                 { $inq[e.to]=1; $                     if(++cnt[e.to]>up)return false; -                     if(!que.empty () &&cost[e.to]<cost[que.front ()])//Optimized - Que.push_front (e.to); the                     Else - Que.push_back (e.to);Wuyi                 } the             } -         } Wu     } -     return true; About } $  - intMain () - { -Freopen ("Input.txt","R", stdin); A     intN, M, a, B, D, L, U; +     Charc[Ten]; the      while(SCANF ("%d", &N), N) -     { $ sett.clear (); theEdge_cnt=0; theMemset (Edge,0,sizeof(Edge)); the          for(intI=0; i<=n; i++) vect[i].clear (); the  -scanf"%d",&m); in          for(intI=0; i<m; i++) the         { thescanf"%d%d%s%d", &a, &b, &c, &d); AboutSett.insert (A-1); theSett.insert (A +b); the             if(c[0]=='g')//Greater than the             { +Add_node (A+b, A-1,-(d+1)); -             } the             ElseBayi             { theAdd_node (A-1, A+b, D-1); the             } -         } -         if(SPFA (Sett.size ())) puts ("Lamentable Kingdom"); the         ElsePuts"Successful Conspiracy"); the     } the     return 0; the}
AC Code

Uvalive 5532 King (differential constraint, SPFA)

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.