POJ3169-Layout

Layout

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6582   Accepted: 3182

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

裸的差分约束
主要是判断-1和-2的情况
当1到n的距离为无穷大的时候,说明可以选1到n的所有数
当存在负环的时候,则说明1到n的距离为无穷小,说明选不了任何点嘛!
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 1000+10;
const int INF = 1e9;
vector<pair<int,int> > g[maxn];
int dist[maxn];
int cnt[maxn];
queue<int>que;
bool inQue[maxn];
int src = 1;
int n,ml,md;
bool spfa(){
    memset(inQue,0,sizeof inQue);
    memset(cnt,0,sizeof cnt);
    for(int i = 1; i <= n; i++) dist[i] = INF;
    dist[src]= 0;
    que.push(src);
    inQue[src] = 1;
    while(!que.empty()){
        int u = que.front();
        que.pop();
        for(int i = 0; i < g[u].size(); i++){
            if(dist[g[u][i].first]>g[u][i].second+dist[u]){
                dist[g[u][i].first] = g[u][i].second+dist[u];
                if(!inQue[g[u][i].first]){
                    cnt[g[u][i].first]++;
                    if(cnt[g[u][i].first] > n)
                        return false;
                    inQue[g[u][i].first] = 1;
                    que.push(g[u][i].first);
                }
            }
        }
        inQue[u] = 0;
    }
    return true;
}
int main(){

    while(cin >> n >> ml >> md){
        for(int i = 1; i <= n; i++){
            g[i].clear();
        }
        int a,b,d;
        while(ml--){
            scanf("%d%d%d",&a,&b,&d);
            g[a].push_back(make_pair(b,d));
        }
        while(md--){
            scanf("%d%d%d",&a,&b,&d);
            g[b].push_back(make_pair(a,-d));
        }
        if(!spfa()) cout<<-1<<endl;
        else{
            if(dist[n]==INF){
                cout<<-2<<endl;
            }else{
                cout<<dist[n]<<endl;
            }

        }
    }

    return 0;
}

POJ3169-Layout,布布扣,bubuko.com

时间: 2024-11-04 13:47:52

POJ3169-Layout的相关文章

POJ3169 Layout【SPFA】【差分约束】

Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7608 Accepted: 3653 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 str

POJ3169 Layout 【差分约束系统】

Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7468   Accepted: 3581 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

POJ3169 Layout (差分约束系统)

传送门 题意:有N头牛,第i头牛一定在第i-1头和第i+1头中间(按照编号从小到大排列)给出两种约束 1. A 到 B 的距离不超过 D 2. A 到 B 的距离小于 D 要求第N头到第一头的距离最远.若无解,输出-1,若无限远,输出-2. 用dis(i)表示从第一头牛到第i头牛的距离,所以 -对于条件1有:dis(A) + D >= dis(B) 连一条从A到B的有向边权值为D(因为最大为D--) -对于条件2有:dis(A) + D <= dis(B),变形为dis(B) - D >

poj3169——Layout(差分约束+SPFA判断负环)

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 numbe

POJ3169:Layout(差分约束)

http://poj.org/problem?id=3169 题意: 一堆牛在一条直线上按编号站队,在同一位置可以有多头牛并列站在一起,但编号小的牛所占的位置不能超过编号大的牛所占的位置,这里用d[i]表示编 号为i的牛所处的位置,即要满足d[i]-d[i+1]<=0,同时每两头牛之间有以下两种关系(对于输入的a b d来说):             1>如果是喜欢关系:即需要满足d[b]-d[a]<=d             2>如果是讨厌关系:即需要满足d[b]-a[a]&

差分约束 刷题记录

把问题转化成一堆不等式,然后用最短路求解 POJ3169 Layout 最后要求1和n之间最大dis是多少  ->  转化为得到一堆  d[n] - d[1] <= xi  然后求xi的最小值 对于给出的是d[u] - d[v] >= xi  同乘-1转化为 d[v] - d[u] <= -xi 即可 然后用spfa求 1到n的最短路 若有负环则无解,若求得的最短路为inf则说明1,n间距离可随便大 代码: 1 #include <cstdio> 2 #include

【POJ3169 】Layout (认真的做差分约束)

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 ar

Android新建项目手动添加Layout布局

前言: 这是看<第一行代码>学习到的第一章,之前使用Eclipse创建Android项目都是自动生成MainActivity.java文件和layout文件夹下的activity_main.xml布局文件,今天把自动生成这些文件的对勾去掉后,手动创建了这两个           文件,于是就写下随笔来记录一下加深印象,同时这也是申请博客以来第一次发表一些东西,就是想把记笔记当做一个习惯保持下去,OK,到这里了.... 1.所有创建项目的步骤都是一样的,只有到最后把Create Activity

寒假学干货之------初步布局Layout

在开发的最初,需要设计好我们的Activity,在res/layout下,找到**activitymian(名字都差不多的)的.xml文件,打开他就可以开始编辑. http://www.tuicool.com/articles/3uUZbmu(参考,转载文献网址) 下面是几个比较常用的布局: LinearLayout(线性布局).FrameLayout(单帧布局).AbsoluteLayout(绝对布局).TablelLayout(表格布局).RelativeLayout(相对布局).其中最常用

如何解决Xamarin for VS:Disconnected from layout renderer

最近学习Xamarin for Android,我用的是for VS2013版本.由于开始使用的Xamarin是低版本的,所以在和VS2013配合后,可以编译,可以release,但是不能查看layout文件的布局文件.总是出现: Disconnected from layout renderer.后来从Xamarin的官网论坛上查到是因为Android SDK tools的版本过高才导致的此问题.?当时论坛的信息比较老,没有给出我当时使用版本的对应解决方案.于是我就只能自己试验了.最后结合论坛