hdu 3666 THE MATRIX PROBLEM (差分约束)

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 C N*M, each element E of C N*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.

 

 

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
lcy   |   We have carefully selected several similar problems for you:   3665  3669  3667  3664  3663 
 
 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 }

 

转载于:https://www.cnblogs.com/GO-NO-1/p/3450809.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值