POJ 1716 Integer intervals SPFA differential constraint

Source: Internet
Author: User
Use array dist[i] to indicate the number of key points contained in point 0 to I-1;
Input data can be converted to: dist[b+1]-dist[a]>=2 = dist[a]-dist[b+1]<=-2
There are also two constraints, depending on the actual conditions:
Dist[i+1]-dist[i]<=1
Dist[i+1]-dist[i]>=0 = dist[i]-dist[i]+1<=0
So you can build a picture, SPFA.

Last output DIST[MAX]-DIST[MIN] (max and Min are the maximum and minimum points that occur for the input data)

/* id:sdj22251 prog:subset lang:c++ * * #include <iostream> #include <vector> #include <list> #include <map> #include <set> #include <deque> #include <queue> #include <stack> #include <bitse t> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> # Include <cctype> #include <map> #include <string> #include <cstring> #include <cmath> #
Include <ctime> #define LOCA #define MAXN 50500 #define INF 1000000000 #define EPS 1e-7 using namespace std; struct Node {int V, W, next;}
EDGE[MAXN * 10];
int HEAD[MAXN], E, MX, MI, D[MAXN], q[120 * MAXN], VIS[MAXN];
    void spfa (int src) {for (int i = mi; i <= mx; i++) d[i] = INF;
    int h = 0, t = 1;
    D[SRC] = 0;
    Q[0] = src;
    VIS[SRC] = 1;
 while (H < T) {       int u = q[h++];
        Vis[u] = 0;
            for (int i = head[u]; i =-1; i = edge[i].next) {int v = EDGE[I].V;
            int w = EDGE[I].W;
                if (D[v] > D[u] + W) {D[v] = D[u] + W;
                    if (!vis[v]) {q[t++] = V;
                VIS[V] = 1;
    }}}}} void insert (const int &AMP;X, const int &y, const int &w) {edge[e].v = y;
    EDGE[E].W = W;
    Edge[e].next = Head[x];
HEAD[X] = e++;
    } int main () {int n, u, v;
    scanf ("%d", &n);
    Memset (Head,-1, sizeof (head));
        for (int i = 1; I <= n; i++) {scanf ("%d%d", &u, &v);
        if (v + 1 > mx) mx = v + 1;
        if (Mi > U) mi = u;
        Insert (v + 1, u,-2);
    Insert (U, v + 1, v-u + 1);
        } for (int i = 0; i < mx; i++) {Insert (I, i + 1, 1);
    Insert (i + 1, I, 0); } SPFA (MX);
    printf ("%d\n", D[mx]-d[mi]);
return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.