THE MATRIX PROBLEM
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4096 Accepted Submission(s): 1051
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
今天才发现用指针链表的速度低于用数组实现的速度,而且
有一个很给力的优化,就是每个点进队列的次数最多有sqrt(N)(N为总的顶点数目)次,
还要注意的一点就是
数组千万不要开小了,我一开始开了10W,WA,接着20W,还是WA,换成30W,仍然WA,经过N次提交测试,开个33W的不会再WA了
囧~啊,没见过这么坑爹的题目。。。。。。HDOJ,我还会回来的。。。。。。
View Code
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 #include <cmath> 6 using namespace std; 7 #define maxn 1000 8 #define edgeNum 330000 9 #define inf 1e9 10 #define eps 1e-6 11 int n,m,k,cnt,s,t,ti; 12 double l,u; 13 int v[edgeNum],next[edgeNum],head[maxn]; 14 double w[edgeNum]; 15 void addedge(int uu,int vv,double ww){ 16 v[cnt]=vv; 17 w[cnt]=ww; 18 next[cnt]=head[uu]; 19 head[uu]=cnt++; 20 } 21 void init(){ 22 cnt=0; 23 memset(head,-1,sizeof(head)); 24 l=log(l);u=log(u); 25 double tmp; 26 for(int i=0;i<n;i++){ 27 for(int j=0;j<m;j++){ 28 scanf("%lf",&tmp); 29 tmp=log(tmp); 30 addedge(i,j+n,u-tmp); 31 addedge(j+n,i,tmp-l); 32 } 33 }s=0;t=n+m;ti=sqrt(t*1.0); 34 } 35 double dis[maxn]; 36 int num[maxn]; 37 bool inq[maxn]; 38 bool spfa(int s,int t){ 39 queue<int> q; 40 for(int i=0;i<t;i++){num[i]=0;q.push(i);inq[i]=true;dis[i]=inf;} 41 dis[s]=0; 42 while(!q.empty()){ 43 int u=q.front();q.pop();inq[u]=false; 44 for(int e=head[u];e!=-1;e=next[e]){ 45 int vv=v[e];double ww=w[e]; 46 if(dis[vv]>dis[u]+ww+eps){ 47 dis[vv]=dis[u]+ww; 48 if(!inq[vv]){inq[vv]=true;num[vv]++;q.push(vv);if(num[vv]>ti) return false;} 49 } 50 } 51 }return true; 52 } 53 int main() 54 { 55 while(scanf("%d%d%lf%lf",&n,&m,&l,&u)!=EOF){ 56 init(); 57 if(spfa(s,t)) printf("YES\n"); 58 else printf("NO\n"); 59 } 60 return 0; 61 }