POJ—3169 Layout (差分约束,SPFA实现)

Layout

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 16036 Accepted: 7704

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

USACO 2005 December Gold

题目链接:http://poj.org/problem?id=3169

这道题目是从挑战程序设计竞赛上看来的,当时看了好久才看懂,其实这道题用的算法并不难实现,只是理解起来比较困难,首先题目给出的约束条件有:(1)牛是按照编号顺序排列的,所以有d[i]<=d[i+1]成立。(2)对于每对关系好的牛之间有最大距离限制,都有d[AL]+DL<=d[BL](AL BL DL为题目给出的数据,意为牛AL与BL最大距离不超过DL)。(3)对于每对关系不好的牛之间有最小的距离限制,都有d[AD]+DD>=d[BD]。因此,原问题可以转化为在满足这三类不等式的情况下,求解d的d[n]-d[1]的最大值的问题。这是线性规划问题,可以通过运筹学中的单纯形法等较复杂的算法求解。

但是这题有更简单的解法,这些不等式的特点是所有的式子两边都只出现了一个变量(这种特殊形式的不等式方程组又叫做差分约束系统)。实际上,图上的最短路径问题也可以用这样的形式表示出来。记从起点s出发,到各个顶点v的最短距离为d(v)。因此,对于每条权值为w的边e=(u,v),都有d(u)+w>=d(v)成立。反之,在满足全部这些约束不等式的d中,d(v)-d(s)的最大值就是从s到v的最短路径。需要注意这里不是最小值,而是最大值对应着最短路径。

把原来的问题和最短路问题进行比较就可以发现,两个问题的形式是完全一样的。也就是说,可以把原来的问题的每一个约束条件对形成图中的一条边来绘制出一张图,然后通过解决图上的最短路问题来求得原问题的答案。首先把顶点编号为1~N,d[i]<=d[i+1]变形为d[i+1]+0>=d[i],因此从顶点i+1向顶点i连接一条长度为0的边。同理d[AL]+DL>=d[BL]对应从顶点AL向顶点BL连接一条长度为DL的边,d[AD]+DD<=d[BD]对应从顶点BD向顶点AD连接一条长度为-DD的边。所求的问题是d[N]-d[1]的最大值,对应为顶点1到顶点N的最短路径。由于图中有负权,所以可以考虑用Bellman-Ford算法求解。

#include <iostream>
#include <queue>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
const int MAX=1e4+5;
const int inf=(1<<31)-1;
vector<pair<int,int> > edge[1010];
bool book[1010];
int vis[1010];
bool spfa(int x,int n,int dis[])
{
    for(int i=1;i<=n;i++)
        dis[i]=inf;
    queue<int> q;
    dis[x]=0;
    q.push(x);
    while(!q.empty()){
        int now=q.front();
        q.pop();
        vis[now]++;
        if(vis[now]>=n)return false;
        book[now]=false;
        for(int i=0;i<edge[now].size();i++){
            if(dis[edge[now][i].first]>dis[now]+edge[now][i].second){
                dis[edge[now][i].first]=dis[now]+edge[now][i].second;
                if(!book[edge[now][i].first]){
                    q.push(edge[now][i].first);
                    book[edge[now][i].first]=true;
                }
            }
        }
    }
    return true;
}

int main()
{
    int n,dl,dd,x,y,v;
    int dis[1010];
    cin>>n>>dl>>dd;
    for(int i=1;i<=dl;i++){
        cin>>x>>y>>v;
        edge[x].push_back(make_pair(y,v));
    }
    for(int i=1;i<=dd;i++){
        cin>>x>>y>>v;
        edge[y].push_back(make_pair(x,-v));
    }
    for(int i=2;i<=n;i++){
        edge[i].push_back(make_pair(i-1,0));
    }
    if(spfa(1,n,dis)){
        if(dis[n]<inf)cout<<dis[n]<<endl;
        else cout<<-2<<endl;
    }else{
        cout<<-1<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值