二次联通门 : LibreOJ #102. 最小费用流

/*
LibreOJ #102. 最小费用流 Spfa跑花费
记录路径
倒推回去 */
#include <cstring>
#include <cstdio>
#include <queue> #define Max 10000 void read (int &now)
{
now = ;
register char word = getchar ();
while (word > '' || word < '')
word = getchar ();
while (word >= '' && word <= '')
{
now = now * + word - '';
word = getchar ();
}
} inline int min (int a, int b)
{
return a < b ? a : b;
} class Cost_Flow_Type
{ private : int __to[Max * ];
int __next[Max * ];
int __flow[Max * ], __cost[Max * ]; int edge_list[Max];
int Edge_Count; int dis[Max], can_flow[Max];
bool visit[Max];
int pre[Max]; public : Cost_Flow_Type ()
{
Edge_Count = ;
} inline void Insert_edge (int from, int to, int flow, int cost)
{
Edge_Count ++; __to[Edge_Count] = to;
__next[Edge_Count] = edge_list[from];
edge_list[from] = Edge_Count; __flow[Edge_Count] = flow;
__cost[Edge_Count] = cost; Edge_Count ++; __to[Edge_Count] = from;
__next[Edge_Count] = edge_list[to];
edge_list[to] = Edge_Count; __flow[Edge_Count] = ;
__cost[Edge_Count] = -cost;
} bool Spfa (int Start, int End)
{
memset (dis, 0x3f, sizeof dis);
memset (visit, false, sizeof visit); std :: queue <int> Queue;
register int now; visit[Start] = true;
can_flow[Start] = dis[];
pre[Start] = ; for (Queue.push (Start), dis[Start] = ; !Queue.empty (); Queue.pop ())
{
now = Queue.front (); visit[now] = false; for (int i = edge_list[now]; i; i = __next[i])
if (__flow[i] && dis[__to[i]] > dis[now] + __cost[i])
{
dis[__to[i]] = dis[now] + __cost[i];
pre[__to[i]] = i; can_flow[__to[i]] = min (can_flow[now], __flow[i]); if (!visit[__to[i]])
{
Queue.push (__to[i]);
visit[__to[i]] = true;
}
}
} return dis[End] < dis[];
} void Get_Cost_Flow (int Start, int End)
{
int res = ;
int Cost_ = ;
for (int x; Spfa (Start, End); )
{
x = can_flow[End];
for (int i = End; i != Start; i = __to[pre[i] ^ ])
{
__flow[pre[i]] -= x;
__flow[pre[i] ^ ] += x;
}
res += x;
Cost_ += dis[End] * x;
} printf ("%d %d", res, Cost_);
}
}; int N, M; Cost_Flow_Type Make; int main (int argc, char *argv[])
{
int x, y, z, Flandre; for (read (N), read (M); M --; )
{
read (x);
read (y);
read (z);
read (Flandre); Make.Insert_edge (x, y, z, Flandre);
} Make.Get_Cost_Flow (, N); return ;
}

