当前位置: 首页 > news >正文

[BZOJ1877][SDOI2009]晨跑[最大流+费用流]

天数最多 长度最小 天数是流量,长度是费用

每个点拆成两个点限流1,就能保证只走一次 然后跑费用流

#include <bits/stdc++.h>
using namespace std;
#define MP make_pair
#define pb push_back
#define read2(a, b) (read(a), read(b))
#define read3(a, b, c) (read(a), read(b), read(c))
#define lop(i,a,b) for(register int i = (a); i <= (b); ++i)
#define dlop(i,a,b) for(register int i = (a); i >= (b); --i)
#define eps (1e-7)
#define fir first
#define sec second
    
const int inf = 0x3f3f3f3f-1;
const int MAXN = 4e2+7;
const int MAXM = 8e4+7;
typedef long long LL;
typedef long double LF;
typedef pair<int,int> pii;
typedef unsigned long long ULL;
typedef unsigned int uint;
template<class T> void read(T & x) {
  register int c = getchar(), f = 1;x = 0;
  while(!isdigit(c)) {if (c == '-') f = -f;c = getchar();}
  while(isdigit(c)) x = x * 10 + c - '0', c = getchar();
  x *= f;
}
    
int n, m, k, maxflow, dep[MAXN], s, t, cur[MAXN], head[MAXN], ans, dis[MAXN];
bool inq[MAXN];
    
struct Edge {
  int u, v, w, c, next;
} G[MAXM]; int tot = 1;
inline void add(int u, int v, int w, int c) {
  // cout << u << ' ' << v << ' ' << w << endl;
  G[++tot] = (Edge){u, v, w, c, head[u]}; head[u] = tot;
  G[++tot] = (Edge){v, u, 0, -c, head[v]}; head[v] = tot;
} 
bool spfa(int s, int t) {
  memset(dis, 0x3f, sizeof dis);
  memset(inq, 0, sizeof inq);
  queue<int>q; q.push(s); dis[s] = 0, inq[s] = 1;
  while(!q.empty()) {
    int u = q.front();
    inq[u] = 0;
    q.pop();
    for(int i = head[u]; i; i = G[i].next) {
      int v = G[i].v, w = G[i].w, c = G[i].c;
      if (dis[v] > dis[u] + c && w) {
        dis[v] = dis[u] + c;
        cur[v] = i;
        if (!inq[v]) q.push(v), inq[v] = 1;
      }
    }
  }
  return dis[t] < inf; 
}
  
void update(int s, int t) {
  int i, x = inf;
  for(i = cur[t]; i; x = min(x, G[i].w), i = cur[G[i].u]);
  for(i = cur[t]; i; G[i].w -= x, G[i^1].w += x, ans += x * G[i].c, i = cur[G[i].u]);
  maxflow += x;
}
 
void EK(int s, int t) {
  while(spfa(s, t)) update(s, t);
}
 
int main(void) {
  // freopen("data.in", "r", stdin);
  read2(n, m);
  for(int i = 2; i < n; ++i) add(i, i+n, 1, 0);
  for(int u, v, w, i = 1; i <= m; ++i) {
    read3(u, v, w); add(u+n, v, 1, w);
  }
  EK(1+n, n);
  cout << maxflow << ' ' << ans;
  return 0;
}

转载于:https://www.cnblogs.com/storz/p/10191475.html

相关文章:

  • 构造字符串 之 hdu 4850 Wow! Such String!
  • Nginx负载均衡demo
  • Mysql 字符串截取
  • Go语言的序列化与反序列化(gob)
  • SqlServer将表中数据复制到另一张表
  • 哈罗单车融资几十亿元,蚂蚁金服与春华资本加持 ...
  • hdu 1222 Wolf and Rabbit
  • css实现移入文字顶部出现提示的效果
  • 【转】使用 Android 的日志工具LogCat
  • 原创教程“ActionScript3.0游戏中的图像编程”开始连载啦!
  • c++避免掩盖继承来的名称
  • MySQL列的默认值主键索引与自增 删除增加与修改
  • DoraemonKit,一款功能齐全的客户端 (iOS、Android) 研发助手,你值得拥有。
  • 欧美斯项目签到功能,实时获取当前所在位置的经纬度
  • 云原生的浪潮下,为什么运维人员适合学习Go语言?
  • [分享]iOS开发 - 实现UITableView Plain SectionView和table不停留一起滑动
  • 【跃迁之路】【733天】程序员高效学习方法论探索系列(实验阶段490-2019.2.23)...
  • exports和module.exports
  • isset在php5.6-和php7.0+的一些差异
  • js算法-归并排序(merge_sort)
  • JWT究竟是什么呢?
  • Linux gpio口使用方法
  • Linux下的乱码问题
  • Promise面试题2实现异步串行执行
  • Rancher-k8s加速安装文档
  • Redux系列x:源码分析
  • 彻底搞懂浏览器Event-loop
  • 从输入URL到页面加载发生了什么
  • 聊一聊前端的监控
  • 强力优化Rancher k8s中国区的使用体验
  • 全栈开发——Linux
  • 如何借助 NoSQL 提高 JPA 应用性能
  • 如何优雅地使用 Sublime Text
  • 深入浅出Node.js
  • 什么是Javascript函数节流?
  • 吴恩达Deep Learning课程练习题参考答案——R语言版
  • 在electron中实现跨域请求,无需更改服务器端设置
  • (1)SpringCloud 整合Python
  • (32位汇编 五)mov/add/sub/and/or/xor/not
  • (C++17) optional的使用
  • (C语言)求出1,2,5三个数不同个数组合为100的组合个数
  • (Redis使用系列) SpirngBoot中关于Redis的值的各种方式的存储与取出 三
  • (免费领源码)Java#Springboot#mysql农产品销售管理系统47627-计算机毕业设计项目选题推荐
  • (十)【Jmeter】线程(Threads(Users))之jp@gc - Stepping Thread Group (deprecated)
  • (转)fock函数详解
  • (转)jQuery 基础
  • . NET自动找可写目录
  • .bat批处理(五):遍历指定目录下资源文件并更新
  • .NET Core 网络数据采集 -- 使用AngleSharp做html解析
  • .Net(C#)常用转换byte转uint32、byte转float等
  • .NET中的Event与Delegates,从Publisher到Subscriber的衔接!
  • @AutoConfigurationPackage的使用
  • @Autowired 与@Resource的区别
  • @zabbix数据库历史与趋势数据占用优化(mysql存储查询)
  • @基于大模型的旅游路线推荐方案