【nowcoder网络流】Birthday 思维建图

原题链接:https://ac.nowcoder.com/acm/problem/19802

题意

在这里插入图片描述

分析

题目唯一的难点是x^2该如何去表示

我们写出来看一下1, 4, 9, 16, 25…

我们再观察一下差1, 3, 5, 7, 9…

惊奇发现居然是等差数列,那么我们在给点加边的时候可以一次性加入n条边,分别代表第i次加入的费用。

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const ll INF = 1e9;
const int N = 500 + 10;
const int M = 3e6 + 10;
const int MOD = 1e9 + 7;
int st, ed;
struct node {
    int maxflow, mincost, cnt;
    int vis[N], dis[N], pre[N], last[N], h[N], flow[N];
    struct edge {
        int to, next, cap, cos;
    } e[M << 1];

    void add(int u, int v, int cap, int cos) {
        e[cnt].to = v;
        e[cnt].cap = cap;
        e[cnt].cos = cos;
        e[cnt].next = h[u];
        h[u] = cnt++;

        e[cnt].to = u;
        e[cnt].cap = 0;
        e[cnt].cos = -cos;
        e[cnt].next = h[v];
        h[v] = cnt++;
    }

    void init() {
        memset(h, -1, sizeof h);
        cnt = 0;
        mincost = maxflow = 0;
    }
    bool spfa() {
        queue<int> q;
        for (int i = 0; i <= ed; i++) dis[i] = INF, vis[i] = 0;
        vis[st] = 1, dis[st] = 0, flow[st] = INF;
        q.push(st);
        while (q.size()) {
            int u = q.front();
            q.pop(); vis[u] = 0;
            for (int i = h[u]; ~i; i = e[i].next) {
                int v = e[i].to;
                if (e[i].cap && dis[v] > dis[u] + e[i].cos) {
                    dis[v] = e[i].cos + dis[u];
                    flow[v] = min(flow[u], e[i].cap);
                    pre[v] = u;
                    last[v] = i;
                    if (!vis[v]) {
                        vis[v] = 1;
                        q.push(v);
                    }
                }
            }
        }
        if (dis[ed] != INF) return true;
        else return false;
    }

    void MCMF() {
        while (spfa()) {
            int now = ed;
            maxflow += flow[ed];
            mincost += flow[ed] * dis[ed];

            while (st != now) {
                e[last[now]].cap -= flow[ed];
                e[last[now] ^ 1].cap += flow[ed];
                now = pre[now];
            }
        }
    }
}mcmf;

void solve() {
    int n, m; cin >> n >> m;
    mcmf.init();
    st = n + m + 1, ed = n + m + 2;
    for (int i = 1; i <= n; i++) {
        int a, b; cin >> a >> b;
        mcmf.add(i, a + n, 1, 0);
        mcmf.add(i, b + n, 1, 0);
    }
    for (int i = 1; i <= n; i++) {
        mcmf.add(st, i, 1, 0);
    }
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            mcmf.add(i + n, ed, 1, j * 2 - 1);
        }
    }
    mcmf.MCMF();
    cout << mcmf.mincost << endl;
}
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#ifdef ACM_LOCAL
    freopen("input", "r", stdin);
    freopen("output", "w", stdout);
    signed test_index_for_debug = 1;
    char acm_local_for_debug = 0;
    do {
        if (acm_local_for_debug == '$') exit(0);
        if (test_index_for_debug > 20)
            throw runtime_error("Check the stdin!!!");
        auto start_clock_for_debug = clock();
        solve();
        auto end_clock_for_debug = clock();
        cout << "Test " << test_index_for_debug << " successful" << endl;
        cerr << "Test " << test_index_for_debug++ << " Run Time: "
             << double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
        cout << "--------------------------------------------------" << endl;
    } while (cin >> acm_local_for_debug && cin.putback(acm_local_for_debug));
#else
    solve();
#endif
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目标检测(Object Detection)是计算机视觉领域的一个核心问,其主要任务是找出图像中所有感兴趣的目标(物体),并确定它们的类别和位置。以下是对目标检测的详细阐述: 一、基本概念 目标检测的任务是解决“在哪里?是什么?”的问,即定位出图像中目标的位置并识别出目标的类别。由于各类物体具有不同的外观、形状和姿态,加上成像时光照、遮挡等因素的干扰,目标检测一直是计算机视觉领域最具挑战性的任务之一。 二、核心问 目标检测涉及以下几个核心问: 分类问:判断图像中的目标属于哪个类别。 定位问:确定目标在图像中的具体位置。 大小问:目标可能具有不同的大小。 形状问:目标可能具有不同的形状。 三、算法分类 基于深度学习的目标检测算法主要分为两大类: Two-stage算法:先进行区域生成(Region Proposal),生成有可能包含待检物体的预选框(Region Proposal),再通过卷积神经网络进行样本分类。常见的Two-stage算法包括R-CNN、Fast R-CNN、Faster R-CNN等。 One-stage算法:不用生成区域提议,直接在网络中提取特征来预测物体分类和位置。常见的One-stage算法包括YOLO系列(YOLOv1、YOLOv2、YOLOv3、YOLOv4、YOLOv5等)、SSD和RetinaNet等。 四、算法原理 以YOLO系列为例,YOLO将目标检测视为回归问,将输入图像一次性划分为多个区域,直接在输出层预测边界框和类别概率。YOLO采用卷积网络来提取特征,使用全连接层来得到预测值。其网络结构通常包含多个卷积层和全连接层,通过卷积层提取图像特征,通过全连接层输出预测结果。 五、应用领域 目标检测技术已经广泛应用于各个领域,为人们的生活带来了极大的便利。以下是一些主要的应用领域: 安全监控:在商场、银行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值