Hdu3666 the matrix problem --- difference Constraint

Source: Internet
Author: User

It would be crazy if I had to get killed from re to TLE To wa ..


This question is not as straightforward as creating a graph. I cannot think of how to create a graph through the given inequality relationship.

Therefore, we need to simplify the conditions given by the question to a certain extent, and take the logarithm of both sides of the inequality to simplify the form of SA-Sb <= C.

Note that W is of the double type.

Second, we will add pruning based on experience:

1. Number of teams> SQRT (n) indicates a negative ring.

2. Count the total number of queues. If the number is greater than 2n, a negative ring is determined.

Generally, this is not used because it is not rigorous.


The two spfa below are both correct. The handwriting queue is a little faster, and the second pruning effect is obvious.

# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <queue> using namespace STD; struct node {int V; double W; int next;} e [360010]; int n, m, H, head [810], INQ [810], outq [810], Q [50000]; double D [805]; void addedge (int A, int B, double C) {e [H]. V = B; E [H]. W = C; E [H]. next = head [a]; head [a] = H ++;} bool spfa (INT s) {int IQ, I, top, K; for (I = 0; I <= N; I ++) d [I] = 1000000000; memset (INQ, 0, sizeof INQ); memse T (outq, 0, sizeof outq); D [s] = 0; INQ [s] = 1; IQ = 0; I = 0; Q [IQ ++] = s; while (I! = IQ) {Top = Q [I]; INQ [Top] = 0; outq [Top] ++; If (outq [Top]> (INT) SQRT (N * 1.0) return 0; k = head [Top]; while (k> = 0) {If (d [E [K]. v]-E [K]. w> d [Top]) {d [E [K]. v] = E [K]. W + d [Top]; If (! INQ [E [K]. v]) {INQ [E [K]. v] = 1; Q [IQ ++] = E [K]. V ;}} K = E [K]. next;} I ++;} return 1;} int spfa (INT st) // adjacent table STL {for (INT I = 0; I <= N; I ++) d [I] = 100000000; memset (INQ, 0, sizeof INQ); memset (outq, 0, sizeof outq); D [st] = 0; INQ [st] = 1; queue <int> q; q. push (ST); int CNT = 1; while (! Q. empty () {int x = Q. front (); q. pop (); INQ [x] = 0; outq [x] ++; If (outq [x]> SQRT (N * 1.0) + 10) return 0; for (INT I = head [X]; I! =-1; I = E [I]. next) {If (d [E [I]. v]> d [x] + E [I]. w) {d [E [I]. v] = d [x] + E [I]. w; If (! INQ [E [I]. v]) {CNT ++; If (CNT> (n + M) * 2) return 0; INQ [E [I]. v] = 1; q. push (E [I]. v) ;}}}return 1 ;}int main () {Double X, ll, UU, L, U; int I, j; while (~ Scanf ("% d % lf", & N, & M, & L, & U) {memset (Head,-1, sizeof head ); H = 0; LL = Log (l); UU = Log (U); for (I = 0; I <n; I ++) for (j = 0; j <m; j ++) {scanf ("% lf", & X); addedge (I, j + N, log (x)-LL ); addedge (J + N, I, UU-log (x);} n + = m; m + = m; If (spfa (0 )) printf ("Yes \ n"); else printf ("NO \ n");} 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.