HDU 3666 差分約束+判斷有沒有負環

//
//  main.cpp
//  HDU 3666 差分約束
//
//  Created by 鄭喆君 on 8/8/14.
//  Copyright (c) 2014 itcast. All rights reserved.
//

#include<cstring>
#include<iostream>
#include<iomanip>
#include<queue>
#include<cmath>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int int_max = 0x07777777;
const int int_min = 0x80000000;
const int maxn = 1000;
struct Edge {
    int to, next;
    double dist;
}es[410*410*2];
int head[maxn], vis[maxn],ccnt;
double d[maxn];
int n,m;
double U,L;
int cnt[maxn];
void addedge (int from, int to, double dist){
    es[ccnt].to = to;
    es[ccnt].dist = dist;
    es[ccnt].next = head[from];
    head[from] = ccnt++;
}
bool negativeCycle (){
    queue<int> q;
    int limit=(int)sqrt(1.0*(n+m));
    memset(vis, 0, sizeof(vis));
    memset(cnt, 0, sizeof(cnt));
    for(int i = 1; i <= n+m; i++) {d[i] = 0; vis[i] = true; q.push(i);}
    while(!q.empty()){
        int u = q.front();
        q.pop();
        vis[u] = false;
        for(int i = head[u]; i!=-1; i=es[i].next){
            if(d[es[i].to] > d[u]+es[i].dist){
                d[es[i].to] = d[u]+es[i].dist;
                if(!vis[es[i].to]){
                    q.push(es[i].to);
                    vis[es[i].to] = true;
                    if(++cnt[es[i].to] > limit) return true;
                }
            }
        }
    }
    return false;
}
int main(int argc, const char * argv[])
{
    while (scanf("%d %d %lf %lf", &n, &m, &L, &U)!=EOF) {
        memset(head, -1, sizeof(head));
        ccnt = 0;
        double x;
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= m; j++){
                scanf("%lf", &x);
                addedge(n+j, i, log(U/x));
                addedge(i, n+j, log(x/L));
            }
        if(negativeCycle()) cout << "NO" << endl;
        else cout << "YES" << endl;
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章