HDOJ 3666 THE MATRIX PROBLEM 差分约束
2015-07-29 22:30
134 查看
根据题意有乘除的关系,为了方便构图,用对数转化乘除关系为加减关系.....
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7486 Accepted Submission(s): 1914
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
THE MATRIX PROBLEM
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7486 Accepted Submission(s): 1914
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
/* *********************************************** Author :CKboss Created Time :2015年07月29日 星期三 20时55分16秒 File Name :HDOJ3666.cpp ************************************************ */ #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <string> #include <cmath> #include <cstdlib> #include <vector> #include <queue> #include <set> #include <map> using namespace std; const int maxn=1000; const double eps=1e-8; int n,m; double L,R; struct Edge { int to,next; double cost; }edge[maxn*maxn*2]; int Adj[maxn],Size; void init() { memset(Adj,-1,sizeof(Adj)); Size=0; } void Add_Edge(int u,int v,double c) { edge[Size].to=v; edge[Size].next=Adj[u]; edge[Size].cost=c; Adj[u]=Size++; } double dist[maxn]; int cQ[maxn]; bool inQ[maxn]; bool spfa(int rt) { for(int i=0;i<n+m+10;i++) dist[i]=1e40; memset(cQ,0,sizeof(cQ)); memset(inQ,false,sizeof(inQ)); dist[rt]=0; queue<int> q; inQ[rt]=true;q.push(rt); cQ[rt]=1; while(!q.empty()) { int u=q.front();q.pop(); for(int i=Adj[u];~i;i=edge[i].next) { int v=edge[i].to; if(dist[v]>dist[u]+edge[i].cost) { dist[v]=dist[u]+edge[i].cost; if(!inQ[v]) { inQ[v]=true; cQ[v]++; if(cQ[v]>=sqrt(n+m)) return false; q.push(v); } } } inQ[u]=false; } return true; } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); while(scanf("%d%d%lf%lf",&n,&m,&L,&R)!=EOF) { init(); for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { double x; scanf("%lf",&x); int a=i,b=n+j; Add_Edge(b,a,log(R/x)); Add_Edge(a,b,-log(L/x)); } } for(int i=1;i<=n+m;i++) Add_Edge(0,i,0); bool fg=spfa(0); if(fg) puts("YES"); else puts("NO"); } return 0; }
相关文章推荐
- javascript/jquery判断是否为undefined或是null!
- 键的约束及操作
- Perl OOP
- PL/SQL Developer自动补全SQL语句
- PL/SQL Developer自动补全SQL语句
- PL/SQL Developer自动补全SQL语句
- 关系型数据库的典型概念
- GCD介绍(一):基本概念和Dispatch Queue
- UIView属性学习
- WEB服务器、应用程序服务器、HTTP服务器有何区别
- 辞职后的一些感想
- $GLOBALS ["HTTP_RAW_POST_DATA"]为空
- Rotate Array
- 套接字IO模型(一) Select模型
- MySQL读取不重复的数据
- Rotate Array
- (剑指Offer)面试题45:圆圈中最后剩下的数字
- 读书笔记MoreEffectiveC++(28)
- VC学习笔记1对话框应用程序设计
- K - After Training CodeForces 195B-找规律