bzoj2330 糖果

糖果

题目背景:

bzoj2330

分析:差分约束,本题还是比较好想的直接根据题意建边就好,一下分析各个操作:

1a = b è a >= b && b >= a è a >= b + 0, b >= a + 0 è add_edge a to b, dis = 0, add_edge b to a, dis = 0;

2: b > a è b >= a + 1 è add_edge a to b, dis = 1;

3: a >= b è a >= b + 0 è add_edge b to a, dis = 0;

4: a > b è a >= b + 1 è add_edge b to a, dis = 1;

5: b >= a è b >= a + 0 è add_edge a to b, dis = 0;

之后,因为每一个点一定大于等于1,所以建立起点Sadd_edge S to i(1 <= i <= n), dis = 1,然后S = 0,从S开始用spfa跑最长路,各个点的距离即为最少需要的糖果,如果判断存在正环,则表示不能满足,输出-1,否则sum_dis为答案。

Source

/*
    created by scarlyw
*/
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <cctype>
#include <vector>
#include <set>
#include <queue>
    
inline char read() {
    static const int IN_LEN = 1024 * 1024;
    static char buf[IN_LEN], *s, *t;
    if (s == t) {
        t = (s = buf) + fread(buf, 1, IN_LEN, stdin);
        if (s == t) return -1;
    }
    return *s++;
}
    
///*
template<class T>
inline void R(T &x) {
    static char c;
    static bool iosig;
    for (c = read(), iosig = false; !isdigit(c); c = read()) {
        if (c == -1) return ;
        if (c == '-') iosig = true; 
    }
    for (x = 0; isdigit(c); c = read()) 
        x = ((x << 2) + x << 1) + (c ^ '0');
    if (iosig) x = -x;
}
//*/
    
const int OUT_LEN = 1024 * 1024;
char obuf[OUT_LEN], *oh = obuf;
inline void write_char(char c) {
    if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;
    *oh++ = c;
}
    
template<class T>
inline void W(T x) {
    static int buf[30], cnt;
    if (x == 0) write_char('0');
    else {
        if (x < 0) write_char('-'), x = -x;
        for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
        while (cnt) write_char(buf[cnt--]);
    }
}
    
inline void flush() {
    fwrite(obuf, 1, oh - obuf, stdout);
}
    
/*
template<class T>
inline void R(T &x) {
    static char c;
    static bool iosig;
    for (c = getchar(), iosig = false; !isdigit(c); c = getchar())
        if (c == '-') iosig = true; 
    for (x = 0; isdigit(c); c = getchar()) 
        x = ((x << 2) + x << 1) + (c ^ '0');
    if (iosig) x = -x;
}
//*/
   
const int MAXN = 100000 + 10;
const long long INF = 1e17;
   
int n, m, x, a, b;
long long dis[MAXN];
int cnt[MAXN];
bool vis[MAXN];
   
struct node {
    int to, w;
    node(int to = 0, int w = 0) : to(to), w(w) {}
} ;
   
std::vector<node> edge[MAXN];
   
inline void add_edge(int x, int y, int z) {
    edge[x].push_back(node(y, z));
}
   
inline void read_in() {
    R(n), R(m);
    for (int i = 1; i <= m; ++i) {
        R(x), R(a), R(b);
        switch (x) {
            case 1 : add_edge(a, b, 0), add_edge(b, a, 0);
                     break ;
            case 2 : add_edge(a, b, 1);
                     break ;
            case 3 : add_edge(b, a, 0);
                     break ;
            case 4 : add_edge(b, a, 1);
                     break ;
            case 5 : add_edge(a, b, 0);
                     break ;
        }
    }
       
    for (int i = n; i >= 1; --i) add_edge(0, i, 1);
}
   
inline void spfa(int s) {
    static std::queue<int> q;
    for (int i = 1; i <= n; ++i) dis[i] = -INF;
    dis[s] = 0, q.push(s), vis[s] = true, cnt[s]++; 
    while (!q.empty()) {
        int cur = q.front(); 
        q.pop(), vis[cur] = false;
        for (int p = edge[cur].size() - 1; p >= 0; --p) {
            node *e = &edge[cur][p];
            if (dis[e->to] < dis[cur] + e->w) {
                dis[e->to] = dis[cur] + e->w;
                if (!vis[e->to]) {
                    vis[e->to] = true, q.push(e->to), cnt[e->to]++;
                    if (cnt[e->to] == n + 1) std::cout << "-1", exit(0);
                }
            }
        }
    }
       
    long long sum = 0;
    for (int i = 1; i <= n; ++i) sum += dis[i];
    std::cout << sum;
}
   
int main() {
    read_in();
    spfa(0);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值