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): 3001 Accepted Submission(s): 784


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

Recommend
lcy
 
给一个n*m的矩阵X和数L、U,求n个数ai,和m个数bj,使得Xij*ai/bj在L到U的范围内。
刚开始看到不等式的时候就猜测可能是差分约束系统,但是不等式里面有乘法不知道怎么搞,后来才知道通过取对数把乘法转换为加法就行了。
下面来看不等式:
L<=Xij*ai/bj<=U              ==>
L*bj<=Xij*ai<=U*bj         ==>
L*bj-Xij*ai<=0
Xij*ai-U*bj<=0                 ==>
log(L*bj)-log(Xij*ai)<=0
log(Xij*ai)-log(U*bj)<=0   ==>
logbj-logai<=logXij-logL
logai-logbj<=logU-logXij
这样就转换为标准的差分约束系统的不等式。
所以建图,有n+m个节点
i到j+n连边,边权为log(Xij/L)
j+n到i连边,边权为log(U/Xij)
然后用spfa判断是否有负环,有负环说明不等式组无解。
但是普通的bellman,spfa判负环都会超时,递归的spfa也不行,
只有使用经验公式了:
1.cnt[]记录节点加入队列次数,大于sqrt(n)求可以当做有负环了。
2.sum记录所有节点加入队列次数大于2*(n+m),也当做有负环了。
但这是不正确的,要看数据了。
 
代码:
开了输入外挂才优化到400ms...
#include<cstdio>
#include<cstring>
#include<cmath>
#define inf 999999999
using namespace std;

int n,m,num,adj[805],f[805],q[10005];
double dis[805],L,U;
struct edge
{
	int v,pre;
	double w;
}e[320005];
void insert(int u,int v,double w)
{
	e[num].v=v;
	e[num].w=w;
	e[num].pre=adj[u];
	adj[u]=num++;
}
int spfa(int x)
{
	int i,v,sum=0,head=0,tail=0;
	memset(f,0,sizeof(f));
	for(i=0;i<n;i++)
		dis[i]=inf;
	dis[0]=0;
	q[tail++]=x;
	while(head<tail)
	{
		x=q[head++];
		f[x]=0;
		for(i=adj[x];~i;i=e[i].pre)
			if(dis[v=e[i].v]>dis[x]+e[i].w+1e-8)
			{
				dis[v]=dis[x]+e[i].w;
				if(!f[v])
				{
					sum++;
					if(sum>2*(n+m))
						return 0;
					f[v]=1;
					q[tail++]=v;
				}
			}
	}
	return 1;
}
inline int getint()
{
    char c=getchar();
    while(c<'0'||c>'9')c=getchar();
    int n=c-'0';
    c=getchar();
    while(c>='0'&&c<='9')
    {
        n=(n<<1)+(n<<3)+c-'0';
        c=getchar();
    }
    return n;
}
int main()
{
	while(~scanf("%d",&n))
	{
		int i,j,k;
		num=0;
		memset(adj,-1,sizeof(adj));
		m=getint();
		L=getint();
		U=getint();
		for(i=0;i<n;i++)
			for(j=0;j<m;j++)
			{
				k=getint();
				insert(i,j+n,log(k/L));
				insert(j+n,i,log(U/k));
			}
		n+=m;
		m+=m;
		if(spfa(0))
			puts("YES");
		else
			puts("NO");
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值