【最短路建模】【d[u] <= d[v] + g[v][u] v--->u】【差分系统】

这篇博客探讨了如何使用差分系统来建模最短路径问题,详细介绍了输入、输出格式,并通过示例输入和输出进行了解释。

Layout
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8651 Accepted: 4165

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

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





#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[n];
    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();
    }
}


# P1004 [NOIP 2000 提高组] 方格取数 ## 题目背景 NOIP 2000 提高组 T4 ## 题目描述 设有 $N \times N$ 的方格图 $(N \le 9)$,我们将其中的某些方格中填入正整数,而其他的方格中则放入数字 $0$。如下图所示(见样例): ![](https://cdn.luogu.com.cn/upload/image_hosting/0bpummja.png) 某人从图的左上角的 $A$ 点出发,可以向下行走,也可以向右走,直到到达右下角的 $B$ 点。在走过的路上,他可以取走方格中的数(取走后的方格中将变为数字 $0$)。 此人从 $A$ 点到 $B$ 点共走两次,试找出 $2$ 条这样的路径,使得取得的数之和为大。 ## 输入格式 输入的第一行为一个整数 $N$(表示 $N \times N$ 的方格图),接下来的每行有三个整数,前两个表示位置,第三个数为该位置上所放的数。一行单独的 $0$ 表示输入结束。 ## 输出格式 只需输出一个整数,表示 $2$ 条路径上取得的大的和。 ## 输入输出样例 #1 ### 输入 #1 ``` 8 2 3 13 2 6 6 3 5 7 4 4 14 5 2 21 5 6 4 6 3 15 7 2 14 0 0 0 ``` ### 输出 #1 ``` 67 ``` ## 说明/提示 数据范围:$1\le N\le 9$。这是我的代码,用网络流完成:#include <bits/stdc++.h> using namespace std; struct Dinic { struct Edge { int to, cap, rev; }; int n, s, t; vector< vector<Edge> > g; vector<int> level, it; Dinic(int n): n(n), g(n), level(n), it(n) {} void addEdge(int u, int v, int c) { Edge a{v, c, (int)g[v].size()}; Edge b{u, 0, (int)g[u].size()}; g[u].push_back(a); g[v].push_back(b); } bool bfs() { fill(level.begin(), level.end(), -1); queue<int> q; level[s] = 0; q.push(s); while (!q.empty()) { int u = q.front(); q.pop(); for (auto &e : g[u]) { if (e.cap > 0 && level[e.to] < 0) { level[e.to] = level[u] + 1; q.push(e.to); } } } return level[t] >= 0; } int dfs(int u, int f) { if (u == t) return f; for (int &i = it[u]; i < (int)g[u].size(); ++i) { auto &e = g[u][i]; if (e.cap > 0 && level[e.to] == level[u] + 1) { int ret = dfs(e.to, min(f, e.cap)); if (ret > 0) { e.cap -= ret; g[e.to][e.rev].cap += ret; return ret; } } } return 0; } int maxf(int S, int T) { s = S; t = T; int flow = 0, inf = 1e9; while (bfs()) { fill(it.begin(), it.end(), 0); while (int f = dfs(s, inf)) flow += f; } return flow; } }; struct EdgeInput { int u, v; int w; }; int main() { int N, P, T = 2; cin>> N ; vector<EdgeInput> edges(P); vector<int> weights; weights.reserve(P); for (int i = 0; ; ++i) { int u, v, w; cin >> u >> v >> w; if(u==0&&v==0&&w==0){ break; } edges[i] = {u - 1, v - 1, w}; weights.push_back(w); } sort(weights.begin(), weights.end()); weights.erase(unique(weights.begin(), weights.end()), weights.end()); auto feasible = [&](int W)->bool { Dinic dinic(N); for (auto &e : edges) { if (e.w <= W) { dinic.addEdge(e.u, e.v, 1); dinic.addEdge(e.v, e.u, 1); } } int f = dinic.maxf(0, N - 1); return f >= T; }; int left = 0, right = (int)weights.size() - 1, ans = weights.back(); while (left <= right) { int mid = (left + right) >> 1; if (feasible(weights[mid])) { ans = weights[mid]; right = mid - 1; } else { left = mid + 1; } } cout << ans << "\n"; return 0; }
最新发布
09-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值