zoukankan      html  css  js  c++  java
  • hdu1384Intervals(差分约束)

    #include <bits/stdc++.h>
    using namespace std;
    
    const int maxn = 5e4 + 5;
    const int inf = 0x3f3f3f3f;
    int n, head[maxn], dis[maxn], cnt;
    struct node{
        int to, w, next;
    } ed[maxn*4];       //数组开2*maxn超时,开大一点
    inline void add( int u, int v, int w ){
        ed[cnt].to = v;
        ed[cnt].w = w;
        ed[cnt].next = head[u];
        head[u] = cnt++;
    }
    
    inline int max( int a, int b ){
        return a>b ? a:b;
    }
    
    inline int min( int a, int b ){
        return a<b ? a:b;
    }
    
    inline void spfa( int beg ){
        bool vis[maxn];
        memset( vis, 0, sizeof(vis) );
        memset( dis, -inf, sizeof(dis) );
        queue<int> q;
        q.push(beg);
        dis[beg] = 0;
        vis[beg] = 1;
        while( !q.empty() ){
            int u = q.front();
            q.pop();
            vis[u] = 0;
            for( int i=head[u]; i!=-1; i=ed[i].next ){
                int v = ed[i].to;
                if( dis[v] < dis[u]+ed[i].w ){
                    dis[v] = dis[u]+ed[i].w;
                    if( !vis[v] ){
                        vis[v] = 1;
                        q.push(v);
                    }
                }
            }
        }
    }
    
    int main(){
        while( ~scanf("%d", &n) ){
            cnt = 0;
            int a = inf, b = -1;
            memset( head, -1, sizeof(head) );
            for( int i=0; i<n; i++ ){
                int u, v, w;
                scanf("%d%d%d", &u, &v, &w);
                add( u, v+1, w );
                a = min( a, u );
                b = max( b, v+1 );
            }
            for( int i=a; i<=b; i++ ){
                add( i-1, i, 0 );
                add( i, i-1, -1 );
            }
            spfa(a);
            printf("%d
    ", dis[b]);
        }
    
        return 0;
    }
  • 相关阅读:
    ios布局约束
    IOSanimationDidStop
    iosanimationWithKeyPath
    CALayer的分析
    关于集合的小demo
    关于集合越界后 不能使用迭代器遍历的处理方式
    html--day02
    关于LIst Set Map 异常的知识点---我的笔记
    css入门
    html相关标记的含义
  • 原文地址:https://www.cnblogs.com/WAautomaton/p/10877293.html
Copyright © 2011-2022 走看看