zoukankan      html  css  js  c++  java
  • UVA1723 Intervals

    这题$n$倍经验……

    考虑差分约束:

    我们设$s_i$表示$[-1, i]$这个区间中数字的种类数,那么一个条件的限制相当于$s_{b_i} - s_{a_i - 1} leq c_i$,那么连边$(a_i - 1, b_i, c_i)$。

    再挖掘一些隐含条件:$0 leq s_i - s_{i - 1} leq 1$,那么再连边$(i - 1, i, 0)$和$(i, i - 1, -1)$。

    然后从$s_{-1}$开始跑最长路即可,因为题目中保证了$c_i leq b_i - a_i + 1$,所以不会有正环,也就是说最后的$dis_{50000}$就是答案。

    时间复杂度$O(spfa ???)$。

    感觉这些最长路的题目反正要把边权倒过来,不如直接用最短路建模求解。

    Code:

    #include <cstdio>
    #include <cstring>
    #include <queue>
    using namespace std;
    
    const int N = 50010;
    const int M = 2e5 + 5;
    const int Maxn = 50002;
    
    int testCase, n, tot, head[N], dis[N];
    bool vis[N];
    
    struct Edge {
        int to, nxt, val;
    } e[M];
    
    inline void add(int from, int to, int val) {
        e[++tot].to = to;
        e[tot].val =val;
        e[tot].nxt = head[from];
        head[from] = tot;
    }
    
    inline void read(int &X) {
        X = 0; char ch = 0; int op = 1;
        for(; ch > '9' || ch < '0'; ch = getchar())
            if(ch == '-') op = -1;
        for(; ch >= '0' && ch <= '9'; ch = getchar())
            X = (X << 3) + (X << 1) + ch - 48;
        X *= op;
    }
    
    queue <int> Q;
    void spfa(int st) {
        memset(vis, 0, sizeof(vis));
        memset(dis, 0x3f, sizeof(dis));
        vis[st] = 1, dis[st] = 0;
        Q.push(st);
        for(; !Q.empty(); ) {
            int x = Q.front(); Q.pop();
            vis[x] = 0;
            for(int i = head[x]; i; i = e[i].nxt) {
                int y = e[i].to;
                if(dis[y] > dis[x] + e[i].val) {
                    dis[y] = dis[x] + e[i].val;
                    if(!vis[y]) {
                        vis[y] = 1;
                        Q.push(y);
                    }
                }
            }
        }
    }
    
    int main() {
        for(read(testCase); testCase--; ) {
            read(n);
            tot = 0; memset(head, 0, sizeof(head));
            for(int x, y, v, i = 1; i <= n; i++) {
                read(x), read(y), read(v);
                x += 1, y += 2;
                add(x, y, -v);
            }
            for(int i = 1; i < Maxn; i++) add(i, i + 1, 0);
            for(int i = Maxn; i > 1; i--) add(i, i - 1, 1);
    
            spfa(1);
    
            printf("%d
    ", -dis[Maxn]);
            if(testCase) printf("
    ");
        }
        return 0;
    }
    View Code
  • 相关阅读:
    采用坐标变换(移动和旋转)画箭头
    学会Func
    一个链接器的实现
    linux内核skb操作
    终于实现samba可写不可删除
    删掉SafeDrv病毒(这个病毒有点意思)
    Writing a ServiceMain Function(使用RegisterServiceCtrlHandler函数)
    利用Winscp,Putty实现Windows下编写Linux程序
    联发科6亿美元将大陆子公司卖给四维图新(180个人价值6亿美元)
    TFTP:简单文本传输协议,BOOTP:引导程序协议
  • 原文地址:https://www.cnblogs.com/CzxingcHen/p/9803433.html
Copyright © 2011-2022 走看看