zoukankan      html  css  js  c++  java
  • HDU-3592 World Exhibition

    题目大意:

    N个人去看世博会,但是这N个人的关系不同所以希望站的序列也有一定要求,给出X个约束:A和B必须最多离开C距离,再给出Y个约束:A和B必须至少离开C距离,现在问你有没有这样的序列满足,有的话,输出1-n的最大距离,如果没有输出-1,如果有但是1-n的距离可以任意远,输出-2

    解题思路:

    差分约束

    代码:

    #include <queue>
    #include <cstdio>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    typedef struct node{
        int to, w;
        node(int a = 0, int b = 0){
            to = a; w = b;
        }
    }Edge;
    
    const int maxn = 1000 + 5;
    const int INF = 0x3f3f3f3f;
    
    vector<Edge> vec[maxn];
    int vis[maxn], dis[maxn], cnt[maxn];
    
    int spfa(int n){
        Edge v;
        int p, len;
        queue<int> q;
        while(!q.empty()) q.pop();
    
        for(int i = 0; i <= n; ++i){
            dis[i] = INF;
            vis[i] = 0;
            cnt[i] = 0;
        }
        q.push(n);
        cnt[n] = 1;
        vis[n] = 1;
        dis[n] = 0;
    
        while(!q.empty()){
            p = q.front(); q.pop();
            len = vec[p].size();
            for(int i = 0; i < len; ++i){
                v = vec[p][i];
                if(dis[v.to] > dis[p] + v.w){
                    dis[v.to] = dis[p] + v.w;
                    if(!vis[v.to]){
                        vis[v.to] = 1;
                        cnt[v.to] += 1;
                        if(cnt[v.to] >= n) return -1;
                        q.push(v.to);
                    }
                }
            }
            vis[p] = 0;
        }
        return dis[1] == INF ? -2 : dis[1];
    }
    int main(){
        int a, b, d, n, t, ml, md;
        scanf("%d", &t);
        while(t--){
            scanf("%d%d%d", &n, &ml, &md);
            for(int i = 0; i <= n; ++i) vec[i].clear();
            for(int i = 1; i < n; ++i){
                vec[i].push_back(node(i+1, 0));
            }
            for(int i = 0; i < ml; ++i){
                scanf("%d%d%d", &a, &b, &d);
                vec[b].push_back(node(a, d));
            }
            for(int i = 0; i < md; ++i){
                scanf("%d%d%d", &a, &b, &d);
                vec[a].push_back(node(b, -d));
            }
            printf("%d
    ", spfa(n));
        }
        return 0;
    }


  • 相关阅读:
    Ugly Numbers
    Present
    Out of Hay
    Stars(树状数组或线段树)
    Humble Numbers
    Wireless Network
    Rank List
    Argus
    食物链
    Antenna Placement
  • 原文地址:https://www.cnblogs.com/wiklvrain/p/8179424.html
Copyright © 2011-2022 走看看