zoukankan      html  css  js  c++  java
  • POJ 3169 Layout (差分约束)

    Layout
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 5190   Accepted: 2491

    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.

    Source

     
     
    题意:N头牛排队吃饭 排编号顺序排,大的永远在小的前面,但牛之间有的关系好,有的差,所以有的牛想离某些牛的距离最远不超过D 有的必须大于D 给出它们的关系 求第n头牛跟第一头的最远距离

    思路:差分问题,求最大值 约束条件转化为 < ; 所以有S大-S小 <= D1,
    S大-S小>=D2 把这个条件转化一下==> S小-S大<=-D2

    然后就是spfa了
     
     
    #include <stdio.h>
    #include <string.h>
    #define VM 10005
    #define EM 200500    //原先数据都是少一个0的,但RE 不知为什么 多加一个就过了
    #define inf 9999999999
    struct edge
    {
        int v,w,next;
    }e[EM];
    int head[VM],ep;
    void addedge (int cu,int cv,int cw)
    {
        ep ++;
        e[ep].v = cv;
        e[ep].w = cw;
        e[ep].next = head[cu];
        head[cu] = ep;
    }
    
    __int64 spfa (int n)
    {
        int vis[VM],cnt[VM],queue[VM];
        __int64 dis[VM];
        for (int i = 1;i <= n;i ++)
        {
            vis[i] = 0;
            cnt[i] = 0;
            dis[i] = inf;
        }
        dis[1] = 0;
        int front = -1,rear = 0;
        vis[1] = 1;
        cnt[1] = 1;
        queue[rear ++] = 1;
        while (front != rear)
        {
            front = (front+1)%(n+1);
            int u = queue[front];
            vis[u] = 0;
            for (int i = head[u];i != -1;i = e[i].next)
            {
                int v = e[i].v;
                if (dis[v] > dis[u]+e[i].w)
                {
                    dis[v] = dis[u] + e[i].w;
                    if (!vis[v])
                    {
                        vis[v] = 1;
                        cnt[v] ++;
                        if (cnt[v] >= n)
                            return 0;     //有负环输出-1
                        queue[rear] = v;
                        rear = (rear+1)%(n+1);
                    }
                }
            }
        }
       
        if (dis[n] == inf)
            return -1;    //不可达点
        else
            return dis[n];
    }
    int main ()
    {
        int n,ml,md;
        int v1,v2,cost;
        scanf ("%d%d%d",&n,&ml,&md);
        memset (head,-1,sizeof(head));
        ep = 0;
        while (ml --)
        {
            scanf ("%d%d%d",&v1,&v2,&cost);
            if (v1 < v2)            //编号大的牛永远在小的牛前面
                addedge (v1,v2,cost);
            else
                addedge (v2,v1,cost);
        }
        while (md --)
        {
            scanf ("%d%d%d",&v1,&v2,&cost);
            if (v2 < v1)
                addedge (v1,v2,-1*cost);
            else
                addedge (v2,v1,-1*cost);
        }
        __int64 ans = spfa (n);
        if (ans == 0)
            printf ("-1\n");
        else if (ans == -1)
            printf ("-2\n");
        else
            printf ("%I64d\n",ans);
        return 0;
    }
  • 相关阅读:
    spring boot 整合elasticsearch
    elasticsearch 高级查询
    elasticsearch 基本操作
    day9--回顾
    day9--多线程与多进程
    day9--paramiko模块
    day10--异步IO数据库队列缓存
    数据库连接池
    数据库事务的四大特性以及事务的隔离级别
    使用JDBC进行数据库的事务操作(2)
  • 原文地址:https://www.cnblogs.com/jackge/p/3062023.html
Copyright © 2011-2022 走看看