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

news/2025/2/9 6:02:57

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

每个点拆成两个点限流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


http://www.niftyadmin.cn/n/1840265.html

相关文章

个人电脑上搭建OpenStack的实验室

现在OpenStack越来越成熟&#xff0c;对其感兴趣的人也越来越多&#xff0c;有些初学者苦于没有实验环境&#xff0c;对OpenStack的理解只能停留在官方文档层面&#xff0c;没有办法理论联系实践。我在刚开始接触的时候&#xff0c;也是这样一种状态&#xff0c;有些东西只看文…

MTK芯片资料分享,2018MTK芯片资料大全

MTK芯片资料分享&#xff0c;2018MTK芯片资料大全 估计想要MTK资料的人不在少数&#xff0c;然而小编却发现了这么一个论坛&#xff0c;叫做&#xff1a;闯客技术论坛。里边的MTK芯片型号资料居然有如此之多&#xff0c;而且还是我不想复制的情况下&#xff1a; MT2503 MT6737 …

国内OpenStack项目Core现状

经常有朋友问&#xff0c;国内大概有多少位OpenStack项目的Core。这个问题&#xff0c;现在其实不太好回答&#xff0c;如果需要准确统计的话。下面仅仅是一个大概估计&#xff0c;有遗漏的&#xff0c;希望朋友指出&#xff0c;我来补全。 现在OpenStack项目在 github.com/ope…

服务端开发学习路径图,心疼小哥哥们

关注微信公众号《小姐姐味道》获取更多&#xff5e;&#xff5e; 在github上看到一种图的表现形式很不错&#xff08;https://github.com/kamranahme... &#xff09;&#xff0c;迫不及待的自己做了一张:服务端开发学习路径图&#xff0c;表现力还是很强的。我们从选择一门开发…

高通芯片资料下载大全,这是一个资料下载论坛

上次发布了一篇MTK芯片资料分享&#xff0c;2018MTK芯片资料大全&#xff0c;现在再来更新一篇高通版本的。高通由于一些原因&#xff0c;其芯片资料更是难找&#xff0c;相信很多工程师er都有过刻骨铭心的经历&#xff0c;那么这篇文章&#xff0c;则会带来一点点的希望之光&a…

树莓派搭建git服务器并实现公网访问(二)共2篇—安装git

在某商活动期间购买的云服务器一年当时300多块软妹币&#xff0c;到期后续费要800多。感觉太贵&#xff01;&#xff01;平时该服务器主要用做个人svn服务器&#xff0c;利用率比较低&#xff0c;如果买便宜的新机器总是迁移文件也挺烦人的&#xff0c;综合考虑下就不想再续了&…

JS事件流和事件委托

在上一篇《JS知识点大杂烩》中说到了事件流但没有详细的介绍&#xff0c;这篇文章就来介绍一下事件流。 事件流一共由三个阶段分别是&#xff1a; 1.捕获阶段 2.目标阶段 3.冒泡阶段事件绑定大家都知道&#xff0c;有DOM0级&#xff08;ontype&#xff09;和DOM2级&#xff08;…

2.linux的基础命令之pwd和whoami

常用命令&#xff1a; pwd: 显示当前所在的工作目录实例&#xff1a;[rootitxuezhe ~]# cd /etc/sysconfig/[rootitxuezhe sysconfig]# pwd/etc/sysconfigwhoami命令:显示自身的用户名称&#xff0c;本指令相当于执行"id -un"指令。实例显示用户名[rootitxuezhe ~]# …