poj 3169 Layout 差分约束模板题
Layout
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6415 | Accepted: 3098 |
Description
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
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
Sample Input
4 2 1
1 3 10
2 4 20
2 3 3
Sample Output
27
Hint
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.
#include "stdio.h"
#include "string.h"
#include "queue"
using namespace std; #define N 1005
#define INF 0x3fffffff
/**
1. 如果要求最大值想办法把每个不等式变为标准x-y<=k的形式,然后建立一条从y到x权值为k的边,变得时候注意x-y<k =>x-y<=k-1 如果要求最小值的话,变为x-y>=k的标准形式,然后建立一条从y到x的k边,求出最长路径即可 2.如果权值为正,用dijkstra,spfa,bellman都可以,如果为负不能用dijkstra,并且需要判断是否有负环,有的话就不存在
**/ int head[N],idx;
bool mark[N];
int dist[N],countt[N]; struct node
{
int x,y;
int next;
int weight;
}edge[4*20*N]; void Init()
{
idx = 0;
memset(head,-1,sizeof(head));
} void swap(int &a,int &b)
{
int k = a;
a = b;
b = k;
} void Add(int x,int y,int k)
{
edge[idx].x = x;
edge[idx].y = y;
edge[idx].weight = k;
edge[idx].next = head[x];
head[x] = idx++;
} bool SPFA(int start,int end)
{
int i,x,y;
memset(countt,0,sizeof(countt)); //统计每一个点加入队列的次数,判断是否有负环!
memset(mark,false,sizeof(mark));
for(i=start; i<=end; ++i) dist[i] = INF; queue<int> q;
q.push(start);
countt[start]++;
dist[start] = 0; mark[start] = true;
while(!q.empty())
{
x = q.front();
q.pop();
for(i=head[x]; i!=-1; i=edge[i].next)
{
y = edge[i].y;
if(dist[y]>dist[x]+edge[i].weight)
{
dist[y] = dist[x]+edge[i].weight;
if(!mark[y])
{
mark[y] = true;
q.push(y);
countt[y]++;
if(countt[y]>end) return false;
}
}
}
mark[x] = false;
}
return true;
} int main() /**求最大值,不等式化为x-y<=k的形式**/
{
int n;
int x,y,k;
int ML,MD;
while(scanf("%d %d %d",&n,&ML,&MD)!=EOF)
{
Init(); //初始化!
while(ML--)
{
scanf("%d %d %d",&x,&y,&k);
if(x>y) swap(x,y);
Add(x,y,k); /**y-x<=k**/
}
while(MD--)
{
scanf("%d %d %d",&x,&y,&k);
if(x>y) swap(x,y);
Add(y,x,-k); /**y-x>=k => x-y<=-k**/
}
bool flag = SPFA(1,n);
if(!flag) printf("-1\n");
else if(dist[n]==INF) printf("-2\n");
else
printf("%d\n",dist[n]);
}
return 0;
}
poj 3169 Layout 差分约束模板题的更多相关文章
- POJ 3169 Layout (差分约束)
题意:给定一些母牛,要求一个排列,有的母牛距离不能超过w,有的距离不能小于w,问你第一个和第n个最远距离是多少. 析:以前只是听说过个算法,从来没用过,差分约束. 对于第 i 个母牛和第 i+1 个, ...
- POJ 3169 Layout(差分约束+链式前向星+SPFA)
描述 Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 ...
- POJ 3169 Layout(差分约束啊)
题目链接:http://poj.org/problem? id=3169 Description Like everyone else, cows like to stand close to the ...
- POJ 3169 Layout(差分约束 线性差分约束)
题意: 有N头牛, 有以下关系: (1)A牛与B牛相距不能大于k (2)A牛与B牛相距不能小于k (3)第i+1头牛必须在第i头牛前面 给出若干对关系(1),(2) 求出第N头牛与第一头牛的最长可能距 ...
- ShortestPath:Layout(POJ 3169)(差分约束的应用)
布局 题目大意:有N头牛,编号1-N,按编号排成一排准备吃东西,有些牛的关系比较好,所以希望他们不超过一定的距离,也有一些牛的关系很不好,所以希望彼此之间要满足某个关系,牛可以 ...
- POJ 1364 King --差分约束第一题
题意:求给定的一组不等式是否有解,不等式要么是:SUM(Xi) (a<=i<=b) > k (1) 要么是 SUM(Xi) (a<=i<=b) < k (2) 分析 ...
- poj 1201 Intervals——差分约束裸题
题目:http://poj.org/problem?id=1201 差分约束裸套路:前缀和 本题可以不把源点向每个点连一条0的边,可以直接把0点作为源点.这样会快许多! 可能是因为 i-1 向 i 都 ...
- poj 3169&hdu3592(差分约束)
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9687 Accepted: 4647 Descriptio ...
- Bellman-Ford算法:POJ No.3169 Layout 差分约束
#define _CRT_SECURE_NO_WARNINGS /* 4 2 1 1 3 10 2 4 20 2 3 3 */ #include <iostream> #include & ...
随机推荐
- 一些JavaScript题目
在JavaScript中,运行下面代码,sum的值是(). var sum=0;for(i=1;i<10;i++){if(i%5==0)break;sum=sum+i;} A. 40B. 50C ...
- 吉日嘎拉DotNet.BusinessV4.2中的一处bug,及我的修复和扩展
bug所在位置:DotNet.Business\Utilities\BaseManager.GetDataTableByPage.cs的函数 public virtual DataTable GetD ...
- [小北De编程手记] : Lesson 01 - Selenium For C# 之 环境搭建
在我看来一个自动化测试平台的构建,是一种很好的了解开发语言,单元测试框架,自动化测试驱动,设计模式等等等的途径.因此,在下选择了自动化测试的这个话题来和大家分享一下本人关于软件开发和自动化测试的认识. ...
- [翻译]:SQL死锁-锁与事务级别
其实这一篇呢与解决我项目中遇到的问题也是必不可少的.上一篇讲到了各种锁之间的兼容性,里面有一项就是共享锁会引起死锁,如何避免呢,将我们的查询都设置中read uncommitted是否可行呢?其结果显 ...
- js中创建对象的几种方式
创建对象指创建一个object并给这个对象添加属性和方法,有以下几个方式: 最基本的: var Person={}; Person.name='tom'; Person.age='20'; Perso ...
- Vue自带的过滤器
gitHub地址:https://github.com/lily1010/vue_learn/tree/master/lesson05 一 过滤器写法 {{ message | Filter}} 二 ...
- 优化ABAP性能(摘录)
1.使用where语句不推荐Select * from zflight.Check : zflight-airln = ‘LF’ and zflight-fligh = ‘BW222’.Endsele ...
- .NET破解之轻量万能自定义信息管理系统
一般敢说万能的莫非真有两把刷子.今天来破解试试,看效果好用不. 下载:http://down.chinaz.com/soft/36780.htm 补丁: http://www.t00y.com/fil ...
- Objective-C
1.OC基础 第一个OC的类 Objective-C: 字符串NSString与NSMutableString iOS开发的入门总结的第一篇 iOS开发的入门总结的第二篇
- Android系统提供的开发常用的包名及作用
android.app :提供高层的程序模型.提供基本的运行环境 android.content :包含各种的对设备上的数据进行访问和发布的类 android.database :通过内容提供者浏览和 ...