HDU3666 差分约束

THE MATRIX PROBLEM

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

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

题意:

n*m的矩阵c,是否存在n个数a1,a2,...an,和m个数b1,b2,...bm使得L<=cij*ai/bj<=R.

代码:

//显然不满足差分约束的条件,可以L<=cij*ai/bj<=R两边除cij(cij>0)后取对数得到
//log(L/cij)<=log(ai)-log(bj)<=log(R/cij).只求存不存在就行。但是本体如果用stl
//的queue写spfa会超时(可以用节点出队次数小于sqrt(n)判断),可以自己定义一个栈来存储。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn=402;
const double inf=100000008;
int n,m,tol,head[maxn*2+10],cnt[maxn*2+10],stk[maxn*maxn];
double L,R,dis[maxn*2+10];
bool mark[maxn*2+10];
struct node
{
    int to,next;
    double val;
}nodes[360010];
void Add(int a,int b,double c)
{
    nodes[tol].to=b;
    nodes[tol].val=c;
    nodes[tol].next=head[a];
    head[a]=tol++;
}
bool spfa(int s)
{
    for(int i=0;i<=n+m;i++){
        dis[i]=inf;
        cnt[i]=0;
        mark[i]=0;
    }
    int top=0;
    stk[++top]=s;
    mark[s]=1;cnt[s]++;dis[s]=0;
    while(top>0){
        int u=stk[top--];
        mark[u]=0;
        for(int i=head[u];i!=-1;i=nodes[i].next){
            int v=nodes[i].to;
            if(dis[v]>dis[u]+nodes[i].val){
                dis[v]=dis[u]+nodes[i].val;
                if(!mark[v]){
                    mark[v]=1;
                    if(++cnt[v]>=n+m) return 0;
                    stk[++top]=v;
                }
            }
        }
    }
    return 1;
}
int main()
{
    while(scanf("%d%d%lf%lf",&n,&m,&L,&R)==4){
        tol=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);
                Add(j+n,i,log(R/x));
                Add(i,j+n,-log(L/x));
            }
        }
        for(int i=1;i<=n+m;i++)//加一个公共源点0
            Add(0,i,0);
        if(spfa(0)) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}
时间: 2024-09-30 17:29:15

HDU3666 差分约束的相关文章

hdu3666 THE MATRIX PROBLEM --- 差分约束

这要是碰上现场赛我得被搞死 从RE到TLE到WA已疯.. 这题建图没有那么直接,通过给出的不等式关系一时想不到怎么建图 所以要对题目给的条件一定程度化简,将不等式两边取对数化简得到Sa-Sb<=c的形式 要注意w取double类型 其次,这题卡时间,根据经验加剪枝: 1.出队次数>sqrt(n)则判断有负环 2.统计总的入队次数,>2n则判断有负环 一般情况下不用这个,因为不严谨 下面两个spfa都是对的,手写队列稍快一点,上面第二个剪枝效果明显 #include<iostream

HDU3666 THE MATRIX PROBLEM (差分约束+取对数去系数)(对退出情况存疑)

You have been given a matrix C N*M, each element E of C N*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 e

差分约束

1.bzoj3436 思路: 差分约束根据限制条件建图,注意要有一个超级源点向所有点连一条边权为0的边建图看代码. 然后spfa判负环,写bfs会超时的......实测n遍. #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #define inf 0x7fffffff #define ll long long #define N 100007 using na

bzoj2788 festival 差分约束

填坑中--链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2788 题意: 有$n$个正整数$X1,X2,...,Xn$,再给出$m1+m2$个限制条件,限制分为两类:1. 给出$a,b(1<=a,b<=n)$,要求满足$Xa + 1 = Xb$2. 给出$c,d (1<=c,d<=n)$,要求满足$Xc <= Xd$在满足所有限制的条件下,求集合${Xi}$大小的最大值. 首先看情况我们也知道是差分约束-- 但是这个差分

POJ 1201 Intervals 差分约束

http://poj.org/problem?id=1201 TLE了很久,因为用了cin..... 思路和其他差分约束差不多,http://www.cppblog.com/menjitianya/archive/2015/11/19/212292.html 如果区间[a, b]中至少有c个元素,如果用上面的博客,那么说明xa - xb >= c,但是注意这里是闭区间,xa - xb是不包括b这个点的, 就比如用了[a, b]有c个元素,[b, d]有x个,那么ans = c + x - 1个,

【bzoj2330】: [SCOI2011]糖果 图论-差分约束-SPFA

[bzoj2330]: [SCOI2011]糖果 恩..就是裸的差分约束.. x=1 -> (A,B,0) (B,A,0) x=2 -> (A,B,1)  [这个情况加个A==B无解的要特判] x=3 -> (B,A,0)  [恩这个是不少于一开始zz建反了] x=4 -> (B,A,1) x=5 -> (A,B,0) 然后源点到所有点建1的边[恩据说有条链所以要反着连]跑最长路就好了 1 /* http://www.cnblogs.com/karl07/ */ 2 #inc

POJ1275出纳员的雇佣【差分约束】

出纳员的雇佣 Tehran的一家每天24小时营业的超市,需要一批出纳员来满足它的需要.超市经理雇佣你来帮他解决问题:超市在每天的不同时段需要不同数目的出纳员(例如:午夜时只需一小批,而下午则需要很多)来为顾客提供优质服务.他希望雇佣最少数目的出纳员.经理已经提供你一天的每一小时需要出纳员的最少数量--R(0), R(1), ..., R(23).R(0)表示从午夜到上午1:00需要出纳员的最少数目,R(1)表示上午1:00到2:00之间需要的,等等.每一天,这些数据都是相同的.有N人申请这项工作

【POJ3169 】Layout (认真的做差分约束)

Layout Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they ar

POJ 3169 Layout (差分约束)

题意:给定一些母牛,要求一个排列,有的母牛距离不能超过w,有的距离不能小于w,问你第一个和第n个最远距离是多少. 析:以前只是听说过个算法,从来没用过,差分约束. 对于第 i 个母牛和第 i+1 个,D[i] - D[i+1] <= 0,  D[j] -D[i ]<= k, D[i] - D[j] <= - k,那么这个题就可以用差分约束来求这个不等式组了. 1.对于差分不等式,a - b <= c ,建一条 b 到 a 的权值为 c 的边,求的是最短路,得到的是最大值(本题求的就