码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 3666 THE MATRIX PROBLEM

时间:2015-06-10 14:00:32      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:

差分约束系统。

根据题意,可以写出不等式 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\n");
        else printf("NO\n");
    }
    return 0;
}

 

hdu 3666 THE MATRIX PROBLEM

标签:

原文地址:http://www.cnblogs.com/zufezzt/p/4565759.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!