HDU-3666 THE MATRIX PROBLEM

很容易发现约束条件:L<=K[i,j]*A[i]/B[j]<=U

妈呀这可是乘法啊。。。看起来貌似没法化简。。。

那么看成对数呢?

lg(L)<=lg(K[i,j])+lg(A[i])-lg(B[j])<=lg(U)

这样子就能左移右移了吧=v=

判断是否有负权回路即可。。。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <deque>

#define rep(i, l, r) for(int i=l; i<=r; i++)
#define down(i, l, r) for(int i=l; i>=r; i--)
#define N 456
#define MAX 1<<30

using namespace std;
int read()
{
	int x=0, f=1; char ch=getchar();
	while (ch<‘0‘ || ch>‘9‘) { if (ch==‘-‘) f=-1; ch=getchar(); }
	while (ch>=‘0‘ && ch<=‘9‘) { x=x*10+ch-‘0‘; ch=getchar(); }
	return x*f;
}

struct node{int y, n; double v;} e[N*N*4]; int fir[N*2], en;
int n, m, l, u, k[N][N], c[N*2];
double d[N*2];
bool b[N*2], ans;

void Add(int x, int y, double v) { en++; e[en].y=y, e[en].v=v, e[en].n=fir[x], fir[x]=en; }

int main()
{
	while (scanf("%d %d %d %d", &n, &m, &l, &u) != EOF)
	{
		ans=1; en=0; rep(i, 1, N*2-1) fir[i]=0;
		rep(i, 1, n) rep(j, 1, m) k[i][j]=read();
		rep(i, 1, n) rep(j, 1, m) { Add(i, j+N, log(k[i][j])-log(l)); Add(j+N, i, log(u)-log(k[i][j])); }
		deque <int> q;
		rep(i, 1, n) b[i]=1, c[i]=1, d[i]=0, q.push_back(i);
		rep(i, 1+N, m+N) b[i]=1, c[i]=1, d[i]=0, q.push_back(i);
		while (!q.empty())
		{
			int x=q.front(), o=fir[x], y=e[o].y; b[x]=0; q.pop_front();
			if (c[x] > n) { ans=0; break; }
			while (o)
			{
				if (d[y] > d[x]+e[o].v)
				{
					d[y] = d[x]+e[o].v;
					if (!b[y]) b[y]=1, c[y]++, !q.empty()&&d[y]<=d[q.front()] ? q.push_front(y) : q.push_back(y);
				}
				o=e[o].n, y=e[o].y;
			}
		}
		if (ans) printf("YES\n"); else printf("NO\n");
	}
	return 0;
}
时间: 2024-10-11 12:19:23

HDU-3666 THE MATRIX PROBLEM的相关文章

Hdu 3666 THE MATRIX PROBLEM(差分约束)

题目地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=3666 思路:差分约束. 取对数将乘除转化为加减. L<=m[i][j]*a[i]/b[j]<=U log(L/m[i][j])<=log(a[i])-log(b[j])<=log(U/m[i][j]) 则 : log(a[i])<=log(b[j])+log(U/m[i][j]) log(b[j])<=log(a[i])+log(m[i][j]/L) SPFA判

HDU 3666 THE MATRIX PROBLEM (差分约束)

题意:给定一个最大400*400的矩阵,每次操作可以将某一行或某一列乘上一个数,问能否通过这样的操作使得矩阵内的每个数都在[L,R]的区间内. 析:再把题意说明白一点就是是否存在ai,bj,使得l<=cij*(ai/bj)<=u (1<=i<=n,1<=j<=m)成立. 首先把cij先除到两边去,就变成了l'<=ai/bj<=u',由于差分约束要是的减,怎么变成减法呢?取对数呗,两边取对数得到log(l')<=log(ai)-log(bj)<=l

HDU 3666 THE MATRIX PROBLEM (差分约束,最短路)

题意:给一个n*m矩阵,每个格子上有一个数字a[i][j],给定L和U,问:是否有这样两个序列{a1...an}和{b1...bn},满足 L<=a[i][j]*ai/bj<=U .若存在输出yes,否则no. 思路:能够得到的是一对不等式,那么可以用最短路来解决差分约束系统.但是a[i][j]*ai/bj<=U是除的,得提前变成减的才行.可以用log来解决,先不管a[i][j],logai-logbj<=U不就行了?可以得到: (1)logai - logbj<=U/a[i

HDOJ 3666 THE MATRIX PROBLEM 差分约束

根据题意有乘除的关系,为了方便构图,用对数转化乘除关系为加减关系..... THE MATRIX PROBLEM Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7486    Accepted Submission(s): 1914 Problem Description You have been given a matrix CN

矩阵乘法 --- hdu 4920 : Matrix multiplication

Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 820    Accepted Submission(s): 328 Problem Description Given two matrices A and B of size n×n, find the product of them. b

hdu 4965 Fast Matrix Calculation(矩阵快速幂)2014多校训练第9场

Fast Matrix Calculation                                                                   Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description One day, Alice and Bob felt bored again, Bob knows Ali

hdu 5293 Tree chain problem(树链剖分+树形dp)

题目链接:hdu 5293 Tree chain problem 维护dp[u], sum[u],dp[u]表示以u为根节点的子树的最优值.sum[u]表示以u节点的所有子节点的dp[v]之和.对于边a,b,w,在LCA(a,b)节点的时候进行考虑.dp[u] = min{dp[u], Sum(a,b) - Dp(a,b) + sum[u] | (ab链上的点,不包括u } #pragma comment(linker, "/STACK:1024000000,1024000000")

HDU 4965 Fast Matrix Calculation 【矩阵】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4965 题目大意:给你一个N*K的矩阵A以及一个K*N的矩阵B (4 <= N <= 1000)以及 (2 <=K <= 6),然后接下来四步: 算一个新的矩阵C=A*B 算M=C^ (N*N) 对于M中的每个元素%6 将M中每个元素加起来,算出和. 也就是求出A*B * A*B * A*B * A*B * A*B *--* A*B   但是A*B形成的矩阵是N*N,而N大小有可能是10

hdu 4267 A Simple Problem with Integers

题目链接:hdu 4267 A Simple Problem with Integers 类似于题目:hdu 1556 Color the ball 的技巧实现树状数组的段更新点查询. 由于该题对于段的更新并不是连续的,从而可以构造多个树状数组.因为$k \in [1,10] $,从而可以把更新划分为如下类型: 1,2,3,4,5... ------------- 1,3,5,7,9... 2,4,6,8,10... ------------- 1,4,7,10,13... 2,5,8,11,1

HDU 1016 Prime Ring Problem 题解

Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle should always be 1