POJ 3169(Bellman-Ford演算法,差分約束系統)
Layout
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
#include <string.h>
#include <stdio.h>
#define MAX 10005
#define MAX2 1005
#define INF 0x3fffffff
int AL[MAX],BL[MAX],DL[MAX];
int AD[MAX],BD[MAX],DD[MAX];
int d[MAX2];
int N,ML,MD;
int min(int x,int y)
{
return (x<y)?x:y;
}
void solve()
{
int i,j,k;
for(i=0;i<N;i++)
{
d[i]=INF;
}
d[0]=0;
//迴圈k次
for(k=0;k<N;k++)
{
//遍歷所有的邊
for(i=0;i<N-1;i++)
{
if(d[i+1]<INF)
{
d[i]=min(d[i],d[i+1]+0);
}
}
//因為A<B,且要求個頭牛之間的相對位置保持不變,所以只能是BL-AL<=DL
for(i=0;i<ML;i++)
{
if(d[AL[i]-1]<INF)
{
d[BL[i]-1]=min(d[BL[i]-1],d[AL[i]-1]+DL[i]);
}
}
//更新理由同上
for(i=0;i<MD;i++)
{
if(d[BD[i]-1]<INF)
{
d[AD[i]-1]=min(d[AD[i]-1],d[BD[i]-1]-DD[i]);
}
}
}
int res=d[N-1];
if(d[0]<0)
{
res=-1;
}
else if(res==INF)
{
res=-2;
}
printf("%d\n",res);
}
int main()
{
scanf("%d %d %d",&N,&ML,&MD);
int i,j;
for(i=0;i<ML;i++)
{
scanf("%d%d%d",&AL[i],&BL[i],&DL[i]);
}
for(i=0;i<MD;i++)
{
scanf("%d%d%d",&AD[i],&BD[i],&DD[i]);
}
solve();
return 0;
}
相關文章
- POJ 3169-Layout(差分約束系統-入門裸題)
- POJ 2983-Is the Information Reliable?(差分約束系統)ORM
- POJ 1364-King(差分約束系統)
- 差分約束系統+poj1201
- 演算法學習之路|差分約束系統演算法
- 淺談差分約束系統
- 差分約束系統詳解
- POJ 3159-Candies(差分約束系統-SPFA+鄰接表)
- 差分約束
- [演算法學習筆記] 差分約束演算法筆記
- 差分約束學習筆記筆記
- 差分約束的一些理解
- uva 11478 最短路徑問題(負環,差分約束系統)
- 差分約束系統+SPFA/Bellman判斷負權迴路+uva515
- Day2 尤拉路,拓撲排序和差分約束排序
- 演算法隨筆——圖論之差分約束演算法圖論
- Atcoder ABC 216 G 01Sequence 題解 [ 藍 ] [ 差分約束 ]
- Javaweb-約束的分類JavaWeb
- 【SQL】15 SQL 約束(Constraints)、NOT NULL 約束、UNIQUE 約束、PRIMARY KEY 約束、FOREIGN KEY 約束、CHECK 約束、DEFAULT約束SQLAINull
- 時序分析:基礎知識整理(三)差分轉單端的約束等
- 無約束凸優化演算法優化演算法
- Javaweb-約束-外來鍵約束JavaWeb
- 05-無約束優化演算法優化演算法
- 06-等式約束優化演算法優化演算法
- 資料庫系統之實體完整性約束資料庫
- Oracle定義約束 外來鍵約束Oracle
- SQL約束SQL
- Oracle約束Oracle
- oracle 約束Oracle
- MySQL 約束MySql
- 03約束
- 綜合約束
- (10)邏輯綜合新增約束(環境約束)
- 【演算法導論】24.1 Bellman-Ford 演算法演算法
- 陣列演算法-差分陣列陣列演算法
- 約束介紹
- 資料庫的集合,分頁及約束條件資料庫
- Lp分紅USDT合約系統開發(邏輯說明)| Lp分紅USDT合約系統原始碼示例原始碼