最短路练习14/poj/3169 Layout 差分约束spfa解法
2017-04-26 21:41
183 查看
题目链接:http://poj.org/problem?id=3169
Layout
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.
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.
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
Sample Output
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.
这道题我数组开小了,居然wa了,我的Re去哪了,害我又白交了30几遍才发现第三次交的的代码改下数组大小就能过。
题意: 有n头牛,按照序号1……n排队,多头牛可以站在同一个位置(暂且这么认为),也就是任意两头牛之间的距离都大于等于0。先给出ml组约束关系(u,v,w)代表第u牛和第v牛之间的距离要小于等于w。再给出md组关系(u,v,w),代表第u牛和第v牛之间的距离要大于等于w。求这n头牛排成的队伍能否符合以上的约束,不能的话(也就是出现负环),输出“-1”,如果距离是inf,输出“-2”。否则输出这个最短距离。
思路:转化为差分约束模型。dis[i]表示第i头牛所在的位置。要注意除了题目所给出的ml+md条约束外还要加入任意两头牛之间距离大于等于0的约束,也就是dis[i+1]>dis[i]。把1点设为最短路始点,如果spfa判断出负环的话就输出-1,如果dis
==inf则输出-2。否则输出dis
。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<string>
#define LL long long
#define eps 1e-8
using namespace std;
const int mod = 1e7+7;
const int inf = 0x3f3f3f3f;
const int maxx = 200010;
const int N = 10500;
int n,ml,md,cnt=0;
struct node
{
int y,v;
} p[maxx];
int head
,next
;
int dis
,vis
;
int q[maxx];
int num
;
void add(int a,int b,int c)
{
p[cnt].y=b;
p[cnt].v=c;
next[cnt]=head[a];
head[a]=cnt++;
}
int relax(int a,int b,int c)
{
if(dis[a]+c<dis[b])
{
dis[b]=dis[a]+c;
return 1;
}
return 0;
}
int spfa()
{
memset(dis,inf,sizeof(dis));
memset(vis,0,sizeof(vis));
memset(num,0,sizeof(num));
dis[1]=0;
vis[1]=1;
num[1]++;
int top=0,f,k;
q[top++]=1;
while(top)
{
f=q[--top];
vis[f]=0;
k=head[f];
while(k!=-1)
{
if(relax(f,p[k].y,p[k].v)&&!vis[p[k].y])
{
vis[p[k].y]=1;
q[top++]=p[k].y;
num[p[k].y]++;
if(num[p[k].y]>n)
return -1;
}
k=next[k];
}
}
if(dis
==inf)
return -2;
return dis
;
}
int main()
{
int a,b,c;
memset(head,-1,sizeof(head));
scanf("%d%d%d",&n,&ml,&md);
for(int i=0; i<ml; i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
for(int i=0; i<md; i++)
{
scanf("%d%d%d",&a,&b,&c);
add(b,a,-c);
}
printf("%d",spfa());
}
Layout
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11283 | Accepted: 5421 |
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.
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.
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 1 1 3 10 2 4 20 2 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.
这道题我数组开小了,居然wa了,我的Re去哪了,害我又白交了30几遍才发现第三次交的的代码改下数组大小就能过。
题意: 有n头牛,按照序号1……n排队,多头牛可以站在同一个位置(暂且这么认为),也就是任意两头牛之间的距离都大于等于0。先给出ml组约束关系(u,v,w)代表第u牛和第v牛之间的距离要小于等于w。再给出md组关系(u,v,w),代表第u牛和第v牛之间的距离要大于等于w。求这n头牛排成的队伍能否符合以上的约束,不能的话(也就是出现负环),输出“-1”,如果距离是inf,输出“-2”。否则输出这个最短距离。
思路:转化为差分约束模型。dis[i]表示第i头牛所在的位置。要注意除了题目所给出的ml+md条约束外还要加入任意两头牛之间距离大于等于0的约束,也就是dis[i+1]>dis[i]。把1点设为最短路始点,如果spfa判断出负环的话就输出-1,如果dis
==inf则输出-2。否则输出dis
。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<string>
#define LL long long
#define eps 1e-8
using namespace std;
const int mod = 1e7+7;
const int inf = 0x3f3f3f3f;
const int maxx = 200010;
const int N = 10500;
int n,ml,md,cnt=0;
struct node
{
int y,v;
} p[maxx];
int head
,next
;
int dis
,vis
;
int q[maxx];
int num
;
void add(int a,int b,int c)
{
p[cnt].y=b;
p[cnt].v=c;
next[cnt]=head[a];
head[a]=cnt++;
}
int relax(int a,int b,int c)
{
if(dis[a]+c<dis[b])
{
dis[b]=dis[a]+c;
return 1;
}
return 0;
}
int spfa()
{
memset(dis,inf,sizeof(dis));
memset(vis,0,sizeof(vis));
memset(num,0,sizeof(num));
dis[1]=0;
vis[1]=1;
num[1]++;
int top=0,f,k;
q[top++]=1;
while(top)
{
f=q[--top];
vis[f]=0;
k=head[f];
while(k!=-1)
{
if(relax(f,p[k].y,p[k].v)&&!vis[p[k].y])
{
vis[p[k].y]=1;
q[top++]=p[k].y;
num[p[k].y]++;
if(num[p[k].y]>n)
return -1;
}
k=next[k];
}
}
if(dis
==inf)
return -2;
return dis
;
}
int main()
{
int a,b,c;
memset(head,-1,sizeof(head));
scanf("%d%d%d",&n,&ml,&md);
for(int i=0; i<ml; i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
for(int i=0; i<md; i++)
{
scanf("%d%d%d",&a,&b,&c);
add(b,a,-c);
}
printf("%d",spfa());
}
相关文章推荐
- 最短路练习9/poj/2240 Arbitrage;正环:floyd或spfa解法;
- 最短路练习10/poj/1511 Invitation Cards ,(两次spfa),(单源最短路,优先队列优化的Dijkstra)
- (模板题)sdut 2143 图结构练习——最短路径(SPFA求最短路)
- 最短路练习12/poj/2502 Subway dijkstar,spfa,floyd都可以解决
- 【算法系列学习】SPFA邻接表最短路 [kuangbin带你飞]专题四 最短路练习 F - Wormholes
- 最短路练习13/poj/1847 / Tram/floyd解法;memset用0x3f初始化详解
- 最短路练习3/poj/1797/Heavy Transportation/dijkstar解法和并查集解法
- POJ 2387 Til the Cows Come Home (单源最短路SPFA解法)
- 蓝桥杯算法练习最短路(spfa)
- hdu 0 or 1(最短路spfa)(本质,抽象)
- 3最短路的几种解法 ------例题< 最短路 >
- Islands Travel——SPFA求最短路
- 最短路 spfa 算法 && 链式前向星存图
- POJ - 1511 Invitation Cards (SPFA最短路)
- poj 2387最短路 使用SPFA
- Proxy(2016山东省省赛C)(最短路)(spfa)
- Bellman Ford+SPFA队列优化(路径还原 输出最短路的路径)
- HDU1317 变形SPFA 求最大路 判断正环 点权替代边权 最短路求法灵活应用
- poj 1860 Currency Exchange ([kuangbin带你飞]专题四 最短路练习)
- POJ-1511(SPFA找最短路(正向和反向))