zoukankan      html  css  js  c++  java
  • hdu 3666 THE MATRIX PROBLEM

    差分约束系统。

    根据题意,可以写出不等式 L <= (Xij * Ai) / Bj <= U

    即 Ai/Bj<=U/Xij和Ai/Bj>=L/Xij

    由于差分约束系统是减法。。除法变减法可以用对数来解决。

    两个式子两边取对数,可以写成log(Ai)-log(Bj)<=log(U/Xij)和log(Ai)-log(Bj)>=log(L/Xij)

    log(Ai)和log(Bj)看作两个节点。编号分别为i和n+j,建立有向图,判断有没有负环存在。

     if(summ[hh]>4){jieguo=0;break;} 这个常数4是看了别人的博客水过去的,还可以用sqrt(入队次数)水过去。。。。正确AC姿势目前还没找到。

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<queue>
    #include<vector>
    #include<algorithm>
    using namespace std;
    
    const int maxn=405;
    const int INF=0x7fffffff;
    int jz[maxn][maxn],tott,jieguo,mm;
    struct abc
    {
        int startt;
        int endd;
        double costt;
    } node[2*maxn*maxn];
    vector<abc>ljb[maxn+maxn];
    int ff[maxn+maxn],summ[maxn+maxn];
    double dist[maxn+maxn];
    
    void spfa()
    {
        queue<int>Q;
        while(!Q.empty()) Q.pop();
        int i;
        for(i=0; i<=mm; i++) dist[i]=INF;
        memset(ff,0,sizeof(ff));
        memset(summ,0,sizeof(summ));
        dist[0]=0;
        ff[0]=1;
        Q.push(0);
        while(!Q.empty())
        {
            int hh=Q.front();
            Q.pop();
            summ[hh]++;
            if(summ[hh]>4)
            {
                jieguo=0;
                break;
            }
            ff[hh]=0;
            for(i=0; i<ljb[hh].size(); i++)
            {
                abc noww;
                noww=ljb[hh][i];
                if(dist[hh]+noww.costt<dist[noww.endd])
                {
                    dist[noww.endd]=dist[hh]+noww.costt;
                    if(ff[noww.endd]==0)
                    {
                        ff[noww.endd]=1;
                        Q.push(noww.endd);
                    }
                }
            }
        }
    }
    int main()
    {
        int n,m,L,U,i,j;
        while(~scanf("%d%d%d%d",&n,&m,&L,&U))
        {
            tott=1;
            jieguo=1;
            mm=n+m;
            for(i=0; i<=mm; i++) ljb[i].clear();
            for(i=0; i<n; i++)
                for(j=0; j<m; j++)
                    scanf("%d",&jz[i][j]);
            for(i=0; i<n; i++)
            {
                for(j=0; j<m; j++)
                {
                    node[tott].startt=j+n;
                    node[tott].endd=i;
                    node[tott].costt=log(1.0*U/jz[i][j]);
                    ljb[j+n].push_back(node[tott]);
                    tott++;
                    node[tott].startt=i;
                    node[tott].endd=j+n;
                    node[tott].costt=-log(1.0*L/jz[i][j]);
                    ljb[i].push_back(node[tott]);
                    tott++;
                }
            }
            spfa();
            if(jieguo==1) printf("YES
    ");
            else printf("NO
    ");
        }
        return 0;
    }
  • 相关阅读:
    hdu 2680:Choose the best route(Dijkstra , SPFA)
    Gym
    Gym
    Gym
    常用的相似度计算
    kafka 配置
    Kafka-broker配置说明
    kafka 安装以及测试
    adaboost算法
    solr scheme配置简介
  • 原文地址:https://www.cnblogs.com/zufezzt/p/4565759.html
Copyright © 2011-2022 走看看