Collect Jewel

Collect Jewel

https://ac.nowcoder.com/acm/problem/15331

最小费用最大流

题意:

图片说明

分析:

这是我第一次做最小费用最大流的题目。感觉还行,写个题解留作纪念。

意识到这是个关于最小费用最大流问题并不难。但是,关键在于如何建图。
拆点是一定的。我刚开始想,对每一个节点,拆成两个。原节点到拆点一条c=1 d=-vi 的路
拆点到原节点一条c=inf,d=0的路。但是这时不行的!
因为这会造成负圈!这真的太致命了。
所以,我们不能这样建图。我们可以这样建图,原节点到拆点建两条边,一条c=1,d=-vi 另一条c=inf,d=0
然后原节点到其他节点的边,都变成拆点到其他节点的边。这样我们也可以实现。
注意,这里有一个条件1<=u<v<=n 所以,我们这样做不会有负圈的。

然后造个起点,连所有的原节点,建个终点连所有拆点。
然后我们跑个最小费用最大流,再取个相反数就可以了。

代码如下:

#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
typedef pair<int, int> pii;
const int max_n = 250;
const int max_m = 1100;
const int inf = 2e9;
int n, m, k;

struct edge
{
    int to, cap, rev, cost, next;
}E[max_m * 4];
int head[max_n];
int cnt = 1;
void add(int from, int to, int cap, int cost) {
    E[cnt].to = to;E[cnt].cap = cap;
    E[cnt].cost = cost;E[cnt].rev = cnt + 1;
    E[cnt].next = head[from];head[from] = cnt++;

    E[cnt].to = from;E[cnt].cap = 0;
    E[cnt].cost = -cost;E[cnt].rev = cnt - 1;
    E[cnt].next = head[to];head[to] = cnt++;
}

int dist[max_n];
bool used[max_n];
pii fa[max_n];
bool spfa(int s,int t) {
    fill(dist, dist + max_n, inf);
    fill(used, used + max_n, false);
    dist[s] = 0;used[s] = true;
    queue<int> que;que.push(s);
    while (!que.empty()) {
        int u = que.front();que.pop();
        used[u] = false;
        for (int i = head[u];i;i = E[i].next) {
            edge& e = E[i];
            if (e.cap <= 0 || dist[e.to] <= dist[u] + e.cost)continue;
            dist[e.to] = dist[u] + e.cost;
            fa[e.to] = { u,i };
            if (used[e.to]) continue;
            que.push(e.to);
            used[e.to] = true;
        }
    }return dist[t] != inf;
}
int matchpath(int s,int t,int& f) {
    int d = f;int res = 0;
    for (int cur = t;cur != s;cur = fa[cur].first) {
        edge& e = E[fa[cur].second];
        d = min(d, e.cap);
    }
    f -= d;res += d * dist[t];
    for (int cur = t;cur != s;cur = fa[cur].first) {
        edge& e = E[fa[cur].second];
        e.cap -= d;E[e.rev].cap += d;
    }
    return res;
}
int mcf(int s,int t,int f) {
    int res = 0;int cost = 0;
    while (f > 0 && spfa(s, t)) {
        cost = matchpath(s, t, f);
        res += cost;
    }return res;
}
void init() {
    fill(head, head + max_n, false);
    cnt = 1;
}

int main() {
    ios::sync_with_stdio(0);
    int T;cin >> T;
    for (int t = 1;t <= T;t++) {
        cin >> n >> m >> k;init();
        int start = n + 1;int ed = start + 1;
        for (int i = 1;i <= n;i++) {
            int val;cin >> val;
            add(start, i, inf, 0);
            add(i, i + ed, 1, -val);
            add(i, i + ed, inf, 0);
            add(i + ed, ed, inf, 0);
        }
        for (int i = 1;i <= m;i++) {
            int u, v, cost;
            cin >> u >> v >> cost;
            add(u + ed, v, inf, cost);
        }cout << "Case #" << t << ": " << -mcf(start, ed, k) << endl;
    }
}
全部评论

相关推荐

老粉都知道小猪猪我很久没更新了,因为秋招非常非常不顺利,emo了三个月了,接下来说一下我的情况吧本人是双非本&nbsp;专业是完全不着计算机边的非科班,比较有优势的是有两段大厂实习,美团和字节。秋招面了50+场泡池子泡死的:滴滴&nbsp;快手&nbsp;去哪儿&nbsp;小鹏汽车&nbsp;不知名的一两个小厂其中字节13场&nbsp;两次3面挂&nbsp;两次2面挂&nbsp;一次一面挂其中有2场面试题没写出来,其他的都是全a,但该挂还是挂,第三次三面才面进去字节,秋招加暑期总共面了22次字节,在字节的面评可以出成书了快手面了8场,2次实习的,通过了但没去,一次2面挂&nbsp;最后一次到录用评估&nbsp;至今无消息滴滴三面完&nbsp;没几天挂了&nbsp;所有技术面找不出2个问题是我回答不上来的,三面还来说我去过字节,应该不会考虑滴滴吧,直接给我干傻了去哪儿一天速通&nbsp;至今无消息小鹏汽车hr&nbsp;至今无消息美团2面挂&nbsp;然后不捞我了,三个志愿全部结束,估计被卡学历了虾皮二面挂&nbsp;这个是我菜,面试官太牛逼了拼多多二面挂&nbsp;3道题也全写了&nbsp;也没问题是回答不出来的&nbsp;泡一周后挂腾讯面了5次&nbsp;一次2面挂&nbsp;三次一面挂,我宣布腾讯是世界上最难进的互联网公司然后还有一些零零散散的中小厂,但是数量比较少,约面大多数都是大厂。整体的战况非常惨烈,面试机会少,就算面过了也需要和各路神仙横向对比,很多次我都是那个被比下去的人,不过这也正常,毕竟谁会放着一个985的硕士不招,反而去招一个双非读化学的小子感觉现在互联网对学历的要求越来越高了,不仅仅要985还要硕士了,双非几乎没啥生存空间了,我感觉未来几年双非想要进大厂开发的难度应该直线上升了,唯一的打法还是从大二刷实习,然后苟个转正,不然要是去秋招大概率是炮灰。而且就我面字节这么多次,已经开始问很多ai的东西了,你一破本科生要是没实习没科研懂什么ai啊,纯纯白给了
不知名牛友_:爸爸
秋招你被哪家公司挂了?
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务