THE MATRIX PROBLEM

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8016    Accepted Submission(s): 2092

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
 
题意:给出一个矩阵 C 问是否存在这样两个序列使得  l <= cij * ai/bj <= r 对这个矩阵成立。
题解:将cij除过去,然后取个对数, log(l/cij) <= log(ai) - log(bj) <= log(r/cij) 这样的话就可以化为一个差分约束的题了,但是这个题有个问题就是判环会超时,有人提出可以只判断sqrt(n)次就行了,,但是无法证明,这个题的更好的方法是用一个手动栈代替队列.这样的话就能够快速判环了,给出两份代码..
只用sqrt(n)的队列写法:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<math.h>
using namespace std;
const double INF = ;
const int N = ;
const int M = ;
struct Edge{
int v,next;
double w;
}edge[M];
int head[N],tot;
int n,m,l,r;
void init(){
memset(head,-,sizeof(head));
tot = ;
}
void addEdge(int u,int v,double w,int &k){
edge[k].v =v ,edge[k].w = w ,edge[k].next = head[u],head[u] = k++;
}
double low[N];
int time[N];
bool vis[N];
bool spfa(int s){
for(int i=;i<=n+m;i++){
vis[i] = false;
time[i] = ;
low[i] = INF;
}
int num = ((int)sqrt(n+m))+;
low[s] = ;
time[s]++;
queue<int>q;
q.push(s);
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
for(int k=head[u];k!=-;k=edge[k].next){
int v = edge[k].v;
double w = edge[k].w;
if(low[v]>low[u]+w){
low[v] = low[u]+w;
if(!vis[v]){
vis[v] = true;
q.push(v);
time[v]++;
if(time[v]>num) return false;
}
}
}
}
return true;
}
int main()
{
double c;
while(scanf("%d%d%d%d",&n,&m,&l,&r)!=EOF){
init();
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
scanf("%lf",&c);
addEdge(i,n+j,-log(l/c),tot);
addEdge(n+j,i,log(r/c),tot);
}
}
for(int i=;i<=n+m;i++){
addEdge(,i,,tot);
}
if(spfa()) printf("YES\n");
else printf("NO\n");
}
return ;
}

手动栈解决:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<math.h>
using namespace std;
const double INF = ;
const int N = ;
const int M = ;
struct Edge{
int v,next;
double w;
}edge[M];
int head[N],tot;
int n,m,l,r;
void init(){
memset(head,-,sizeof(head));
tot = ;
}
void addEdge(int u,int v,double w,int &k){
edge[k].v =v ,edge[k].w = w ,edge[k].next = head[u],head[u] = k++;
}
double low[N];
int time[N];
bool vis[N];
int stk[N*N];
bool spfa(int s){
for(int i=;i<=n+m;i++){
vis[i] = false;
time[i] = ;
low[i] = INF;
}
int top = ;
low[s] = ;
time[s]++;
stk[top++] = s;
while(top!=){
int u = stk[--top];
vis[u] = false;
for(int k=head[u];k!=-;k=edge[k].next){
int v = edge[k].v;
double w = edge[k].w;
if(low[v]>low[u]+w){
low[v] = low[u]+w;
if(!vis[v]){
vis[v] = true;
stk[top++] = v;
time[v]++;
if(time[v]>n+m) return false;
}
}
}
}
return true;
}
int main()
{
double c;
while(scanf("%d%d%d%d",&n,&m,&l,&r)!=EOF){
init();
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
scanf("%lf",&c);
addEdge(i,n+j,-log(l/c),tot);
addEdge(n+j,i,log(r/c),tot);
}
}
for(int i=;i<=n+m;i++){
addEdge(,i,,tot);
}
if(spfa()) printf("YES\n");
else printf("NO\n");
}
return ;
}

