【差分约束系统】[USACO05DEC] 布局 Layout

并没有什么用的更新时间

【2019/07/10】 题目及代码、题解更新


【差分约束系统】[USACO05DEC] 布局Layout

tip:翻译来自洛谷,如有出入请见谅

  • 题目描述
    正如其他物种一样,奶牛们也喜欢在排队打饭时与它们的朋友挨在一起。FJ 有编号为 1\dots N1…N 的 NN 头奶牛 (2\le N\le 1000)(2≤N≤1000)。开始时,奶牛们按照编号顺序来排队。奶牛们很笨拙,因此可能有多头奶牛在同一位置上。
    有些奶牛是好基友,它们希望彼此之间的距离小于等于某个数。有些奶牛是情敌,它们希望彼此之间的距离大于等于某个数。
    给出 M_L对好基友的编号,以及它们希望彼此之间的距离小于等于多少;又给出 M_D对情敌的编号,以及它们希望彼此之间的距离大于等于多少 (1≤M L,M_D≤10^4)。
    请计算:如果满足上述所有条件,11 号奶牛和 NN 号奶牛之间的距离最大为多少。
    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.
  • 输入输出格式
  • 输入格式:
    第1行:三个以空格分隔的整数:N,ML和MD。
    第2行…ML + 1:每行包含三个以空格分隔的正整数:A,B和D,其中1 <= A <B <= N.奶牛A和B必须至多为D(1 <= D <= 1,000,000)分开。
    ML + 2…ML + MD + 1行:每行包含三个以空格分隔的正整数:A,B和D,其中1 <= A <B <= N.奶牛A和B必须至少为D( 1 <= D <= 1,000,000)。
    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.
  • 输出格式:
    一行,一个整数。如果没有合法方案,输出 -1. 如果有合法方案,但 11 号奶牛可以与 NN 号奶牛相距无穷远,输出 -2. 否则,输出 11 号奶牛与 NN 号奶牛间的最大距离。
    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.
  • 输入输出样例
  • 输入样例#1:
    4 2 1
    1 3 10
    2 4 20
    2 3 3
  • 输出样例#1:
    27
  • 题解
    题目难度:★★★☆☆
    思路:1、因为我们要求1号牛和n号牛的最大距离,那么我们希望得到这样一个不等式(假设n==5):①-⑤<=x,那么这个x就是1号牛和n号牛的最大距离。
    2、对应ml条信息:
    ①牛A和牛B的距离不想超过D,那么建立不等式:posA-posB<=D;加入到图中直接add(A,B,D)即可、
    对应md条信息:
    ②牛A和牛B的距离至少要为D,那么建立不等式:posA-posB>=D,那么我们左右两边同乘-1有:posB-posA<=-D,那么加入到图中add(B,A,-D)即可。
    3、图建立好之后直接跑最短路即可。
    对应输出:
    ①如果dis【n】==inf,输出-2;
    ②否则输出dis【n】;
    ③如果在跑SPFA过程中发现了负环,说明问题无解,那么输出-1.
  • 代码
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=210005;
const int inf=9999999;
struct tree{
    int v;
    int to;
    int next;
}a[maxn];
int n,ml,md,tot,head[maxn],dis[maxn],f[maxn],vis[maxn];
void addedge(int from,int to,int v){
	a[tot].v=v;
	a[tot].to=to;
	a[tot].next=head[from];
	head[from]=tot++;
}
void spfa(){
	for (int i=1;i<=n;++i) dis[i]=inf;
	dis[1]=0;
	queue<int> q;
    q.push(1);
    while(!q.empty())
    {
        int x=q.front();
        f[x]++;
        if (f[x]>n){
        	cout<<"-1"<<endl;
        	return;
        }
	    q.pop();
        vis[x] = 0;
        for(int e=head[x];e!=-1;e=a[e].next)
          if(dis[a[e].to]>dis[x]+a[e].v){
              dis[a[e].to]=dis[x]+a[e].v;
              if(!vis[a[e].to]){
                  q.push(a[e].to);
                  vis[a[e].to] = 1;
              }
          }
    }
    if (dis[n]==inf) cout<<"-2"<<endl;
                else cout<<dis[n]<<endl;
}
int main(){
	while (~scanf("%d%d%d",&n,&ml,&md)){
		tot=0;
		memset(head,-1,sizeof(head));
		memset(dis,0,sizeof(dis));
		memset(vis,0,sizeof(vis));
		memset(f,0,sizeof(f));
		memset(a,0,sizeof(a));
		int x,y,v;
		for (int i=1;i<=ml;++i){
			cin>>x>>y>>v;
		    addedge(x,y,v);
		}
		for (int i=1;i<=md;++i){
			cin>>x>>y>>v;
		    addedge(y,x,-v);
		}
	    for(int i=1;i<n;i++) addedge(i+1,i,0);
		for (int i=1;i<=n;++i) addedge(0,i,0);
		spfa();
	}
}
  • 相关连接

北大P3169
洛谷P4878

猜你喜欢

转载自blog.csdn.net/qq_44618728/article/details/95315304
今日推荐