【最短路建模】【d[u] <= d[v] + g[v][u] v--->u】【差分系统】
2015-08-24 18:46
513 查看
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.
Source
USACO 2005 December Gold
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8651 | Accepted: 4165 |
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.
Source
USACO 2005 December Gold
#include <iostream> #include <cstring> #include <stdio.h> #include <queue> using namespace std; int n,ml,md; const int maxn = 100010; int u1[maxn],v1[maxn],c1[maxn]; int u2[maxn],v2[maxn],c2[maxn]; int d[1010]; const int INF = 0x3f3f3f3f; void solve() { for(int i=1;i<=n;i++) d[i] = INF; d[1] = 0; for(int i=0;i<n;i++) { for(int j=1;j+1<=n;j++) if(d[j+1] < INF) d[j] = min(d[j],d[j+1]+0); for(int j=1;j<=ml;j++) if(d[u1[j]] < INF) d[v1[j]] = min(d[v1[j]],d[u1[j]] + c1[j]); for(int j=1;j<=md;j++) if(d[v2[j]] < INF) d[u2[j]] = min(d[u2[j]],d[v2[j]] - c2[j]); } int ans = d ; if(d[1] < 0) ans = -1; else if(ans == INF) ans = -2; printf("%d\n",ans); } // d[i] <= d[i+1] + 0 d[i+1]---->d[i] // v1[i] - u1[i] <= c1[i] || v1[i] <= u1[i] + c1[i] u1 ----> v1 // v2[i] - u2[i] >= c2[i] || v2[i] - c2[i] >= u2[i] v2 ----> u2 int main() { freopen("in.txt","r",stdin); while(scanf("%d%d%d",&n,&ml,&md) != EOF) { for(int i=1;i<=ml;i++) { scanf("%d%d%d",&u1[i],&v1[i],&c1[i]); } for(int i=1;i<=md;i++) { scanf("%d%d%d",&u2[i],&v2[i],&c2[i]); } solve(); } }
相关文章推荐
- Calendar--Java快速获取昨天日期
- Nuget安装更新包到指定版本
- 编写高质量代码改善C#程序的157个建议——建议156:利用特性为应用程序提供多个版本
- 【数据库】主键、外键、索引
- Hadoop2 namenode 联邦 实验
- CDH集群调优:内存、Vcores和DRF
- 网络爬虫:采用“负载均衡”策略来优化网络爬虫
- 网络爬虫:采用“负载均衡”策略来优化网络爬虫
- 大数据技术就在生活中: 登机牌、阅卷与 Map-Reduce(归约)
- 笔画
- STL学习----入门(1)[stack]
- UIAlert的使用
- Asp.Net中的三种分页方式
- 关于AutoCompleteTextView实现前几个历史提示
- linux之chattr命令
- 【POJ 3662】Telephone Lines
- Censored! - POJ 1625(ac自动机+简单dp+高精度运算)
- Asp.Net中的三种分页方式
- 一次奇怪的调优
- swift 动画