ZOJ2770, fire connection, differential Constraint

Source: Internet
Author: User

The key to difference constraints is to understand why they can be converted into triangular inequalities.

In addition, for the inequality Xi-Xj <= c, it must be converted to an edge. .

The ZOJ2770 analysis is correct.

The specific analysis is not written, and the code = may be slightly messy.



# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include
        # Include
         
           Using namespace std; # define clr (x) memset (x, 0, sizeof (x) # define fp1 freopen ("in.txt", "r", stdin) # define fp2 freopen ("out.txt", "w", stdout) # define pb push_back # define INF 0x3c3c3c3ctypedef long LL; struct Edge {LL from, to, dist ;}; const int maxn = 1e6; struct BF {// The node number should start from 0. It should be possible not to start from 0 int n; vector
          
            Edges; vector
           
             G [maxn]; bool inq [maxn]; // queue optimization spfa determines whether edges have been added to LL d [maxn]; int p [maxn]; // The Last arc int is_neg [maxn] in the shortest path; // The number of times the queue enters is used to determine whether there is a negative weight loop void init () {for (int I = 0; I <= n; I ++) G [I]. clear (); edges. clear ();} void addedge (LL from, LL to, LL dist) // unidirectional edge operation {// printf ("% d ~ \ N ", from, to, dist); edges. push_back (Edge) {from, to, dist}); int len = edges. size (); G [from]. push_back (len-1);} int spfa (LL be) // bool can be returned to determine the negative weight ring {memset (inq, 0, sizeof (inq); memset (p, 0, sizeof (p); memset (is_neg, 0, sizeof (is_neg); // The comment is used to judge the negative ring queue.
            
              Q; for (int I = 0; I <= n; I ++) d [I] = INF; d [be] = 0; Q. push (be); inq [be] = true; while (! Q. empty () // non-empty ~ {Int u = Q. front (); Q. pop (); inq [u] = false; for (int I = 0; I <G [u]. size (); I ++) {Edge & e = edges [G [u] [I]; if (d [e. to]> d [u] + e. dist) {// the value you want to input has been converted to e. dist d [e. to] = d [u] + e. dist; p [e. to] = u; // G [u] [I] stores the information of an edge, which does not exist and is directly changed to u if (! Inq [e. to]) {Q. push (e. to); inq [e. to] = true; if (++ is_neg [e. to]> n) return false;} // else ...}} return true ;}} test; LL sold [1000 + 10]; LL sum [1000 + 10]; int main () {// fp1; LL n, m,, b, c; while (scanf ("% lld", & n, & m) = 2) {test. n = n; test. init (); clr (sold); clr (sum); for (int I = 1; I <= n; I ++) {scanf ("% lld ", & sold [I]); sum [I] = sum [I-1] + sold [I]; test. addedge (I, I-1, 0); // Si-Si-1> 0 per barracks Cannot be less than 0 test. addedge (I-1, I, sold [I]); // up to sold [I];} // puts ("-----------------"); for (int I = 1; I <= m; I ++) {scanf ("% lld", & a, & B, & c); test. addedge (B, A-1,-c); // a --- B at least c People // test. addedge (A-1, B, sum [B]-sum [A-1]); // a --- B up to x people} if (! Test. spfa (n) {puts ("Bad Estimations"); // printf ("% d \ n", test. d [0]);} else printf ("% d \ n",-test. d [0]); // test. n = 3; // test. addedge (1, 2, 5); // test. addedge (2, 3, 6); // if (! Test. spfa (1) puts ("hehe"); // else printf ("% d \ n", test. d [1], test. d [2], test. d [3]);} return 0 ;}
            
           
          
         
       
      
     
    
   
  
 


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.