P1344 [USACO4.4] 追查坏牛奶 Pollutant Control

Description

给定一张 n n n m m m 边的有向图,求 1 → n 1 \to n 1n 的最小割的容量和边数。

Analysis

第一问是模板,直接套最小割(最大流)即可。
第二问的话,我们可以将流满的边容量改成 1 1 1,没流满的改成 ∞ \infty ,在新图上跑一遍最小割即可求出。

Code

最大流贴的是 jiangly 的模板

// Problem: P1344 [USACO4.4] 追查坏牛奶 Pollutant Control
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1344
// Memory Limit: 128 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;

using i64 = long long;
using ui64 = unsigned long long;
using i128 = __int128;
using ui128 = unsigned __int128;
using f4 = float;
using f8 = double;
using f16 = long double;

template<class T>
bool chmax(T &a, const T &b){
	if(a < b){ a = b; return true; }
	return false;
}

template<class T>
bool chmin(T &a, const T &b){
	if(a > b){ a = b; return true; }
	return false;
}

const int INF = 2e9;

template<class T>
struct MaxFlow {
    struct _Edge {
        int to;
        T cap;
        _Edge(int to, T cap) : to(to), cap(cap) {}
    };
    
    int n;
    T inf;
    std::vector<_Edge> e;
    std::vector<std::vector<int>> g;
    std::vector<int> cur, h;
    
    MaxFlow() {}
    MaxFlow(int n) {
        init(n);
        inf = numeric_limits<T>::max();
    }
    
    void init(int n) {
        this->n = n;
        e.clear();
        g.assign(n, {});
        cur.resize(n);
        h.resize(n);
    }
    
    bool bfs(int s, int t) {
        h.assign(n, -1);
        std::queue<int> que;
        h[s] = 0;
        que.push(s);
        while (!que.empty()) {
            const int u = que.front();
            que.pop();
            for (int i : g[u]) {
                int v = e[i].to;
                T c = e[i].cap;
                if (c > 0 && h[v] == -1) {
                    h[v] = h[u] + 1;
                    if (v == t) {
                        return true;
                    }
                    que.push(v);
                }
            }
        }
        return false;
    }
    
    T dfs(int u, int t, T f) {
        if (u == t) {
            return f;
        }
        auto r = f;
        for (int &i = cur[u]; i < int(g[u].size()); ++i) {
            const int j = g[u][i];
            int v = e[j].to;
            T c = e[j].cap;
            if (c > 0 && h[v] == h[u] + 1) {
                auto a = dfs(v, t, std::min(r, c));
                e[j].cap -= a;
                e[j ^ 1].cap += a;
                r -= a;
                if (r == 0) {
                    return f;
                }
            }
        }
        return f - r;
    }
    void addEdge(int u, int v, T c) {
        g[u].push_back(e.size());
        e.emplace_back(v, c);
        g[v].push_back(e.size());
        e.emplace_back(u, 0);
    }
    T flow(int s, int t) {
        T ans = 0;
        while (bfs(s, t)) {
            cur.assign(n, 0);
            ans += dfs(s, t, std::numeric_limits<T>::max());
        }
        return ans;
    }
    
    std::vector<bool> minCut() {
        std::vector<bool> c(n);
        for (int i = 0; i < n; i++) {
            c[i] = (h[i] != -1);
        }
        return c;
    }
    
    struct Edge {
        int from;
        int to;
        T cap;
        T flow;
    };
    std::vector<Edge> edges() {
        std::vector<Edge> a;
        for (int i = 0; i < e.size(); i += 2) {
            Edge x;
            x.from = e[i + 1].to;
            x.to = e[i].to;
            x.cap = e[i].cap + e[i + 1].cap;
            x.flow = e[i + 1].cap;
            a.push_back(x);
        }
        return a;
    }
};


signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	
	int n, m;
	cin >> n >> m;
	
	// Find the capacity of the mincut.
	MaxFlow<int> G(n);
	for(int i = 0, u, v, w; i < m; i++){
	    cin >> u >> v >> w;
	    u--, v--;
	    G.addEdge(u, v, w);
	}
	
	int ans = G.flow(0, n - 1);
	
    // Find the number of edges of the mincut.
	auto edges = G.edges();

    // Build a new network graph.
	MaxFlow<int> G2(n);
	for(auto &edge: edges){
	    if(edge.cap == edge.flow) G2.addEdge(edge.from, edge.to, 1);
	    else G2.addEdge(edge.from, edge.to, INF);
	}
	int ecnt = G2.flow(0, n - 1);
	
	cout << ans << ' ' << ecnt << endl;
	return 0;
}
### USACO 1.5 回文质数 Problem Solution #### 题目概述 给定一个整数范围,找出该范围内所有的既是回文又是质数的数字并输出。 #### 方法一:素数筛法结合回文判断 此方法先通过埃拉托斯特尼筛法预处理一定范围内的所有质数,再逐一验证这些质数是否为回文数[^1]。 ```cpp #include <iostream> #include <vector> using namespace std; bool isPalindrome(int n) { string str = to_string(n); int len = str.length(); for (int i = 0; i < len / 2; ++i) if (str[i] != str[len - 1 - i]) return false; return true; } const int MAXN = 1e6 + 5; vector<int> primes; void sieve() { vector<bool> prime(MAXN, true); for (long long p = 2; p * p < MAXN; ++p) if (prime[p]) for (long long multiple = p * p; multiple < MAXN; multiple += p) prime[multiple] = false; for (int p = 2; p < MAXN; ++p) if (prime[p] && isPalindrome(p)) primes.push_back(p); } ``` 上述代码实现了对指定区间内所有满足条件的数值进行筛选的功能。首先定义了一个辅助函数`isPalindrome()`用于检测某个正整数n是不是回文结构;接着利用布尔数组标记合数位置完成初步过滤工作,在此基础上进一步挑选出符合条件的目标对象加入到最终的结果列表当中去。 #### 方法二:直接构造特定长度的回文序列 考虑到题目特殊性质(即所求解必然是奇位数且回文),可以尝试按照固定模式构建候选集,之后仅需检验其可除性即可确认是否属于目标集合成员之一[^3]。 ```cpp for (int d1 = 1; d1 <= 9; d1 += 2) { // 奇数才可能是素数 for (int d2 = 0; d2 <= 9; ++d2) { for (int d3 = 0; d3 <= 9; ++d3) { int palindrome = 10000*d1 + 1000*d2 + 100*d3 + 10*d2 + d1; bool flag = true; for (int j = 2; j*j <= palindrome; ++j) if (palindrome % j == 0){ flag = false; break; } if(flag) cout << palindrome << endl; } } } ``` 这段程序片段展示了如何基于三位模板生成五位长的可能答案,并对其进行简单的因式分解测试来决定保留与否的操作逻辑。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值