hdu 3666(差分约束,手动栈解决超时问题)的更多相关文章

  1. hdu 1531(差分约束)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1531 差分约束的题之前也碰到过,刚好最近正在进行图论专题的训练,就拿来做一做. ①:对于差分不等式,a ...

  2. I - 动物狂想曲 HDU - 6252(差分约束)

    I - 动物狂想曲 HDU - 6252 雷格西桑和路易桑是好朋友,在同一家公司工作.他们总是一起乘地铁去上班.他们的路线上有N个地铁站,编号从1到N.1站是他们的家,N站是公司. 有一天,雷格西桑起 ...

  3. hdu 4598 差分约束

    思路:首先就是判断是否有奇环,若存在奇环,则输出No. 然后用差分约束找是否符合条件. 对于e(i,j)属于E,并且假设顶点v[i]为正数,那么v[i]-v[j]>=T--->v[j]-v ...

  4. hdu 1364(差分约束)

    King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12056   Accepted: 4397 Description ...

  5. hdu 1534(差分约束)

    Schedule Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. hdu 1534(差分约束+spfa求最长路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1534 思路:设s[i]表示工作i的开始时间,v[i]表示需要工作的时间,则完成时间为s[i]+v[i] ...

  7. hdu 3440 差分约束

    看完题目第一遍,感觉很简单.当写完程序跑测试用例的时候,发现第二个总是过不了,然后好好研究了一下测试用例,才知道原来不是程序有问题,而是我的建图方式错了.对于这些无序的点,如果高的在右边,不等式是di ...

  8. hdu 3440(差分约束好题)

    House Man Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  9. POJ 3159 Candies 还是差分约束(栈的SPFA)

    http://poj.org/problem?id=3159 题目大意: n个小朋友分糖果,你要满足他们的要求(a b x 意思为b不能超过a x个糖果)并且编号1和n的糖果差距要最大. 思路: 嗯, ...

随机推荐

  1. python 调用RESTFul接口

    本周需要将爬虫爬下来的数据入库,因为之前已经写好PHP的接口的,可以直接通过python调用PHP接口来实现,所以把方法总结一下. //python编码问题,因为好久用,所以很容易出现 # -*- c ...

  2. Java循环控制语句-switch

    Java循环控制语句之一switch 不同于其他循环控制语句的特性: switch的英文解释为开关,正如它的解释一样,switch循环的特点就像开关一样,跳到哪一个条件即会出现某一种结果. 写法: s ...

  3. OJ 列表

    维护一个 OJ 列表.(这件事似乎没啥意义?) AtCoder hihoCoder Codeforces DMOJ CodeChef CS Academy HackerRank HackerEarth ...

  4. 【ZJ选讲·画山】

    给出一张纸(N × M),你要在上面画山,但不能画出界(N,M<=100) Like this: 起点为(0,0),终点为(N,0) 给出w种线段画法(x,y),表示用了这种画法后,笔迹末 ...

  5. 【BZOJ 4034】[HAOI2015]树上操作 差分+dfs序+树状数组

    我们只要看出来这道题 数组表示的含义就是 某个点到根节点路径权值和就行 那么我们可以把最终答案 看做 k*x+b x就是其深度 ,我们发现dfs序之后,修改一个点是差分一个区间,修改一个点的子树,可以 ...

  6. JavaScript的相等(==)与全等(===)

    有段代码如下: view source   print? 1 if (![] == []) { 2     //Code 3 } ![] == [],true or false? 我们都知道,ECMA ...

  7. 如何使用Eclipse调试framework

    1.下载Eclipse EE(下载地址:http://www.eclipse.org/downloads/) 2.下载并安装JDK(下载地址:http://www.oracle.com/technet ...

  8. mysql删除id最小的条目

    DELETE FROM 表1 WHERE Mid in (select Mid from (SELECT Min(Mid) Mid FROM 表1 c1) t1);

  9. 测试数据库DG搭建为正式库以后做准备

    Data guard 部署 1.系统准备(备库只需建立数据库软件) 两台操作系统 oracle linux 7 Node1 172.16.70.191 Node2 172.16.70.192 Orac ...

  10. (转)Django常用命令

    转自GoodSpeed,http://www.cnblogs.com/cacique/archive/2012/09/30/2709145.html . . . . .