题目大意:
给你一个n*m的矩阵,现在有一个长度为n的序列a,一个长度为m的序列b,让你把这个矩阵第i行的所有元素都乘以a[i],把第j列的元素都除以b[j],问你存不存在这样的两个序列a,b,使得经过这些操作之后的矩阵每个元素都在[L, U]之间
解题思路:
可以得出要求是 L <= num[i][j] * a[i] / b[j] <= U
可以转换一下变成
log(L / num[i][j]) <= log(a[i]) - log(b[i]) <= log(U / num[i][j])
这样就是一道差分约束模板题了
代码:
#include <cmath> #include <queue> #include <cstdio> #include <cstring> using namespace std; const int maxn = 400 + 5; const int INF = 1e9 + 7; typedef struct node{ int to; int next; double w; node(int a = 0, int b = 0, double c = 0){ to = a; next = b; w = c; } }Edge; int s[maxn * 3]; double dis[maxn * 3]; Edge edge[maxn * maxn * 3]; int tot, head[maxn * maxn * 3]; int vis[maxn * 3], cnt[maxn * 3]; void add(int u, int v, double w){ edge[tot] = node(v, head[u], w); head[u] = tot++; } bool spfa(int e){ int u, v, top = 0; for(int i = 0; i <= e; ++i){ dis[i] = INF; vis[i] = 0; cnt[i] = 0; } s[top++] = 0; vis[0] = 1; dis[0] = 0; while(top){ u = s[--top]; vis[u] = 0; if((++cnt[u]) > e) return 0; for(int i = head[u]; ~i; i = edge[i].next){ v = edge[i].to; if(dis[v] > dis[u] + edge[i].w){ dis[v] = dis[u] + edge[i].w; if(!vis[v]){ s[top++] = v; vis[v] = 1; } } } } return 1; } int main(){ int n, m, l, u, num; while(~scanf("%d%d%d%d", &n, &m, &l, &u)){ tot = 0; memset(head, -1, sizeof(head)); for(int i = 0; i < n; ++i){ for(int j = 0; j < m; ++j){ scanf("%d", &num); add(i, j + n, log(1.0 * u / num)); add(j + n, i, -log(1.0 * l / num)); } } if(spfa(n + m - 1)) puts("YES"); else puts("NO"); } return 0; }