zoukankan      html  css  js  c++  java
  • POJ3169 Layout

    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
    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

    正解:差分约束系统+SPFA

    解题报告:

         大概题意是给定排成一列的牛,然后两头牛之间的距离可能要大于等于某个值或者小于等于某个值,问是否存在或者终点是否可以无限远。

      以前在codevs上面做过一道差分约束系统的题,然后就学会了这种神奇的思想。其实思想很简单,结合图论的话还是很有用的。

      考虑题意,需要求1到n的最大距离。题目中给了很多限制条件,比如说x2-x1<=3,x4-x2>=6这样的条件。我们考虑像x2-x1<=3这样的条件,因为我们想让距离尽可能大,就要使距离最大化,然后建图,1向2连一条权值为3的边。那么像x4-x2>=6这样大于的怎么办呢,我们就可以把它变成x2-x4<=-6,边权为负即可。然后图上跑SPFA。

      接着是个很重要的问题,是最短路还是最长路呢?按理说要想距离大应该跑最长路,但是我们想,我们这个图是怎么建的,根据每个条件的最大条件连边,那么说明我们肯定要取所有对这个点的约束中最小的那个(取交),所以只会越来越小。不难想到,最后求出的dis[n]就是我们要求的。

         题意中的-1、-2怎么特判呢?如果有负权环就说明不可能,记一下每个点入队n次就说明有负权环。而可以无限大则说明还到不了n,则说明dis[n]仍为初值

         轻松AC,代码如下:

     1 //It is made by jump~
     2 #include <iostream>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <cstdio>
     6 #include <cmath>
     7 #include <algorithm>
     8 #include <ctime>
     9 #include <vector>
    10 #include <queue>
    11 #include <map>
    12 #ifdef WIN32   
    13 #define OT "%I64d"
    14 #else
    15 #define OT "%lld"
    16 #endif
    17 using namespace std;
    18 typedef long long LL;
    19 const int MAXN = 100011;
    20 const int MAXM = 400011;
    21 const int inf = (1<<30);
    22 int n,m1,m2;
    23 int first[MAXN],to[MAXM],next[MAXM],w[MAXM],ecnt;
    24 int dis[MAXN];
    25 queue<int>Q;
    26 bool pd[MAXN];
    27 int cnt[MAXN];
    28 
    29 inline int getint()
    30 {
    31        int w=0,q=0;
    32        char c=getchar();
    33        while((c<'0' || c>'9') && c!='-') c=getchar();
    34        if (c=='-')  q=1, c=getchar();
    35        while (c>='0' && c<='9') w=w*10+c-'0', c=getchar();
    36        return q ? -w : w;
    37 }
    38 
    39 inline void link(int x,int y,int z){
    40     next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y; w[ecnt]=z;
    41 }
    42 
    43 inline bool spfa(){
    44     Q.push(1); pd[1]=1;
    45     for(int i=2;i<=n;i++) dis[i]=inf;
    46     while(!Q.empty()){
    47     int u=Q.front(); Q.pop(); pd[u]=0;
    48     for(int i=first[u];i;i=next[i]) {
    49         int v=to[i];
    50         if(dis[v]>dis[u]+w[i]) { 
    51         dis[v]=dis[u]+w[i];
    52         if(!pd[v]) {
    53             Q.push(v);
    54             pd[v]=1;
    55             cnt[v]++;
    56             if(cnt[v]>=n) return false;
    57         }
    58         }
    59     }
    60     }
    61     if(dis[n]==inf) printf("-2");
    62     else printf("%d",dis[n]);
    63     return true;
    64 }
    65 
    66 inline void solve(){
    67     n=getint(); m1=getint(); m2=getint();
    68     int x,y,z;
    69     for(int i=1;i<=m1;i++) {
    70     x=getint();y=getint();z=getint();
    71     link(x,y,z);
    72     }
    73     for(int i=1;i<=m2;i++) {
    74     x=getint(); y=getint(); z=getint();
    75     link(y,x,-z);
    76     }
    77     if(!spfa()) printf("-1");
    78 }
    79 
    80 int main()
    81 {
    82   solve();
    83   return 0;
    84 }
  • 相关阅读:
    生活不仅有诗远方,还有身体和枸杞
    星空
    住进布达拉宫,我是雪域最大的王。流浪在拉萨街头,我是世间最美的情郎。
    安得与君相决绝,免教生死作相思
    黑马程序员-传智健康项目资料
    黑马程序员-传智健康项目(第十三章)
    黑马程序员-传智健康项目(第十二章)
    黑马程序员-传智健康项目(第十一章)
    黑马程序员-传智健康项目(第十章)
    黑马程序员-传智健康项目(第九章)
  • 原文地址:https://www.cnblogs.com/ljh2000-jump/p/5540630.html
Copyright © 2011-2022 走看看