• POJ 3169 Layout


    Layout
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 13261   Accepted: 6361

    Description

    Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they are numbered, and since they can be rather pushy, it is possible that two or more cows can line up at exactly the same location (that is, if we think of each cow as being located at some coordinate on a number line, then it is possible for two or more cows to share the same coordinate). 

    Some cows 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 ML (1 <= ML <= 10,000) constraints describes which cows like each other and the maximum distance by which they may be separated; a subsequent list of MD constraints (1 <= MD <= 10,000) tells which cows 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 cow 1 and cow N that satisfies the distance constraints.

    Input

    Line 1: Three space-separated integers: N, ML, and MD. 

    Lines 2..ML+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at most D (1 <= D <= 1,000,000) apart. 

    Lines ML+2..ML+MD+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at least D (1 <= D <= 1,000,000) apart.

    Output

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

    Sample Input

    4 2 1
    1 3 10
    2 4 20
    2 3 3

    Sample Output

    27

    Hint

    Explanation of the sample: 

    There are 4 cows. Cows #1 and #3 must be no more than 10 units apart, cows #2 and #4 must be no more than 20 units apart, and cows #2 and #3 dislike each other and must be no fewer than 3 units apart. 

    The best layout, in terms of coordinates on a number line, is to put cow #1 at 0, cow #2 at 7, cow #3 at 10, and cow #4 at 27.
     
    题意:
    •       N个人编号为1-N,并且按照编号顺序排成一条直线,两个人的位置可能重合,然后给定一些约束条件。
    •       X(X <= 100000)组约束Ax Bx Cx(1 <= Ax < Bx <= N),表示Ax和Bx的距离不能大于Cx。
    •       Y(X <= 100000)组约束Ay By Cy(1 <= Ay < By <= N),表示Ay和By的距离不能小于Cy。
    •       如果这样的排列存在,输出1-N这两个人的最长可能距离,如果不存在,输出-1,如果无限长输出-2。
    题解:
    求最大解转换成最短路 形如 ai - bi <= ki 的方程组
    注意是按顺序排的,所以有ai - ai-1 >= 0 即 ai-1 - ai <= 0
    #include<cstdio>
    #include<iostream>
    #include<cstdlib>
    #include<queue>
    using namespace std;
    const int maxn = 1005,maxm = 40005, inf = 1000000008;
    int head[maxn],dis[maxn],vis[maxn],N,Ml,Nl,tot;
    bool inq[maxn];
    struct edge{
        int to, nxt, w;
    }G[maxm];
    void add(int u,int v,int w){
        G[++tot].nxt = head[u];
        G[tot].to = v;
        G[tot].w = w;
        head[u] = tot;
    }
    int SPFA(){
        queue <int> Q;
        for(int i = 1; i <= N; i++)dis[i] = inf;
        Q.push(1);
        inq[1] = 1;
        vis[1]++;
        dis[1] = 0;
        while(!Q.empty()){
            int u = Q.front();
            Q.pop();
            inq[u] = 0;
            
            for(int i = head[u]; i; i = G[i].nxt){
                int v = G[i].to;
                if(dis[v] > dis[u] + G[i].w){
                    dis[v] = dis[u] + G[i].w;                                
                    if(!inq[v]){
                        if(++vis[v] > N)return 0;//入队时判负环
                        Q.push(v);
                        inq[v] = 1;
                    }
                }
            }
        }
        if(dis[N] == inf)return 2;
        return 1;
        
        
        
    }
    int main(){
        scanf("%d%d%d",&N,&Ml,&Nl);
        for(int i = 1; i <= N; i++)
            add(i, i-1, 0);
        for(int i = 1; i <= Ml; i++){
            int u, v, w;
            scanf("%d%d%d",&u,&v,&w);
            add(u, v, w);
        }
        for(int j = 1; j <= Nl; j++){
            int u, v, w;
            scanf("%d%d%d",&u,&v,&w);
            add(v, u, -w);
        }
        int p = SPFA();
        if(!p)cout<<"-1"<<endl;
        if(p == 1)cout<<dis[N]<<endl;
        if(p == 2)cout<<"-2"<<endl;
    }

    差束约分求最大解跑最小路,求最小解跑最长路,是反的,一定不要搞反了啊

     
     
  • 相关阅读:
    开发中遇到的问题点
    ThreadLocal
    Java多线程10:join()方法
    Java多线程9:中断机制
    Java多线程8:wait()和notify()/notifyAll()
    Java多线程7:死锁
    Java多线程0:核心理论
    MySQL中and和or的优先级的问题
    Java多线程6:Synchronized锁代码块(this和任意对象)
    shiro英语
  • 原文地址:https://www.cnblogs.com/EdSheeran/p/8467354.html
Copyright © 2020-2023  润新知