HDU 3666 (差分约束)

THE MATRIX PROBLEM

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7370    Accepted Submission(s): 1881


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
 

题意:看了半天才看懂。。对于一个n*m矩阵,问是否存在N个数a1,a2,...an,和M个数b1,b2,...bm满足第i行的乘以ai,第j列的除以bj,每个元素都操作完后的结果都在[L,U]内,如果在则输出YES,否则输出NO。其实就是说是否存在a1,a2...an,以及b1,b2...bm满足L <= Cij * ai / bj <= U。

分析:对于这个约束条件可以通过log形式转换为加减运算。最后利用spfa找出是否存在负环即可。一般判断负环是看一个点入队次数是否大于n,而这里我这样写会超时,所以搞了个大于2次也过了。。

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
using namespace std;
#define INF 1e9
#define MAXN 810
int N, M, cnt, head[MAXN], vis[MAXN];
double d[MAXN];
struct Edge
{
    int to, next;
    double cost;
}e[320010];

void add(int u, int v, double w)
{
    e[cnt].to = v;
    e[cnt].cost = w;
    e[cnt].next = head[u]; //邻接表也可以用vector写
    head[u] = cnt++;
}

bool SPFA()
{
    queue<int> q;
    int times[MAXN];
    for(int i=1; i<=N+M; i++)
    {
        times[i] = 0; d[i] = INF;
        q.push(i); vis[i] = 1; //将所有点都先放入队列,保证图的连通性(如存在负环就一定能找出)
    }
    while(!q.empty())
    {
        int u = q.front(); q.pop(); vis[u] = 0;
        for(int i=head[u]; i!=-1; i=e[i].next)
        {
            int v = e[i].to;
            if(d[u]+e[i].cost < d[v])
            {
                d[v] = d[u]+e[i].cost;
                if(!vis[v])
                {
                    vis[v] = 1;
                    if(++times[v] > 2) return 0; //判断入队次数
                    q.push(v);                  //time[v]>N+M超时
                }
            }
        }
    }
    return 1;
}

int main()
{
    double L, U, C;

    while(~scanf("%d%d%lf%lf", &N,&M,&L,&U))
    {
        cnt = 0;
        memset(head, -1, sizeof(head));
        for(int i=1; i<=N; i++)
        {
            for(int j=1; j<=M; j++)
            {
                scanf("%lf", &C);
                add(i,j+N,log(C/L));
                add(j+N,i,log(U/C));
            }
        }
        if(SPFA()) puts("YES");
        else puts("NO");
    }
    return 0;
}

如果有起点,终点的约束,起点d[]距离就赋值为0,其余赋值为无穷。而对于没有起点,终点的约束,全部d[]距离都赋值为无穷。spfa算法的话要把所有点一开始都入队,这样每个点都遍历到了,就能保证不会有负环由于图的不连通而不被找到。这些都是最近做的几道差分约束系统的题目的总结吧。。发现差分约束还是挺有意思的,能把所有约束条件转换成边求最短路,判断负环之类的来解决问题。。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值