题意:
给你n 只牛。
告诉你一些牛的最远距离是多少, 告诉你一些牛的最近距离是多少, 并且 第i 只牛一定在第i-1只牛的右边,求1到n的最远距离, 不存在输出-1, 无限远输出-2;
思路:
差分约束。
我们知道最短路的判断:
如果d[e.to] >= d[e.from] + e.dist;
那么d[e.to] = d[e.from] + e.dist
所以 d[e.to] <= d[e.from] + e.dist;
因此我们只需要构造出上面的式子,然后 从from 到 to 连一条边,权值为e.dist 求最短路即可。
这里 对于最远的牛。
u,v,w
d[v] - d[u] <= w ---> d[v] <= d[u] + w
因此我们从u到v 连一条边。 边权为w。
对于最近的牛。
u,v,w;
d[v] - d[u] >= w ---> d[u] <= d[v] - w
因此 我们从v到u 连一条边,边权为-w
还有一个条件 第i 只牛一定在第i-1只牛的右边
那么d[i] - d[i-1] >= 0 --> d[i-1] <= d[i]
因此从i 到i-1 连一条边 边权为0 .
求最短路即可, 出现负环 显然无解, d[n] 是inf 表示无限远。
二逼的我 bellman_ford 一个小地方写错了 wa 了一中午。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
const int maxn = 1000 + 7;
const int inf = 0x3f3f3f3f;
int n,q1,q2;
struct Edge{
int f,t,w;
Edge(int f = 0, int t = 0, int w = 0): f(f), t(t), w(w){}
};
struct BBBBB{
int n,m;
vector<int> g[maxn];
vector<Edge> edges;
int d[maxn];
queue<int>q;
bool inq[maxn];
int cnt[maxn];
void init(int n){
this->n = n;
for (int i = 1; i <= n; ++i)g[i].clear();
edges.clear();
m = 0;
}
void add(int f,int t,int w){
edges.push_back(Edge(f,t,w));
g[f].push_back(m++);
}
bool bellman_ford(int s){
memset(d,inf,sizeof d);
memset(inq,0,sizeof inq);
memset(cnt,0,sizeof cnt);
while(!q.empty()) q.pop();
d[s] = 0;
inq[s] = 1;
q.push(s);
while(!q.empty()){
int u = q.front(); q.pop();
inq[u] = 0;
for (int i = 0; i < g[u].size(); ++i){
int v = g[u][i];
Edge& e = edges[v];
if (d[u] < inf && d[e.t] > d[u] + e.w){
d[e.t] = d[u] + e.w;
if (!inq[e.t]){
q.push(e.t);
inq[e.t] = 1;
if (++cnt[e.t] > n) return false;
}
}
}
}
return true;
}
}dic;
int main(){
scanf("%d %d %d",&n, &q1, &q2);
int u,v,w;
dic.init(n);
while(q1--){
scanf("%d %d %d",&u, &v, &w);
dic.add(u,v,w);
}
while(q2--){
scanf("%d %d %d",&u, &v, &w);
dic.add(v,u,-w);
}
for (int i = 2; i <= n; ++i){
dic.add(i,i-1,0);
}
if (!dic.bellman_ford(1)) puts("-1");
else {
if (dic.d[n] == inf) puts("-2");
else printf("%d\n",dic.d[n]);
}
return 0;
}
/**
5 2 1
2 4 10
3 5 20
3 4 17
**/
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). Input Line 1: Three space-separated integers: N, ML, and MD. 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: Source |
Time Limit: 1000MS | | Memory Limit: 65536K |
Total Submissions: 11054 | | Accepted: 5316 |
[Submit] [Go Back] [Status] [Discuss]
Go Back