HDOJ 3666 the matrix problem difference Constraint
There is a multiplication and division relationship based on the meaning of the question. To facilitate the diagram, use the logarithm to convert the multiplication and division relationship to the addition and subtraction relationship .....
THE MATRIX PROBLEM
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 7486 Accepted Submission (s): 1914
Problem Description You have been given a matrix CN * M, each element E of CN * M is positive and no more than 1000, The problem is that if there exist N numbers a1, a2,... An and M numbers b1, b2 ,..., Bm, which satisfies that each elements in row-I multiplied with ai and each elements in column-j divided by bj, after this operation every element in this matrix is between L and U, L indicates the lowerbound and U indicates the upperbound of these elements.
Input There are several test cases. You shoshould process to the end of file.
Each case when des two parts, in part 1, there are four integers in one line, N, M, L, U, indicating the matrix has N rows and M columns, L is the lowerbound and U is the upperbound (1 <= N, M <= 10000, 1 <= L <= U <= ). in part 2, there are N lines, each line between des M integers, and they are the elements of the matrix.
Output If there is a solution print YES, else print NO.
Sample Input
3 3 1 62 3 48 2 65 2 9
Sample Output
YES
Source 2010 Asia Regional Harbin
/*************************************** * ******** Author: CKbossCreated Time: October 16, Wednesday, July 29, 2015 File Name: HDOJ3666.cpp *************************************** * *********/# include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Using namespace std; const int maxn = 1000; const double eps = 1e-8; int n, m; double L, R; struct Edge {int to, next; double cost ;} edge [maxn * 2]; int Adj [maxn], Size; void init () {memset (Adj,-1, sizeof (Adj); Size = 0 ;} void Add_Edge (int u, int v, double c) {edge [Size]. to = v; edge [Size]. next = Adj [u]; edge [Size]. cost = c; Adj [u] = Size ++;} double dist [maxn]; int cQ [maxn]; bool inQ [maxn]; bool spfa (int rt) {for (int I = 0; I
Q; inQ [rt] = true; q. push (rt); cQ [rt] = 1; while (! Q. empty () {int u = q. front (); q. pop (); for (int I = Adj [u]; ~ I; I = edge [I]. next) {int v = edge [I]. to; if (dist [v]> dist [u] + edge [I]. cost) {dist [v] = dist [u] + edge [I]. cost; if (! InQ [v]) {inQ [v] = true; cQ [v] ++; if (cQ [v]> = sqrt (n + m) return false; q. push (v) ;}} inQ [u] = false;} return true;} int main () {// freopen(in.txt, r, stdin); // freopen(out.txt, w, stdout); while (scanf (% d % lf, & n, & m, & L, & R )! = EOF) {init (); for (int I = 1; I <= n; I ++) {for (int j = 1; j <= m; j ++) {double x; scanf (% lf, & x); int a = I, B = n + j; Add_Edge (B,, log (R/x); Add_Edge (a, B,-log (L/x) ;}} for (int I = 1; I <= n + m; I ++) Add_Edge (0, I, 0); bool fg = spfa (0); if (fg) puts (YES); else puts (NO );} return 0 ;}