THE MATRIX PROBLEM
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6380 Accepted Submission(s): 1633
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 should process to the end of file.
Each case includes 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<=400,1<=L<=U<=10000). In part 2, there are N lines, each line includes M integers, and they are the elements of the matrix.
Each case includes 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<=400,1<=L<=U<=10000). In part 2, there are N lines, each line includes 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 6
2 3 4
8 2 6
5 2 9
Sample Output
YES
Source
Recommend
1 //1671MS 8128K 1290 B G++ 2 /* 3 4 题意: 5 给出一个n*m的矩阵,问是否存在ai、bj,使 6 l<=g[i][j](ai/bj)<=u 7 8 差分约束: 9 看到这种形式的题目应该首先想到差分约束了。 10 这里有个小变形 11 log(l')<=log(ai)-log(bj)<=log(u') 12 l'=l/(g[i][j]),u'=u/(g[i][j]) 13 这样,我们就可以的到所要的不等式组 14 注意建图时总点数为 n+m 15 注意这里判断负环只要循环超过sqrt(n+m)次即可跳出,不然会超时 16 17 */ 18 #include<stdio.h> 19 #include<math.h> 20 #include<string.h> 21 #define N 1005 22 #define inf 0x7fffffff 23 struct node{ 24 int u,v; 25 double w; 26 }edge[N*N]; 27 double g[N][N]; 28 double d[N]; 29 int n,m; 30 double l,r; 31 int edgenum; 32 void addedge(int u,int v,double w) 33 { 34 edge[edgenum].u=u; 35 edge[edgenum].v=v; 36 edge[edgenum++].w=w; 37 } 38 bool bellman_ford() 39 { 40 int k=(int)sqrt(1.0*(n+m)); 41 memset(d,0,sizeof(d)); 42 for(int i=0;i<k;i++){ 43 int flag=1; 44 for(int j=0;j<edgenum;j++) 45 if(d[edge[j].v]>d[edge[j].u]+edge[j].w){ 46 flag=0; 47 d[edge[j].v]=d[edge[j].u]+edge[j].w; 48 } 49 if(flag) break; 50 } 51 for(int j=0;j<edgenum;j++) 52 if(d[edge[j].v]>d[edge[j].u]+edge[j].w) 53 return false; 54 return true; 55 } 56 int main(void) 57 { 58 while(scanf("%d%d%lf%lf",&n,&m,&l,&r)!=EOF) 59 { 60 edgenum=0; 61 for(int i=1;i<=n;i++) 62 for(int j=1;j<=m;j++) 63 scanf("%lf",&g[i][j]); 64 for(int i=1;i<=n;i++) 65 for(int j=1;j<=m;j++){ 66 addedge(i,j+n,-log(l/g[i][j])); 67 addedge(j+n,i,log(r/g[i][j])); 68 } 69 if(bellman_ford()) puts("YES"); 70 else puts("NO"); 71 } 72 return 0; 73 }