HDU 3666 (差分约束)

来源:互联网 发布:数据统计培训 编辑:程序博客网 时间:2024/05/22 09:42

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 CN*M, each element E of CN*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 62 3 48 2 65 2 9
 

Sample Output
YES
 

Source
2010 Asia Regional Harbin
 

题意:看了半天才看懂。。对于一个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 810int 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
原创粉丝点击