HDU 3666 THE MATRIX PROBLEM (差分约束 深搜 & 广搜)
amp The HDU 约束 Problem matrix 差分
2023-09-27 14:22:23 时间
THE MATRIX PROBLEM
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5437 Accepted Submission(s): 1372
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
lcy
题目意思就是是否存在ai,bj,使得l<=cij*(ai/bj)<=u (1<=i<=n,1<=j<=m)成立
首先,把cij除到两边:l'<=ai/bj<=u',如果差分约束的话,应该是ai-bj的形式,于是可以取对数
log(l')<=log(ai)-log(bj)<=log(u')
把log(ai)和log(bj)看成两个点ai和bj,化成求最短路的形式:dis[ai]-dis[bj]<=log(u'),dis[bj]-dis[ai]<=-log(l')
然后判负环就行,深搜和广搜都可以
注意,如果spfa队列判负环:
(1)不必判断某个点入队次数大于N,只要判断是否大于sqrt(1.0*N)
(2)或者所有点的入队次数大于T*N,即存在负环,一般T取2
N为所有点的个数
1, SPFA广搜:
#include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<cmath> using namespace std; const int N=810; struct Edge{ int to,nxt; double cap; }edge[N*N]; int n,m,cnt,head[N]; int vis[N],Count[N]; double dis[N],L,U; void addedge(int cu,int cv,double cw){ edge[cnt].to=cv; edge[cnt].cap=cw; edge[cnt].nxt=head[cu]; head[cu]=cnt++; } int SPFA(){ int limit=(int)sqrt(1.0*(n+m)); queue<int> q; while(!q.empty()) q.pop(); memset(vis,0,sizeof(vis)); memset(Count,0,sizeof(Count)); for(int i=0;i<=n+m;i++){ dis[i]=0; q.push(i); } while(!q.empty()){ int u=q.front(); q.pop(); vis[u]=0; for(int i=head[u];i!=-1;i=edge[i].nxt){ int v=edge[i].to; if(dis[v]>dis[u]+edge[i].cap){ dis[v]=dis[u]+edge[i].cap; if(!vis[v]){ vis[v]=1; if(++Count[v]>limit) return 0; q.push(v); } } } } return 1; } int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d%d%lf%lf",&n,&m,&L,&U)){ cnt=0; memset(head,-1,sizeof(head)); double x; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++){ scanf("%lf",&x); addedge(j+n,i,log(U/x)); addedge(i,j+n,-log(L/x)); } if(SPFA()) puts("YES"); else puts("NO"); } return 0; }
2, SPFA深搜:(这个更快??)
#include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<cmath> using namespace std; const int N=810; struct Edge{ int to,nxt; double cap; }edge[N*N]; int n,m,cnt,head[N]; int vis[N],instack[N]; double dis[N],L,U; void addedge(int cu,int cv,double cw){ edge[cnt].to=cv; edge[cnt].cap=cw; edge[cnt].nxt=head[cu]; head[cu]=cnt++; } int SPFA(int u){ if(instack[u]) return 0; instack[u]=1; vis[u]=1; for(int i=head[u];i!=-1;i=edge[i].nxt){ int v=edge[i].to; if(dis[v]>dis[u]+edge[i].cap){ dis[v]=dis[u]+edge[i].cap; if(!SPFA(v)) return 0; } } instack[u]=0; return 1; } int solve(){ memset(vis,0,sizeof(vis)); memset(instack,0,sizeof(instack)); memset(dis,0,sizeof(dis)); for(int i=1;i<=n+m;i++) if(!vis[i]){ if(!SPFA(i)) return 0; } return 1; } int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d%d%lf%lf",&n,&m,&L,&U)){ cnt=0; memset(head,-1,sizeof(head)); double x; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++){ scanf("%lf",&x); addedge(j+n,i,log(U/x)); addedge(i,j+n,-log(L/x)); } if(solve()) puts("YES"); else puts("NO"); } return 0; }
相关文章
- Leetcode: Word Squares && Summary: Another Important Implementation of Trie(Retrieve all the words with a given Prefix)
- G 面经 && Leetcode: Longest Repeating Character Replacement
- Leetcode: Combination Sum IV && Summary: The Key to Solve DP
- Lintcode: Hash Function && Summary: Modular Multiplication, Addition, Power && Summary: 长整形long
- "《 Serial Drivers 》by Alessandro Rubini" 学习笔记
- POJ 2388:Who's in the Middle
- Google Earth Engine ——MOD14A2.006: Terra Thermal Anomalies & Fire 8-Day Global 1km提供了1公里分辨率的8天火灾合成数据
- Android内、外存储分区&常用存储目录详解(Android Q)
- iOS 7 Pushing the Limits - Good & Bad Namings in Cocoa
- Java Number & Math 类,超详细整理,适合新手入门
- Android ANR全解析&华为AGC性能管理解决ANR案例集
- UVaLive 6581 && Gym 100299B What does the fox say? (模拟+STL)
- linux复习 --个人快速复习&乱写
- python递归遍历查询文件 & 文件夹
- 淘宝内部分享:MySQL & MariaDB性能优化 【转】
- 我的Android进阶之旅------>解决:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
- spring mvc & velocity 配置备忘
- The bundle does not contain an app icon for iPhone / iPod Touch of exactly '120x120' pixels, in .pn
- 10gocm->session5->数据库管理实验->GC资源管理器的资源消耗组介绍
- 【图像处理】——改变图像的大小(降采样&重采样)下采样和上采样
- 学习笔记(29):Python网络编程&并发编程-信号量
- 工具说明书 - Git: stash & clean
- DVWA文件上传出现Incorrect folder permissions&The PHP module GD is not installed.的解决方法