[题目链接]

https://www.lydsy.com/JudgeOnline/problem.php?id=1877

[算法]

不难看出,第一问要求的是最大流,第二问求的是最小费用最大流

注意建图时要将每个点拆成入点和出点,防止经过同一个地点多次

[代码]

#include<bits/stdc++.h>
using namespace std;
#define MAXN 1010
#define MAXM 40010
const int inf = 2e9; struct edge
{
int to,w,cost,nxt;
} e[MAXM << ]; int i,n,m,tot,a,b,c,S,T,ans1,ans2;
int pre[MAXN << ],dist[MAXN << ],incf[MAXN << ],head[MAXN << ]; template <typename T> inline void read(T &x)
{
int f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar())
{
if (c == '-') f = -f;
}
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline void addedge(int u,int v,int w,int cost)
{
tot++;
e[tot] = (edge){v,w,cost,head[u]};
head[u] = tot;
tot++;
e[tot] = (edge){u,,-cost,head[v]};
head[v] = tot;
} inline bool spfa()
{
int i,l,r,u,v,w,cost;
static int q[MAXN << ];
static bool inq[MAXN << ];
for (i = ; i <= * n; i++)
{
dist[i] = inf;
incf[i] = inf;
inq[i] = false;
}
q[l = r = ] = S;
inq[S] = true;
pre[S] = ;
dist[S] = ;
while (l <= r)
{
u = q[l];
l++;
inq[u] = false;
for (i = head[u]; i; i = e[i].nxt)
{
v = e[i].to;
w = e[i].w;
cost = e[i].cost;
if (w && dist[u] + cost < dist[v])
{
dist[v] = dist[u] + cost;
incf[v] = min(incf[u],w);
pre[v] = i;
if (!inq[v])
{
inq[v] = true;
q[++r] = v;
}
}
}
}
if (dist[T] != inf) return true;
else return false;
}
inline void update()
{
int pos,x = T;
while (x != S)
{
pos = pre[x];
e[pos].w -= incf[T];
e[pos ^ ].w += incf[T];
x = e[pos ^ ].to;
}
ans1 += incf[T];
ans2 += dist[T] * incf[T];
} int main()
{ read(n); read(m);
tot = ;
addedge(,n + ,inf,);
addedge(n, * n,inf,);
for (i = ; i < n; i++) addedge(i,i + n,,);
for (i = ; i <= m; i++)
{
read(a); read(b); read(c);
addedge(a + n,b,,c);
}
S = ; T = * n;
while (spfa()) update();
printf("%d %d\n",ans1,ans2); return ; }

[SDOI 2009] 晨跑的更多相关文章

  1. C++之路进阶——codevs2306(晨跑)

    2306 晨跑 2009年省队选拔赛山东  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 大师 Master       题目描述 Description Elaxia最近迷恋 ...

  2. 1877: [SDOI2009]晨跑

    1877: [SDOI2009]晨跑 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 2007  Solved: 1085[Submit][Status][ ...

  3. 晨跑(bzoj 1877)

    Description Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑.仰卧起坐等 等,不过到目前为止,他坚持下来的只有晨跑. 现在给出一张学校附近的地图,这张地图中包含N个十 ...

  4. BZOJ-1877 晨跑 最小费用最大流+拆点

    其实我是不想做这种水题的QWQ,没办法,剧情需要 1877: [SDOI2009]晨跑 Time Limit: 4 Sec Memory Limit: 64 MB Submit: 1704 Solve ...

  5. 【BZOJ】1877: [SDOI2009]晨跑(最小费用最大流)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1877 费用流做多了,此题就是一眼题. 拆点表示只能经过一次,容量为1,费用为0. 然后再连边即可,跑 ...

  6. BZOJ 1877: [SDOI2009]晨跑 费用流

    1877: [SDOI2009]晨跑 Description Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑.仰卧起坐等 等,不过到目前为止,他坚持下来的只有晨跑. 现在给出一 ...

  7. 【BZOJ】【1877】【SDOI2009】晨跑

    网络流/费用流 费用流入门题……根本就是模板题好吗! 拆点搞定度数限制,也就是每个点最多经过一次……源点汇点除外. /***************************************** ...

  8. bzoj1877: [SDOI2009]晨跑

    挺裸的最小费用最大流... #include<cstdio> #include<queue> #include<cstring> #include<iostr ...

  9. 【BZOJ 1877】 [SDOI2009]晨跑

    Description Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑.仰卧起坐等 等,不过到目前为止,他坚持下来的只有晨跑. 现在给出一张学校附近的地图,这张地图中包含N个十 ...

随机推荐

  1. Unity中带有alpha通道的视频叠加播放

    问题: 如何让两个透明视频叠加播放 解决播放: 1:使用Unity自带的shader,shader代码如下所示 Shader "Unlit/MaskVideo" { Propert ...

  2. bzoj2693 jzptab 莫比乌斯反演|题解

    Description   Input 一个正整数T表示数据组数 接下来T行 每行两个正整数 表示N.M Output T行 每行一个整数 表示第i组数据的结果 Sample Input 1 4 5 ...

  3. Extjs杂记录

    1,页面跳转到另外一个页面 这段话的意思:取得恢复密码窗口,关闭这个窗口,页面跳转到Login页面 2,keypecial 当与导航相关的键(如箭头.tab键.Enter键.ESC键等)按下时,该事件 ...

  4. Python学习-字符串函数操作1

    字符串的函数操作 capitalize():可以将字符串首字母变为大写 返回值:首字符大写后的新字符串 str = "liu" print(str.capitalize()); / ...

  5. linux初步学习有感

    经过了一段时间对linux的接触,从最开始接触到的deepin到后来我最喜欢的KaliLinux,感受到了这个我曾经并不了解的操作系统的独特魅力. 我是到了大学才知道linux这个系统的,但是在小时候 ...

  6. laravel 开发辅助工具

    laravel 开发辅助工具 配置 添加服务提供商 将下面这行添加至 config/app.php 文件 providers 数组中: 'providers' => [ ... App\Plug ...

  7. 1031. Hello World for U

    Given any string of N (>=5) characters, you are asked to form the characters into the shape of U. ...

  8. [luoguP2387] 魔法森林(LCT + 并查集)

    传送门 并查集真是一个判断连通的好东西! 连通性用并查集来搞. 把每一条边按照 a 为关键字从小到大排序. 那么直接枚举,动态维护 b 的最小生成树 用 a[i] + 1 ~ n 路径上最大的 b[i ...

  9. 印象笔记再WIN10同步失败解决方法

    IE11不支持tls1.1导致印象笔记安全性失效 打开组策略,关闭IE的加密策略就可以同步了

  10. java复习volatile关键字解析

    转自https://www.cnblogs.com/dolphin0520/p/3920373.html volatile这个关键字可能很多朋友都听说过,或许也都用过.在Java 5之前,它是一个备受 ...