zoukankan      html  css  js  c++  java
  • 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;
    }
    




查看全文
  • 相关阅读:
    iOS 6编程UIScrollView滚动视图结合UIImageView图像视图实现图像缩放效果
    iOS 6编程UIScrollView滚动视图和UIPageControl分页控件实现图像分页显示(2)
    iOS 6编程基于AV Foundation框架开发简单音乐播放器
    iOS 6 的5个新特性创建杀手级应用
    mysql数据库备份和还原
    SEO实战:解决百度收录问题
    nginx的80端口配置两个Web服务
    DedeCMS, Discuz, Phpwind, PhpCMS
    nginx下安装wordpress
    larbin编译和配置
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10592074.html
  • Copyright © 2011-2022 走看看