HDU 3666 (differential constraint, manual stack resolution timeout problem)

Source: Internet
Author: User

The MATRIX problem

Time limit:4000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 8016 Accepted Submission (s): 2092


Problem Descriptionyou has been given a matrix cn*m, each element E of cn*m are positive and no more than, the proble M is that if there exist N numbers a1, A2, ... an and M numbers B1, B2, ..., BM, which satisfies, each elements in Row-i m Ultiplied with AI and all elements in Column-j divided by BJ, after this operation every element in the this matrix is Betwee n L and U, L indicates the lowerbound and U indicates the upperbound of these elements.

Inputthere is several test cases. You should process to the end of file.
Each case includes-parts, in Part 1, there is four integers on one line, N,m,l,u, indicating the matrix have N rows an d M Columns, L is the lowerbound and U are the Upperbound (1<=n, m<=400,1<=l<=u<=10000). In Part 2, there is N lines, each line includes M integers, and they is the elements of the matrix.

Outputif There is a solution print "YES", else print "NO".

Sample INPUT3 3 1 62 3 48 2 65 2 9

Sample Outputyes

Source Regional Harbin Test instructions: gives a matrix C asking if there are such two sequences so that L <= CIJ * AI/BJ <= R to this matrix is established. The problem: Cij the past, and then take a logarithm, log (L/CIJ) <= log (AI)-log (BJ) <= log (R/CIJ) so that it can be converted to a differential constraint, but the question is that the ring will time out, It is suggested that you can only judge sqrt (n) times, but it is not possible to prove that the better way to do this is to use a manual stack instead of a queue. That way you can quickly get a ring and give two code. Use only the sqrt (n) queue notation:
#include <iostream>#include<cstdio>#include<cstring>#include<queue>#include<math.h>using namespacestd;Const DoubleINF =999999999;Const intN =805;Const intM =800000;structedge{intV,next; DoubleW;} EDGE[M];intHead[n],tot;intN,m,l,r;voidinit () {memset (head,-1,sizeof(head)); Tot=0;}voidAddedge (intUintVDoubleWint&k) {EDGE[K].V=v, EDGE[K].W = w, Edge[k].next = head[u],head[u] = k++;}DoubleLow[n];intTime[n];BOOLVis[n];BOOLSPFA (ints) {     for(intI=0; i<=n+m;i++) {Vis[i]=false; Time[i]=0; Low[i]=INF; }    intnum = ((int) sqrt (n+m)) +1; Low[s]=0; Time[s]++; Queue<int>Q;    Q.push (s);  while(!Q.empty ()) {        intU =Q.front ();        Q.pop (); Vis[u]=false;  for(intk=head[u];k!=-1; k=Edge[k].next) {            intv =edge[k].v; DoubleW =EDGE[K].W; if(low[v]>low[u]+W) {Low[v]= low[u]+W; if(!Vis[v]) {Vis[v]=true;                    Q.push (v); TIME[V]++; if(Time[v]>num)return false; }            }        }    }    return true;}intMain () {DoubleC;  while(SCANF ("%d%d%d%d", &n,&m,&l,&r)! =EOF)        {init ();  for(intI=1; i<=n;i++){             for(intj=1; j<=m;j++) {scanf ("%LF",&c); Addedge (I,n+j,-log (l/c), tot); Addedge (n+j,i,log (r/c), tot); }        }         for(intI=1; i<=n+m;i++) {Addedge (0I0, tot); }        if(SPFA (0)) printf ("yes\n"); Elseprintf"no\n"); }    return 0;}

Manual Stack Resolution:

#include <iostream>#include<cstdio>#include<cstring>#include<queue>#include<math.h>using namespacestd;Const DoubleINF =999999999;Const intN =805;Const intM =800000;structedge{intV,next; DoubleW;} EDGE[M];intHead[n],tot;intN,m,l,r;voidinit () {memset (head,-1,sizeof(head)); Tot=0;}voidAddedge (intUintVDoubleWint&k) {EDGE[K].V=v, EDGE[K].W = w, Edge[k].next = head[u],head[u] = k++;}DoubleLow[n];intTime[n];BOOLVis[n];intstk[n*N];BOOLSPFA (ints) {     for(intI=0; i<=n+m;i++) {Vis[i]=false; Time[i]=0; Low[i]=INF; }    inttop =0; Low[s]=0; Time[s]++; Stk[top++] =s;  while(top!=0){        intU = stk[--top]; Vis[u]=false;  for(intk=head[u];k!=-1; k=Edge[k].next) {            intv =edge[k].v; DoubleW =EDGE[K].W; if(low[v]>low[u]+W) {Low[v]= low[u]+W; if(!Vis[v]) {Vis[v]=true; Stk[top++] =v; TIME[V]++; if(time[v]>n+m)return false; }            }        }    }    return true;}intMain () {DoubleC;  while(SCANF ("%d%d%d%d", &n,&m,&l,&r)! =EOF)        {init ();  for(intI=1; i<=n;i++){             for(intj=1; j<=m;j++) {scanf ("%LF",&c); Addedge (I,n+j,-log (l/c), tot); Addedge (n+j,i,log (r/c), tot); }        }         for(intI=1; i<=n+m;i++) {Addedge (0I0, tot); }        if(SPFA (0)) printf ("yes\n"); Elseprintf"no\n"); }    return 0;}

HDU 3666 (differential constraint, manual stack resolution timeout problem)

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.