HDU 3666 THE MATRIX PROBLEM

Description

You have been given a matrix CNM, each element E of CNM 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.

solution

正解:差分约束
化简式子 \(L<=a[i][j]*a[i]/b[j]<=U\)
\[a[i][j]*a[i]>=L*b[j]\]
\[a[i][j]*a[i]<=U*b[j]\]
这样子并不好处理,差分约束式子一般表示为 \(x+y<z\) 的形式,所以想办法转化为加法
考虑取对数:
式子就变成了
\[logai-logbj>=logL-loga[i][j]\]
\[logai-logbj<=logU-loga[i][j]\]
ai到bj分别建边即可

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#define RG register
#define il inline
using namespace std;
const int N=405,P=805,M=320005,inf=2e8;
int a[N][N],n,m,U,L,num=0,head[P],to[M],nxt[M],inst[P];
double dis[M];
void link(int x,int y,double z){
   nxt[++num]=head[x];to[num]=y;dis[num]=z;head[x]=num;}
void Clear(){
   memset(head,0,sizeof(head));
   memset(inst,0,sizeof(inst));
   num=0;
}
queue<int>q;
double f[P];int lim;bool vis[P];
bool spfa(){
   while(!q.empty())q.pop();
   q.push(1);
   for(RG int i=1;i<=n+m;i++)
      vis[i]=0,f[i]=inf;
   RG int x,u;
   f[1]=0;vis[1]=1;inst[1]++;
   while(!q.empty()){
      x=q.front();q.pop();
      for(RG int i=head[x];i;i=nxt[i]){
         u=to[i];
         if(f[x]+dis[i]<f[u]){
            f[u]=f[x]+dis[i];
            if(!vis[u]){
               vis[u]=1,q.push(u);
               inst[u]++;
               if(inst[u]>lim)return 0;
            }
         }
      }
      vis[x]=0;
   }
   return 1;
}
void work()
{
   Clear();
   double lu=log(U),lL=log(L);
   for(int i=1;i<=n;i++)
      for(int j=1;j<=m;j++){
         scanf("%d",&a[i][j]);
         link(j+n,i,-lL+log(a[i][j]));
         link(i,j+n,lu-log(a[i][j]));
    }
   lim=sqrt(n+m);
   bool t=spfa();
   if(t)puts("YES");
   else puts("NO");
}

int main()
{
    while(~scanf("%d%d%d%d",&n,&m,&L,&U))
      work();
    return 0;
}

转载于:https://www.cnblogs.com/Hxymmm/p/7749717.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值