[SDOI 2009] 晨跑
[题目链接]
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] 晨跑的更多相关文章
- C++之路进阶——codevs2306(晨跑)
2306 晨跑 2009年省队选拔赛山东 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 大师 Master 题目描述 Description Elaxia最近迷恋 ...
- 1877: [SDOI2009]晨跑
1877: [SDOI2009]晨跑 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 2007 Solved: 1085[Submit][Status][ ...
- 晨跑(bzoj 1877)
Description Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑.仰卧起坐等 等,不过到目前为止,他坚持下来的只有晨跑. 现在给出一张学校附近的地图,这张地图中包含N个十 ...
- BZOJ-1877 晨跑 最小费用最大流+拆点
其实我是不想做这种水题的QWQ,没办法,剧情需要 1877: [SDOI2009]晨跑 Time Limit: 4 Sec Memory Limit: 64 MB Submit: 1704 Solve ...
- 【BZOJ】1877: [SDOI2009]晨跑(最小费用最大流)
http://www.lydsy.com/JudgeOnline/problem.php?id=1877 费用流做多了,此题就是一眼题. 拆点表示只能经过一次,容量为1,费用为0. 然后再连边即可,跑 ...
- BZOJ 1877: [SDOI2009]晨跑 费用流
1877: [SDOI2009]晨跑 Description Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑.仰卧起坐等 等,不过到目前为止,他坚持下来的只有晨跑. 现在给出一 ...
- 【BZOJ】【1877】【SDOI2009】晨跑
网络流/费用流 费用流入门题……根本就是模板题好吗! 拆点搞定度数限制,也就是每个点最多经过一次……源点汇点除外. /***************************************** ...
- bzoj1877: [SDOI2009]晨跑
挺裸的最小费用最大流... #include<cstdio> #include<queue> #include<cstring> #include<iostr ...
- 【BZOJ 1877】 [SDOI2009]晨跑
Description Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑.仰卧起坐等 等,不过到目前为止,他坚持下来的只有晨跑. 现在给出一张学校附近的地图,这张地图中包含N个十 ...
随机推荐
- CAD与用户交互在图面上选择一个实体(com接口VB语言)
主要用到函数说明: IMxDrawUtility::GetEntity 与用户交互到在图面上选择一个实体,详细说明如下: 参数 说明 [out] IMxDrawPoint** pPickPoint 返 ...
- JavaScipt30(第四个案例)(主要知识点:数组原型链上的一些方法)
承接上文,下面是第四个案例 附上项目链接: https://github.com/wesbos/JavaScript30 const inventors = [ { first: 'Albert', ...
- 01Struts 2
Struts 2 Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交 ...
- Uploadify上传大文件
一丶参考地址 <script type="text/javascript"> var auth = "@(Request.Cookies[FormsAuthe ...
- qemu vm setup network(ssh) with buildroot
1, build buildroot with buildroot.config, that is 'make qemu_x86_64_defconfig' + some packages, sshd ...
- Storm 开箱笔记
目录 Storm 开箱 1. 什么是 Storm 2. Hello World(WordCountTopology) 3. 常用API 4. 基本概念 5. 流分组策略 6. 并行度 7. Acker ...
- Linux 查看发行版版本信息和内核版本
版本信息: cat /etc/centos-release 或 redhat-release cat /etc/issiue 内核信息:uname -r 或 uname -a
- MySQL数据库grant授权命令
MySQL数据库grant授权命令 制作人:全心全意 grant授权命令的使用 grant授权命令使用语法: grant 权限 on 数据库对象 to 用户 grant 权限 on 数据库对象 to ...
- illuminate/routing 源码分析之注册路由
我们知道,在 Laravel 世界里,外界传进来一个 Request 时,会被 Kernel 处理并返回给外界一个 Response.Kernel 在处理 Request 时,会调用 illumina ...
- 关于字符串不为空 错误:s!=null
错误:s!=null 正确:StringUtils.isNotBlank(s); public static boolean isBlank(CharSequence cs) { int strLen ...