poj 3169 Layout(线性差分约束,spfa:跑最短路+判断负环)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 15349 | Accepted: 7379 |
Description
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.
Source
给n,m1,m2
n头牛,每头牛跟其他的牛直接的距离有一定的约束
m1个约束1,m2个约束2
约束1:
a b c 表示a牛和b牛之间的距离最多c
约束2:
a b c 表示a牛和b牛之间的距离最少c
问你两头牛之间的最大距离至少是多少才能满足所有的约束
x[i]表示牛i的在x[i]处或者说牛i在距离原点x[i]的地方
x[a]-x[b]<=c
约束2可以表示为:
x[b]-x[a]<=-c
按照j到i建图,权值为c
然后起点是1,跑个最短路(不能使用dj,因为存在负权)
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<set>
#include<map>
#include<list>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long LL;
#define INF 9999999999
#define me(a,x) memset(a,x,sizeof(a))
int mon1[]= {,,,,,,,,,,,,};
int mon2[]= {,,,,,,,,,,,,};
int dir[][]= {{,},{,-},{,},{-,}}; int getval()
{
int ret();
char c;
while((c=getchar())==' '||c=='\n'||c=='\r');
ret=c-'';
while((c=getchar())!=' '&&c!='\n'&&c!='\r')
ret=ret*+c-'';
return ret;
}
void out(int a)
{
if(a>)
out(a/);
putchar(a%+'');
} #define max_v 1005
struct node
{
int v;
LL w;
node(int vv=,LL ww=):v(vv),w(ww){}
};
LL dis[max_v];
int vis[max_v];
int cnt[max_v];
vector<node> G[max_v];
queue<int> q; void init()
{
for(int i=;i<max_v;i++)
{
G[i].clear();
dis[i]=INF;
vis[i]=;
cnt[i]=;
}
while(!q.empty())
q.pop();
} int spfa(int s,int n)
{
vis[s]=;
dis[s]=;
q.push(s);
cnt[s]++; while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=; for(int j=;j<G[u].size();j++)
{
int v=G[u][j].v;
LL w=G[u][j].w; if(dis[v]>dis[u]+w)
{
dis[v]=dis[u]+w;
if(vis[v]==)
{
q.push(v);
cnt[v]++;
vis[v]=; if(cnt[v]>n)
return ;
}
}
}
}
return ;
}
int f(int u,int v)
{
for(int j=;j<G[u].size();j++)
{
if(G[u][j].v==v)
return ;
}
return ;
}
int main()
{
int n,a,b;
while(~scanf("%d %d %d",&n,&a,&b))
{
init();
int x,y,w;
while(a--)
{
scanf("%d %d %d",&x,&y,&w);
if(f(x,y))
G[x].push_back(node(y,w));
}
while(b--)
{
scanf("%d %d %d",&x,&y,&w);
if(f(y,x))
G[y].push_back(node(x,-w));
}
int flag=spfa(,n);
if(flag==)
{
printf("-1\n");
}else if(dis[n]<INF)
{
printf("%lld\n",dis[n]);
}else
{
printf("-2\n");
}
}
return ;
}
/*
题目意思:
给n,m1,m2
n头牛,每头牛跟其他的牛直接的距离有一定的约束
m1个约束1,m2个约束2
约束1:
a b c 表示a牛和b牛之间的距离最多c
约束2:
a b c 表示a牛和b牛之间的距离最少c
问你两头牛之间的最大距离至少是多少才能满足所有的约束 分析:
x[i]表示牛i的在x[i]处或者说牛i在距离原点x[i]的地方 约束1可以表示为:
x[a]-x[b]<=c
约束2可以表示为:
x[b]-x[a]<=-c <=代表的是最大值,代表的是最短路,表达式形式为x[i]-x[j]<=c
按照j到i建图,权值为c
然后起点是1,跑个最短路(不能使用dj,因为存在负权)
1到n的最短路就是能满足所有牛约束的最小距离值 */
poj 3169 Layout(线性差分约束,spfa:跑最短路+判断负环)的更多相关文章
- poj 3169 Layout(差分约束+spfa)
题目链接:http://poj.org/problem?id=3169 题意:n头牛编号为1到n,按照编号的顺序排成一列,每两头牛的之间的距离 >= 0.这些牛的距离存在着一些约束关系:1.有m ...
- (简单) POJ 3169 Layout,差分约束+SPFA。
Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ ...
- HDU 3592 World Exhibition(线性差分约束,spfa跑最短路+判断负环)
World Exhibition Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- POJ 3169 Layout 【差分约束】+【spfa】
<题目链接> 题目大意: 一些母牛按序号排成一条直线.有两种要求,A和B距离不得超过X,还有一种是C和D距离不得少于Y,问可能的最大距离.如果没有最大距离输出-1,如果1.n之间距离任意就 ...
- poj 3169 Layout (差分约束)
3169 -- Layout 继续差分约束. 这题要判起点终点是否连通,并且要判负环,所以要用到spfa. 对于ML的边,要求两者之间距离要小于给定值,于是构建(a)->(b)=c的边.同理,对 ...
- POJ 3169 Layout(差分约束+最短路)题解
题意:有一串数字1~n,按顺序排序,给两种要求,一是给定u,v保证pos[v] - pos[u] <= w:二是给定u,v保证pos[v] - pos[u] >= w.求pos[n] - ...
- poj 3169 Layout(差分约束)
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6549 Accepted: 3168 Descriptio ...
- O - Layout(差分约束 + spfa)
O - Layout(差分约束 + spfa) Like everyone else, cows like to stand close to their friends when queuing f ...
- POJ 3167 Layout(差分约束)
题面 Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 ...
随机推荐
- Javascript 随机数函数 学习之一:产生服从均匀分布随机数
大家都知道Math.random是 javascript 中返回伪随机数的函数,但查看 MDN, The Math.random() function returns a floating-point ...
- WebGIS裁剪算法-线裁剪多边形
在gis系统中 经常会用到一些裁剪的方法,首先推荐一个非常好用的空间分析JavaScript库--Turf.js,不仅功能强大.使用简单,同时处理速度也很快. Turf.js中提供了一中多边形的裁剪方 ...
- ArcGIS三种方式打断相交线------Planarize Lines工具
1. 只有一个layer图层时,我们只需要选择”Planarize Lines“工具即可. (1)选择工具栏”Customize“选项: (2)选择Customize工具栏中的”Toolbars“选项 ...
- IT的2017,面临数字生态系统新挑战,该怎么办?
所谓数字生态系统,就是包含一系列基于标准,规模可变的硬件.软件.数字设备和服务,可系统地实现企业信息数字化,数据流通,以帮助企业提高运营效率. 随着越来越多的中国企业加入数字生态系统,中国CIO在技术 ...
- SQLServer 学习笔记之超详细基础SQL语句 Part 11
Sqlserver 学习笔记 by:授客 QQ:1033553122 -----------------------接Part 10------------------- DECLARE @myavg ...
- flutter 防止键盘弹出 导致超出屏幕
return Scaffold( appBar: AppBar( elevation: 0.0, title: new Text("登陆"), ), resizeToAvoidBo ...
- 检测到在集成的托管管道模式下不适用的ASP.NET设置
解决方法是修改web.config如下: <system.webServer> <validation validateIntegratedModeConfiguration=&qu ...
- Linux下动态链接库加载路径
引子 近日,服务器迁移后,偷懒未重新编译nginx的,直接./nginx启动,结果遇到如下问题: "error while loading shared libraries" 这是 ...
- PostMan请求不到接口问题
在些接口的时候经常需要调试,调试的有很多选择,比如swagger.postman,我就是使用过两个都用:为了避免被swagger坑到就再去用postman试试确认看行不行,结果太小白了还是遇到了一些问 ...
- C#中获取数组中相加和最接近或等于(<=)给定值的算法
, ,,,,,,,,, }; List<List<int>> mylist = new List<List<int>>(); int length = ...