poj 3169 差分约束
来源:互联网 发布:c4d r16 mac安装 编辑:程序博客网 时间:2024/05/21 17:31
Layout
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 10068 Accepted: 4840
Description
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.
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
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.
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
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.
Sample Input
4 2 11 3 102 4 202 3 3
Sample Output
27
Hint
Explanation of the sample:
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.
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.
题意:
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 n 没有连接在一起,就输出 -1
如果距离可以无限远就输出 -2
有限距离,计算出最大的距离就好了
无限远用 dis 【n】==INF 就好了
本题队列比栈实现SPFA快一些,不知道为什么,有些题目栈却快一点
本题中隐含的条件:
i +1 - i >=0
我一直没有将所有的点这样处理,只是处理输入的点
唉!!!算法理解的还不够透彻啊,继续加油吧
#include<queue>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int INF=0x3f3f3f3f;int n,ml,md;int head[21000],vis[21000];int dis[21000],num[21000];struct node{ int v,w,next;}path[21000];int top;void add(int u,int v,int w){ path[top].v=v; path[top].w=w; path[top].next=head[u]; head[u]=top++;}int SPFA(){ memset(vis,0,sizeof(vis)); memset(num,0,sizeof(num)); for(int i=1;i<=n;i++) dis[i]=INF; queue<int>q; dis[1]=0; q.push(1); num[1]++; while(q.size()) { int p=q.front(); q.pop(); vis[p]=0; for(int i=head[p];i!=-1;i=path[i].next){ int t=path[i].v; if(dis[t]>dis[p]+path[i].w){ dis[t]=dis[p]+path[i].w; if(vis[t]==0){ q.push(t); num[t]++; vis[t]=1; if(num[t]>n) return -1; } } } } if(dis[n]>=INF) return -2; return dis[n];}int main(){ top=0; int a,b,c; //freopen("in.txt","r",stdin); memset(head,-1,sizeof(head)); scanf("%d%d%d",&n,&ml,&md); while(ml--) { scanf("%d%d%d",&a,&b,&c); add(a,b,c); } while(md--) { scanf("%d%d%d",&a,&b,&c); add(b,a,-c); } for(int i=1;i<=n;i++) add(i,i-1,0); int ans=SPFA(); printf("%d\n",ans); return 0;}
0 0
- poj 3169 差分约束
- POJ 3169 差分约束
- POJ 3169 差分约束
- POJ 3169 差分约束
- poj 3169 差分约束
- POJ 3169 差分约束
- POJ 3169 差分约束
- poj 3169 差分约束
- POJ 3169 差分约束
- 【POJ 3169】差分约束
- POJ 3169 Layout(差分约束系统)
- poj 3169 差分约束 + spfa
- POJ 3169 Layout [差分约束]
- POJ 3169 差分约束系统
- POJ 3169 Layout 差分约束系统
- POJ 3169 Layout 差分约束系统
- poj 3169 Layout(差分约束)
- poj 3169 Layout(差分约束)
- 第三方登录
- B站的DanmakuFlameMaster的使用
- yii邮件发送
- discuz!X3分类信息调用-主题列表页调用分类信息字段
- POJ 1961 字符串 KMP (i-next[i])
- poj 3169 差分约束
- php从mysql取出int数据,变成了string
- Effetive Objective - C 1.Objective 23.委托与数据源协议进行对象间通信
- Android总结篇系列:Android广播机制
- [leetcode] 110. Balanced Binary Tree
- Oracle中有关Group by 中avg();sum();min();max();count();的运用整理(Oracle的执行顺序)
- 阿里2017校园招聘电话面试总结
- 数据挖掘的一些评价标准
- xshell传输文件