题意:
给你一个N*M的矩阵C,问是否存在一个长度为N的数列a和长度为M的数列b使得所有C(i,j)*a(i)/b(j)在L , U范围内。
解题思路:
有两个式子 C(i,j)*a(i)/b(j) >= L , C(i,j)*a(i)/b(j) <= U ,两边均取对数得到 log(i) - log(j) <= log( U/C(i , j) ) 和 log(j) - log(i) <= log( C(i,j) / L) ,很显然的差分约束。。可惜用普通的队列版SPFA是会TLE的!网上说只需要入队sqrt(n)次就可以判断有负环了,不过没有具体的证明解释呀!所以正解应该是DFS版的也就是用栈的SPFA,因为要找负环,DFS找到一个负环后接下来相当于一直会绕着这个负环走个n+1次就跳出了,比BFS版判负环高效的多!
/* **********************************************
Author : JayYe
Created Time: 2013-10-23 16:18:20
File Name : JayYe.cpp
*********************************************** */
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int maxn = 800 + 5;
const int INF = 2000000000;
struct Edge{
int to, next;
double w;
}edge[maxn*maxn*2];
int head[maxn], T[maxn], E, q[maxn*maxn];
bool vis[maxn];
void init(int n) {
for(int i = 0;i <= n; i++)
head[i] = -1;
E = 0;
}
void newedge(int u, int to, double w) {
edge[E].to = to;
edge[E].w = w;
edge[E].next = head[u];
head[u] = E++;
}
double dis[maxn];
bool SPFA(int n) {
for(int i = 0;i <= n; i++) {
dis[i] = INF; vis[i] = T[i] = 0;
}
T[0] = 1;
dis[0] = 0;
vis[0] = 1;
int st = 0, ed = 0;
q[++ed] = 0;
while(ed) {
int u = q[ed--];
//if(st > n*n) return false;
vis[u] = 0;
for(int i = head[u];i != -1;i = edge[i].next) {
int to = edge[i].to;
double w = edge[i].w;
if(dis[to] > dis[u] + w) {
dis[to] = dis[u] + w;
if(!vis[to]) {
vis[to] = 1;
T[to]++;
if(T[to] > n + 1 ) return true;
q[++ed] = to;
}
}
}
}
return false;
}
int main() {
int n, m, L, U, c;
while(scanf("%d%d%d%d", &n, &m, &L, &U) != -1) {
init(n+m);
for(int i = 1;i <= n; i++) {
for(int j = 1;j <= m; j++) {
scanf("%d", &c);
newedge(j + n, i, log((double)U) - log((double)c));
newedge(i, j + n, log((double)c) - log((double)L));
}
}
for(int i = 1;i <= n+m; i++) newedge(0, i, 0);
if(!SPFA(n + m)) puts("YES");
else puts("NO");
}
return 0;
}