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. iOS-调用百度地图,苹果自带地图,高德地图,谷歌地图导航方法

    - (void)actionSheet : (ServiceNetworkModel *)model{ __block NSString *urlScheme = @"demoURI://& ...

  2. 用Electron开发桌面应用app的相关文献集锦

    1. 超棒的发声器(项目实战) 原文点此链接 2. Electron中文文档 原文点此链接

  3. HighCharts中几种tooltip的显示格式

    推荐学习地址 => https://www.hcharts.cn/docs/basic-tooltip   https://api.hcharts.cn/#Highcharts.numberFo ...

  4. 关于全球唯一标识符GUID

    在C#中的语法: Console.WriteLine(System.Guid.NewGuid()); Console.ReadKey(); System.Guid.NewGuid().ToString ...

  5. 洛谷 [FJOI2014]最短路径树问题 解题报告

    [FJOI2014]最短路径树问题 题目描述 给一个包含\(n\)个点,\(m\)条边的无向连通图.从顶点\(1\)出发,往其余所有点分别走一次并返回. 往某一个点走时,选择总长度最短的路径走.若有多 ...

  6. 【NOIP 模拟赛】Evensgn 剪树枝 树形dp

    由于树规做的少所以即使我考试想出来正确的状态也不会转移. 一般dp的转移不那么繁杂(除了插头.....),即使多那也是清晰明了的,而且按照树规的一般思路,我们是从下到上的,所以我们要尽量简洁地从儿子那 ...

  7. 写一个JavaScript“返回顶部”功能

    在web页面中,如果页面较高,为了方便用户快速地返回顶部,都会添加一个返回顶部按钮. 效果演示可以查看本页.如果页面有滚动高度,右下角就会有一个含有“返回顶部”字样的黑色背景半透明的小条条.点击这里“ ...

  8. POJ1182:食物链(并查集)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 94930   Accepted: 28666 Description ...

  9. D. Relatively Prime Graph

    Let's call an undirected graph G=(V,E)G=(V,E) relatively prime if and only if for each edge (v,u)∈E( ...

  10. lnmp重置mysql数据库root密码

    第一种方法:用军哥的一键修改LNMP环境下MYSQL数据库密码脚本 一键脚本肯定是非常方便.具体执行以下命令: wget http://soft.vpser.net/lnmp/ext/reset_my ...