HDU 3666 THE MATRIX PROBLEM 差分约束
2014-07-11 10:29
337 查看
THE MATRIX PROBLEM
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6845 Accepted Submission(s): 1763
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.
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
2010 Asia Regional Harbin
解题思路:等价于L<=c[i][j]*a[i]/b[j]<=R转化为logL-logc[i][j]<=logi-logj<=logR-logc[i][j],用栈比用队列快,负环判定条件为cnt[ed]>sqrt(n);
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <cmath> #include <stack> #define Maxn 1005 #define Inf 0x3f3f3f3f using namespace std; struct { int e,next; double w; }edge[Maxn*Maxn*2]; int head[Maxn],k; void add(int s,int e,double w) { edge[k].e=e; edge[k].w=w; edge[k].next=head[s]; head[s]=k++; } bool spfa(int n) { int i,st,ed,cnt[Maxn]; double dis[Maxn]; bool vis[Maxn]; stack<int> s; memset(cnt,0,sizeof(cnt)); memset(vis,false,sizeof(vis)); fill(dis,dis+Maxn,Inf*1.0); cnt[1]=1; dis[1]=0; vis[1]=true; s.push(1); int lim=int(sqrt(n*1.0)); while(!s.empty()) { st=s.top(); s.pop(); vis[st]=false; for(i=head[st];i!=-1;i=edge[i].next) { ed=edge[i].e; if(dis[ed]>dis[st]+edge[i].w) { dis[ed]=dis[st]+edge[i].w; if(!vis[ed]) { vis[ed]=true; cnt[ed]++; if(cnt[ed]>lim) return false; s.push(ed); } } } } return true; } int main() { int m,n; double l,r,c; freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); while(~scanf("%d%d%lf%lf",&m,&n,&l,&r)) { k=0; memset(head,-1,sizeof(head)); l=log(l); r=log(r); for(int i=1;i<=m;i++) for(int j=1;j<=n;j++) { scanf("%lf",&c); c=log(c); add(j+m,i,r-c); add(i,j+m,c-l); } printf("%s\n",spfa(m+n)?"YES":"NO"); } return 0; }
相关文章推荐
- hdu 3666 THE MATRIX PROBLEM 差分约束
- HDU 3666 THE MATRIX PROBLEM 差分约束
- HDU 3666 THE MATRIX PROBLEM 差分约束
- hdu 3666 THE MATRIX PROBLEM
- hdu 3666 THE MATRIX PROBLEM (差分约束)
- Hdu 3666 THE MATRIX PROBLEM(差分约束)
- HDU 3666 THE MATRIX PROBLEM (差分约束判负环)
- HDU 3666 THE MATRIX PROBLEM 差分约束系统
- HDU 3666 THE MATRIX PROBLEM(差分约束 + spfa 深搜判环)
- HDU_3666_THE MATRIX PROBLEM(差分约束+spfa+slf优化)
- hdu 3666 THE MATRIX PROBLEM
- hdu 3666 THE MATRIX PROBLEM
- hdu 3666 THE MATRIX PROBLEM(差分约束系统)
- hdu_3666_THE MATRIX PROBLEM
- hdu 3666 THE MATRIX PROBLEM
- HDU-3666 THE MATRIX PROBLEM
- HDU-3666 THE MATRIX PROBLEM
- hdu 3666 THE MATRIX PROBLEM
- hdu 3666 THE MATRIX PROBLEM 差分约束系统
- hdu 3666 THE MATRIX PROBLEM //2010哈尔滨现场赛