LibreOJ #102. 最小费用流的更多相关文章

  1. loj 102 最小费用流

    补一发费用流的代码 %%%棒神 #include<iostream> #include<cstdio> #include<cstring> #include< ...

  2. LibreOJ 题解汇总

    目录 #1. A + B Problem #2. Hello, World! #3. Copycat #4. Quine #7. Input Test #100. 矩阵乘法 #101. 最大流 #10 ...

  3. Entity Framework 6 Recipes 2nd Edition(10-2)译 -> 返回输出参数

    10-2. 返回输出参数 问题 想获取存储过程里的一个或多个输出参数的值 解决方案 假设我们有一个像Figure 10-1所示的,出租车辆与租金收入的模型 Figure 10-1.出租车辆与租金收入的 ...

  4. 【安卓】aidl.exe E 10744 10584 io_delegate.cpp:102] Error while creating directories: Invalid argument

    这几天在使用.aidl文件的时候eclipse的控制台总是爆出如下提示: aidl.exe E 10744 10584 io_delegate.cpp:102] Error while creatin ...

  5. POJ2195 最小费用流

    题目:http://poj.org/problem?id=2195 处理出每个人到每个门的曼哈顿距离,分别建立容量为1费用为曼哈顿距离的边,在源点和每个人人之间建立容量为1费用为0的边,在门和汇点之间 ...

  6. HDU 4067 hdoj 4067 Random Maze 最小费用流

    给出n个点,m条边,入口s和出口t,对于每条边有两个值a,b,如果保留这条边需要花费:否则,移除这条边需要花费b. 题目要求用最小费用构造一个有向图满足以下条件: 1.只有一个入口和出口 2.所有路都 ...

  7. POJ 2516:Minimum Cost(最小费用流)

    https://vjudge.net/problem/11079/origin 题意:有N个商店和M个供应商和K种物品,每个商店每种物品有一个需求数,每个供应商每种物品有一个供应量,供应商到商店之间的 ...

  8. POJ-2175 Evacuation Plan 最小费用流、负环判定

    题意:给定一个最小费用流的模型,根据给定的数据判定是否为最优解,如果不为最优解则给出一个比给定更优的解即可.不需要得出最优解. 解法:由给定的数据能够得出一个残图,且这个图满足了最大流的性质,判定一个 ...

  9. Going Home (hdu 1533 最小费用流)

    集训的图论都快结束了,我才看懂了最小费用流,惭愧啊. = = 但是今天机械键盘到了,有弄好了自行车,好高兴\(^o^)/~ 其实也不是看懂,就会套个模板而已.... 这题最重要的就是一个: 多组输入一 ...

随机推荐

  1. Spring (2)框架

    Spring第二天笔记 1. 使用注解配置Spring入门 1.1. 说在前面 学习基于注解的IoC配置,大家脑海里首先得有一个认知,即注解配置和xml配置要实现的功能都是一样的,都是要降低程序间的耦 ...

  2. mongo与spring集合

    1.加入lib包,在Maven中 <dependency> <groupId>org.springframework.data</groupId> <arti ...

  3. 微信小程序页面滚动到指定位置

    页面上有一个元素或者组件,id 为 comment 则: var me = this; var query = wx.createSelectorQuery().in(me); query.selec ...

  4. 使用github经验

    使用github经验 良好的使用习惯,就像是每天来看朋友圈一样,不一定每天都有东西要提交,但是一定要一直有一个 repository 在维护,持续的提交代码.同时也要注意自己的 repository的 ...

  5. SQL Server系统函数:日期函数

    原文:SQL Server系统函数:日期函数 1.返回当前日期和时间 select GETDATE() '当前日期-精确到33毫秒' select GETUTCDATE() 'UTC日期和时间-精确到 ...

  6. [C#] LINQ之SelectMany和GroupJoin

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  7. class类 - extends

    继承是面向对象中一个比较核心的概念.ES6 class的继承与java的继承大同小异,如果学过java的小伙伴应该很容易理解,都是通过extends关键字继承.相较于ES5当中通过原型链继承要清晰和方 ...

  8. Java 面向对象(四)继承

    一.继承的概述(Inherited) 1.由来 多个类中存在相同属性和行为时,将这些内容抽取到单独一个类中,那么多个类无需再定义这些属性和行为,只要继承那个类即可. 其中,多个类可以称为 子类(派生类 ...

  9. Android开发中UI相关的问题总结

    UI设计和实现是Android开发中必不可少的部分,UI做不好的话,丑到爆,APP性能再好,估计也不会有多少人用吧,而且如果UI和业务代码逻辑中间没有处理好,也会很影响APP的性能的.稍微总结一下,开 ...

  10. 13.MyBatis注解式开发

    mybatis 的注解,主要是用于替换映射文件.而映射文件中无非存放着增.删.改.查 的 SQL 映射标签.所以,mybatis 注解,就是要替换映射文件中的 SQL 标签. mybatis 官方文档 ...