hdu 3666 THE MATRIX PROBLEM(差分约束,思路)

THE MATRIX PROBLEM

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


Problem Description
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 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
题意:存不存在序列a和b满足L<=cij*ai/bj<=R

思路:将原式进行转换,我们的目标是化成一个减法的形式。那么可以得到

log(L/cij)<=logai-logbj<=log(R/cij)

然后我们就把b数组放在a数组后面即可

不过要注意的是,用STL的queue的话,判断负环要大于sqrt(n),不过网上没给一份解释,玄学。

所以这里用手动栈即可

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 1050
#define INF 999999999
struct Edge
{
    int v,next;
    double w;
} edge[N*N*2];
int cnt,head[N],n;
int vis[N],num[N];
double d[N];
void init()
{
    cnt=0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v,double w)
{
    edge[cnt].v=v,edge[cnt].w=w;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
int que[N*N];
int spfa(int s,int t)
{
    memset(vis,0,sizeof(vis));
    memset(num,0,sizeof(num));
    for(int i=s; i<=t; i++)
        d[i]=i==s?0:INF;
    vis[s]=1;
    int st=0,ed=0;
    que[++ed]=s;
    while(ed)
    {
        int u=que[ed--];
        vis[u]=0;
        for(int i=head[u]; i!=-1; i=edge[i].next)
        {
            int v=edge[i].v;
            double w=edge[i].w;
            if(d[v]>d[u]+w)
            {
                d[v]=d[u]+w;
                if(!vis[v])
                {
                    vis[v]=1;
                    num[v]++;
                    if(num[v]>t+1) return 0;
                    que[++ed]=v;
                }
            }
        }
    }
    return 1;
}
int main()
{
    int m,l,r;
    double c;
    while(~scanf("%d %d %d %d",&n,&m,&l,&r))
    {
        init();
        for(int i=1; i<=n; i++)
            for(int j=1; j<=m; j++)
            {
                scanf("%lf",&c);
                addedge(i,n+j,log(c)-log(l));
                addedge(n+j,i,log(r)-log(c));
            }
        for(int i=1; i<=n+m; i++)
            addedge(0,i,0);
        int ans=spfa(0,n+m);
        if(ans) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值