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 ...
随机推荐
- Codeforces633G(SummerTrainingDay06-I dfs序+线段树+bitset)
G. Yash And Trees time limit per test:4 seconds memory limit per test:512 megabytes input:standard i ...
- mysql小试题3
查询结果:
- 31:字符串p型编码
31:字符串p型编码 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 给定一个完全由数字字符('0','1','2',…,'9')构成的字符串str,请写出s ...
- 水平方向margin:auto
先上图 由图可看到,块级元素的水平方向上又"7大属性":margin-left.border-left.padding-left.margin-left.width.paddi ...
- Ubuntu16.04搭建kubernetes v1.11.2集群
1.节点介绍 master cluster-1 cluster-2 cluster-3 hostname k8s-55 k8s-5 ...
- Microsoft MVP MSDN Magazine 最新期分享
下载地址:http://1105insight.com/portal/wts/uemcmQeeDyaq%5Ev2gAe6c3b0Djd 可在线或下载查看
- Android Studio离线打包5+SDK
dcloud官网下载最新版5+SDK 解压后,Android Studio导入HBuilder-Hello,选择From eclispe 修改assets/data/dcloud_control.xm ...
- View的layout机制
View框架的工作流程为:测量每个View大小(measure)-->把每个View放置到相应的位置(layout)-->绘制每个View(draw). 源代码分析 在View的源代码中, ...
- Android 监听手机GPS打开状态
转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/70854942 本文出自[赵彦军的博客] GPS_Presenter package ...
- 机器学习实战(Machine Learning in Action)学习笔记————08.使用FPgrowth算法来高效发现频繁项集
机器学习实战(Machine Learning in Action)学习笔记————08.使用FPgrowth算法来高效发现频繁项集 关键字:FPgrowth.频繁项集.条件FP树.非监督学习作者:米 ...