zoukankan      html  css  js  c++  java
  • POJ3169 Layout

    Time Limit: 1000MS

        Memory Limit: 65536K
    Total Submissions: 10059   Accepted: 4835

    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

     
     
    差分约束。
     
     1 /**/
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<cmath>
     5 #include<cstring>
     6 #include<algorithm>
     7 #include<queue>
     8 using namespace std;
     9 const int INF=0x3f3f3f3f;
    10 const int mxn=2000;
    11 struct edge{
    12     int v,dis;
    13     int next;
    14 }e[mxn*10];
    15 int hd[mxn],cnt;
    16 int dis[mxn];
    17 int vis[mxn];
    18 int inq[mxn];
    19 int n,ml,md;
    20 void add_edge(int u,int v,int w){
    21     e[++cnt].next=hd[u];e[cnt].v=v;e[cnt].dis=w;hd[u]=cnt;
    22 }
    23 bool SPFA(){
    24     memset(dis,0x3f,sizeof dis);
    25     queue<int>q;
    26     q.push(1);
    27     inq[1]=1;
    28     vis[1]=1;
    29     dis[1]=0;
    30     while(!q.empty()){
    31         int u=q.front();
    32         for(int i=hd[u];i;i=e[i].next){
    33             int v=e[i].v;
    34             if(dis[u]+e[i].dis<dis[v]){
    35                 dis[v]=dis[u]+e[i].dis;
    36                 vis[v]++;
    37                 if(vis[v]>n)return 0;
    38                 if(!inq[v]){
    39                     inq[v]=1;
    40                     q.push(v);
    41                 }
    42             }
    43         }
    44         q.pop();
    45         inq[u]=0;
    46     }
    47     return 1;
    48 }
    49 int main(){
    50     scanf("%d%d%d",&n,&ml,&md);
    51     int i,j;
    52     int a,b,c;
    53     for(i=1;i<=ml;i++){
    54         scanf("%d%d%d",&a,&b,&c);
    55         if(a>b)swap(a,b);
    56         add_edge(a,b,c);
    57     }
    58     for(i=1;i<=md;i++){
    59         scanf("%d%d%d",&a,&b,&c);
    60         if(a>b)swap(a,b);
    61         add_edge(b,a,-c);
    62     }
    63     if(!SPFA()){
    64         printf("-1
    ");return 0;
    65     }
    66     if(dis[n]==INF)    printf("-2
    ");
    67     else printf("%d
    ",dis[n]);
    68     return 0;
    69 }
     
  • 相关阅读:
    javaWeb服务详解【客户端调用】(含源代码,测试通过,注释) ——Emp实体类
    javaWeb服务详解【客户端调用】(含源代码,测试通过,注释) ——Dept实体类
    javaWeb服务详解【客户端调用】(含源代码,测试通过,注释) ——Dept实体类
    javaWeb服务详解(含源代码,测试通过,注释) ——web.xml
    hdu2044:一只小蜜蜂
    最大连续子序列和
    牛客练习赛20 F-填数字
    Attack City and Capture Territory & 取石子(博弈)
    畅通工程1
    和最大子序列
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/5737908.html
Copyright © 2011-2022 走看看