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 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
 

 

Source
 题意:
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;
}

 

 

转载于:https://www.cnblogs.com/--ZHIYUAN/p/6536119.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值