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 ...
随机推荐
- CentOS 7开启防火墙端口
1.开启防火墙 systemctl start firewalld 2.添加 firewall-cmd --zone=public --add-port=80/tcp --permanent 3.重新 ...
- vue 数据请求
作者QQ:1095737364 QQ群:123300273 欢迎加入! 要引入模块: vue-resource 1.在package.json中的dependencies中添加vue ...
- 【读书笔记】iOS-网络-底层网络
在iOS上,有一个库叫做Core Foundation networking或CFNetwork,它是对原始Socket的轻量级封装,不过它很快对于大多数常见场景来说变得非常笨重了.最后,添加了另一层 ...
- 安装cuda8.0时无法安装.net Framework 4.0 错误的解决
作者:朱金灿 来源:http://blog.csdn.net/clever101 在win7 64位旗舰版(带sp1)上安装cuda时到安装Microsoft.NET Framework4.0,一直停 ...
- 如何获取listview里面的edittext或者RadioGroup的值,涉及到引发的混乱现象
最近要实现从数据库读数据,该数据对应listview的item布局里面的RadioButton值,并且item布局里面还有EditText的控件. 如何将每一条对应的listview对应值获取出来呢? ...
- 2018-10-23 23:29:54 clanguage
2018-10-23 23:29:54 clanguage 在 32 位环境以及 Win64 环境下的运行结果为: short=2, int=4, long=4, char=1 在 64 位 Li ...
- React Native 二维码扫描组件
学rn得朋友们,你们知道rn开源项目吗?来吧看这里:http://www.marno.cn/(rn开源项目) React Native学习之路(9) - 注册登录验证的实现 + (用Fetch实现po ...
- Oracle EBS AP 应付核销到确定一行预付款
-- purpose: 应付标准发票核销预付款发票中的一行 -- 12.2.6 环境 -- author:jenrry create_date: 2017-06-08 declare l_error_ ...
- C# System.IO.Path
Path的常用方法 函数列表 对一个路径做相应操作,包括文件路径,目录路径,通常会用到Path这个类, 本文列举一些常用的操作. 获取指定路径字符串的目录信息 public static string ...
- 基于esky实现python应用的自动升级
一.esky介绍 Esky is an auto-update framework for frozen Python applications. It provides a simple API t ...