zoukankan      html  css  js  c++  java
  • poj 3169 Layout

    Layout

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
    Total Submission(s) : 4   Accepted Submission(s) : 1
    Problem 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. <br> <br>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. <br> <br>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
     

    题目大意:

    n头奶牛按1到n排好序,md个限制及mt个限制,md行表示奶牛A与奶牛B相差最多D,mt个限制奶牛A与奶牛B相差最少D,问你奶牛1与奶牛n最多相差多少?

    解题思路:

    限制条件 : 

    1、相邻奶牛之间,编号大的距离大于编号小的,即 dist[1]-dist[2]<=0,dist[2]-dist[3]<=0,dist[3]-dist[4]<=0。。。。dist[n-1]-dist[n]<=0

    2、md个限制 A与奶牛B相差最多D,dist[B]-dist[A]<=D

    3、mt个限制奶牛A与奶牛B相差最少D,dist[B]-dist[A]>=D ,即 dist[B]-dist[A]<=D

    v-u<=c,即添加 u->v=c 的单向边

    有了这些元素,就可以用差分约束来解了

    初始化dis数组为什么不能memset全部???

     1 #include<iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <string>
     5 #include <cstdio>
     6 #define inf 0x3f3f3f3f
     7 using namespace std;
     8 int n, ml, md;
     9 long long d[1005];
    10 struct node
    11 {
    12     int u, v;
    13     long long w;
    14 }e[30005];
    15 int num=1;
    16 void add(int x, int y, int z)
    17 {
    18     e[num].u = x;
    19     e[num].v = y;
    20     e[num++].w = z;
    21 }
    22 bool bellman_ford()//单源
    23 {
    24     int i,j;
    25     /*memset(d, inf, sizeof(d));*///不知道为什么不能这样初始化,气死
    26     for (i = 1; i <= n; i++)
    27     {
    28         d[i] = inf;
    29     }
    30     d[1] = 0;
    31     for (i = 1; i <= n - 1; i++)
    32     {
    33         bool f =0;
    34         for (j = 1; j <num; j++)//遍历每条边
    35         {
    36             int u = e[j].u;
    37             int v = e[j].v;
    38             int w = e[j].w;
    39             if (d[u] + w < d[v])
    40             {
    41                 d[v] = d[u] + w;
    42                 f = 1;
    43             }
    44         }
    45         if (f == 0) break;//没更新则结束
    46     }
    47     for (i = 1; i <num; i++)
    48     {
    49         if (d[e[i].u] + e[i].w < d[e[i].v])
    50         {
    51             return 0;//还在更新说明存在负环
    52         }
    53     }
    54     return 1;
    55 }
    56 
    57 int main()
    58 {
    59     scanf("%d %d %d", &n, &ml, &md);
    60     int i;
    61     for (i = 1; i <= ml; i++)
    62     {
    63         int s, t, d;
    64         scanf("%d%d%d", &s, &t, &d);
    65         add(s, t, d);
    66     }
    67     for (i = 1; i <= md; i++)
    68     {
    69         int s, t, d;
    70         scanf("%d%d%d", &s, &t, &d);
    71         add(t, s, -d);
    72     }
    73     for (i = 1; i <= n - 1; i++)
    74     {
    75         add(i + 1, i, 0);
    76     }
    77     bool sign = bellman_ford();
    78     if (sign)
    79     {
    80         if (d[n] == inf)//最远是无穷大,可以是任意解都可以
    81         {
    82             printf("-2
    ");
    83         }
    84         else
    85         {
    86             printf("%lld
    ", d[n]);
    87         }
    88     }
    89     else//存在负环,最短路无限小,无解,最大值不存在
    90         printf("-1
    ");
    91     return 0;
    92 }
  • 相关阅读:
    [poj] 3068 "Shortest" pair of paths || 最小费用最大流
    [poj] 3686 The Windy's || 最小费用最大流
    [poj] 1235 Farm Tour || 最小费用最大流
    [poj] 3281 Dining || 最大流
    [poj] 3041 Asteroids || 最小点覆盖=最大二分图匹配
    luogu P1072 Hankson 的趣味题
    二分图最佳匹配
    181106 solution
    luogu P2216 [HAOI2007]理想的正方形
    luogu P4362 [NOI2002]贪吃的九头龙
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/8439299.html
Copyright © 2011-2022 走看看