zoukankan      html  css  js  c++  java
  • Hdu 1259 World Exhibition(差分约束)

    hud 1259

    World Exhibition

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2168 Accepted Submission(s): 1069

    Problem Description

    Nowadays, many people want to go to Shanghai to visit the World Exhibition. So there are always a lot of people who are standing along a straight line waiting for entering. Assume that there are N (2 <= N <= 1,000) people numbered 1..N who are standing in the same order as they are numbered. It is possible that two or more person line up at exactly the same location in the condition that those visit it in a group.

    There is something interesting. Some like each other and want to be within a certain distance of each other in line. Some really dislike each other and want to be separated by at least a certain distance. A list of X (1 <= X <= 10,000) constraints describes which person like each other and the maximum distance by which they may be separated; a subsequent list of Y constraints (1 <= Y <= 10,000) tells which person dislike each other and the minimum distance by which they must be separated.

    Your job is to compute, if possible, the maximum possible distance between person 1 and person N that satisfies the distance constraints.

    Input

    First line: An integer T represents the case of test.

    The next line: Three space-separated integers: N, X, and Y.

    The next X lines: Each line contains three space-separated positive integers: A, B, and C, with 1 <= A < B <= N. Person A and B must be at most C (1 <= C <= 1,000,000) apart.

    The next Y lines: Each line contains three space-separated positive integers: A, B, and C, with 1 <= A < B <= C. Person A and B must be at least C (1 <= C <= 1,000,000) apart.

    Output

    For each line: A single integer. If no line-up is possible, output -1. If person 1 and N can be arbitrarily far apart, output -2. Otherwise output the greatest possible distance between person 1 and N.

    Sample Input

    1
    4 2 1
    1 3 8
    2 4 15
    2 3 4

    Sample Output

    19
    思路:差分约束

    AC code
    #include<bits/stdc++.h>
    using namespace std;
    const int N = 1e6+10;
    const int inf = 0x3f3f3f3f;
    struct node{
        int u, v, w, nxt;
    }edge[N];
    int n, m1, m2, cnt;
    int fir[N], dis[N], num[N];
    bool vis[N];
    inline void built(int u, int v, int w){
        edge[cnt] = (node){u, v, w, fir[u]};
        fir[u] = cnt++;
    }
    inline int spfa(int st){
        memset(vis, false, sizeof(vis));
        vis[st] = true;
        memset(num, 0, sizeof(num));
        num[st]++;
        for(int i = 1; i <= n; i++){
            dis[i] = inf;
        }
        dis[st] = 0;
        queue<int> Q;
        Q.push(st);
        while(!Q.empty()){
            int u = Q.front();
            Q.pop();
            vis[u] = false;
            for(int i = fir[u]; i; i = edge[i].nxt){
                int v = edge[i].v;
                if(dis[v] > dis[u] + edge[i].w){
                    dis[v] = dis[u] + edge[i].w;
                    if(!vis[v]){
                        vis[v] = true;
                        Q.push(v);
                        num[v]++;
                        if(num[v] >= n)
                            return -1;
                    }
                }
            }
        }
        if(dis[n] == inf)
            return -2;
        return dis[n];
    }
    int main(){
        #ifdef ONLINE_JUDGE
        #else
            freopen("in.txt", "r", stdin);
        #endif // ONLINE_JUDGE
        int t, a, b, c;
        scanf("%d", &t);
        while(t--){
            scanf("%d%d%d", &n, &m1, &m2);
            cnt = 1;
            memset(fir, 0, sizeof(fir));
            for(int i = 0; i < m1; i++){
                scanf("%d%d%d", &a, &b, &c);
                built(a, b, c);
            }
            for(int i = 0; i < m2; i++){
                scanf("%d%d%d", &a, &b, &c);
                built(b, a, -c);
            }
            int ans = spfa(1);
            printf("%d
    ", ans);
    //        for(int i = 1; i <= n; i++){
    //            printf("%d ", dis[i]);
    //        }
    //        printf("
    ");
    //        for(int i = 1; i < cnt; i++){
    //            printf("%d %d %d
    ", edge[i].u, edge[i].v, edge[i].w);
    //        }
        }
        return 0;
    }
    
    
  • 相关阅读:
    ntp
    mknod
    timeout/timelimit
    Eclipse 包变成文件夹
    Eclipse 包变成文件夹
    Java生成随机字符串和随即生成10以内的字符串
    Java生成随机字符串和随即生成10以内的字符串
    插入排序
    插入排序
    Java 中基本类型和包装类之间的转换
  • 原文地址:https://www.cnblogs.com/kun-/p/10024366.html
Copyright © 2011-2022 走看看