• hdu 3666(差分约束,手动栈解决超时问题)


    THE MATRIX PROBLEM

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


    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 6 2 3 4 8 2 6 5 2 9
     
    Sample Output
    YES
     
    Source
     
    题意:给出一个矩阵 C 问是否存在这样两个序列使得  l <= cij * ai/bj <= r 对这个矩阵成立。
    题解:将cij除过去,然后取个对数, log(l/cij) <= log(ai) - log(bj) <= log(r/cij) 这样的话就可以化为一个差分约束的题了,但是这个题有个问题就是判环会超时,有人提出可以只判断sqrt(n)次就行了,,但是无法证明,这个题的更好的方法是用一个手动栈代替队列.这样的话就能够快速判环了,给出两份代码..
    只用sqrt(n)的队列写法:
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    #include<math.h>
    using namespace std;
    const double INF = 999999999;
    const int N = 805;
    const int M = 800000;
    struct Edge{
        int v,next;
        double w;
    }edge[M];
    int head[N],tot;
    int n,m,l,r;
    void init(){
        memset(head,-1,sizeof(head));
        tot = 0;
    }
    void addEdge(int u,int v,double w,int &k){
        edge[k].v =v ,edge[k].w = w ,edge[k].next = head[u],head[u] = k++;
    }
    double low[N];
    int time[N];
    bool vis[N];
    bool spfa(int s){
        for(int i=0;i<=n+m;i++){
            vis[i] = false;
            time[i] = 0;
            low[i] = INF;
        }
        int num = ((int)sqrt(n+m))+1;
        low[s] = 0;
        time[s]++;
        queue<int>q;
        q.push(s);
        while(!q.empty()){
            int u = q.front();
            q.pop();
            vis[u] = false;
            for(int k=head[u];k!=-1;k=edge[k].next){
                int v = edge[k].v;
                double w = edge[k].w;
                if(low[v]>low[u]+w){
                    low[v] = low[u]+w;
                    if(!vis[v]){
                        vis[v] = true;
                        q.push(v);
                        time[v]++;
                        if(time[v]>num) return false;
                    }
                }
            }
        }
        return true;
    }
    int main()
    {
        double c;
        while(scanf("%d%d%d%d",&n,&m,&l,&r)!=EOF){
            init();
            for(int i=1;i<=n;i++){
                for(int j=1;j<=m;j++){
                    scanf("%lf",&c);
                    addEdge(i,n+j,-log(l/c),tot);
                    addEdge(n+j,i,log(r/c),tot);
                }
            }
            for(int i=1;i<=n+m;i++){
                addEdge(0,i,0,tot);
            }
            if(spfa(0)) printf("YES
    ");
            else printf("NO
    ");
        }
        return 0;
    }

    手动栈解决:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    #include<math.h>
    using namespace std;
    const double INF = 999999999;
    const int N = 805;
    const int M = 800000;
    struct Edge{
        int v,next;
        double w;
    }edge[M];
    int head[N],tot;
    int n,m,l,r;
    void init(){
        memset(head,-1,sizeof(head));
        tot = 0;
    }
    void addEdge(int u,int v,double w,int &k){
        edge[k].v =v ,edge[k].w = w ,edge[k].next = head[u],head[u] = k++;
    }
    double low[N];
    int time[N];
    bool vis[N];
    int stk[N*N];
    bool spfa(int s){
        for(int i=0;i<=n+m;i++){
            vis[i] = false;
            time[i] = 0;
            low[i] = INF;
        }
        int top = 0;
        low[s] = 0;
        time[s]++;
        stk[top++] = s;
        while(top!=0){
            int u = stk[--top];
            vis[u] = false;
            for(int k=head[u];k!=-1;k=edge[k].next){
                int v = edge[k].v;
                double w = edge[k].w;
                if(low[v]>low[u]+w){
                    low[v] = low[u]+w;
                    if(!vis[v]){
                        vis[v] = true;
                        stk[top++] = v;
                        time[v]++;
                        if(time[v]>n+m) return false;
                    }
                }
            }
        }
        return true;
    }
    int main()
    {
        double c;
        while(scanf("%d%d%d%d",&n,&m,&l,&r)!=EOF){
            init();
            for(int i=1;i<=n;i++){
                for(int j=1;j<=m;j++){
                    scanf("%lf",&c);
                    addEdge(i,n+j,-log(l/c),tot);
                    addEdge(n+j,i,log(r/c),tot);
                }
            }
            for(int i=1;i<=n+m;i++){
                addEdge(0,i,0,tot);
            }
            if(spfa(0)) printf("YES
    ");
            else printf("NO
    ");
        }
        return 0;
    }
  • 相关阅读:
    插入数据失败提示: Setting autocommit to false on JDBC Connection 自动提交失败
    MyBatis XML配置properties
    mybatis 测试输出SQL语句到控制台配置
    原创:mysql5 还原至mysql 8.0.11数据库链接配置提示错误(修改内容有三处
    idea 快捷键汇总
    maven依赖配置和依赖范围
    pom.xml 配置 收藏
    单词的提取
    UVA10815 安迪的第一个字典 Andy's First Dictionary
    UVA11054 Gergovia的酒交易 Wine trading in Gergovia
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5699125.html
Copyright © 2020-2023  润新知