【BZOJ】1877: [SDOI2009]晨跑(最小费用最大流)
http://www.lydsy.com/JudgeOnline/problem.php?id=1877
费用流做多了,此题就是一眼题。
拆点表示只能经过一次,容量为1,费用为0。
然后再连边即可,跑一次费用流
- #include <cstdio>
- #include <cstring>
- #include <cmath>
- #include <string>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- #define rep(i, n) for(int i=0; i<(n); ++i)
- #define for1(i,a,n) for(int i=(a);i<=(n);++i)
- #define for2(i,a,n) for(int i=(a);i<(n);++i)
- #define for3(i,a,n) for(int i=(a);i>=(n);--i)
- #define for4(i,a,n) for(int i=(a);i>(n);--i)
- #define CC(i,a) memset(i,a,sizeof(i))
- #define read(a) a=getint()
- #define print(a) printf("%d", a)
- #define dbg(x) cout << #x << " = " << x << endl
- #define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
- inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
- inline const int max(const int &a, const int &b) { return a>b?a:b; }
- inline const int min(const int &a, const int &b) { return a<b?a:b; }
- const int N=1010, M=2000000, oo=~0u>>1, pw=410;
- int ihead[N], cnt=1, d[N], p[N], n, m, vis[N], q[N], front, tail;
- struct ED { int from, to, cap, w, next; } e[M];
- inline void add(const int &u, const int &v, const int &c, const int &w) {
- e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v; e[cnt].from=u; e[cnt].cap=c; e[cnt].w=w;
- e[++cnt].next=ihead[v]; ihead[v]=cnt; e[cnt].to=u; e[cnt].from=v; e[cnt].cap=0; e[cnt].w=-w;
- }
- inline const bool spfa(const int &s, const int &t) {
- for1(i, 0, t) d[i]=1000000000, vis[i]=0;
- vis[s]=1; d[s]=front=tail=0; q[tail++]=s;
- int u, v, i;
- while(front!=tail) {
- u=q[front++]; if(front==N) front=0;
- for(i=ihead[u]; i; i=e[i].next) if(e[i].cap && d[v=e[i].to]>d[u]+e[i].w) {
- d[v]=d[u]+e[i].w; p[v]=i;
- if(!vis[v]) {
- vis[v]=1, q[tail++]=v;
- if(tail==N) tail=0;
- }
- }
- vis[u]=0;
- }
- return d[t]!=1000000000;
- }
- void mcf(const int &s, const int &t) {
- int ret=0, f, u, flow=0;
- while(spfa(s, t)) {
- for(f=oo, u=t; u!=s; u=e[p[u]].from) f=min(f, e[p[u]].cap);
- for(u=t; u!=s; u=e[p[u]].from) e[p[u]].cap-=f, e[p[u]^1].cap+=f;
- ret+=d[t]*f; flow+=f;
- }
- printf("%d %d\n", flow, ret);
- }
- int main() {
- read(n); read(m);
- int x, y, c, s=1, t=n;
- for1(i, 2, n-1) add(i, i+pw, 1, 0);
- add(s, s+pw, oo, 0); add(t, t+pw, oo, 0);
- rep(i, m) {
- read(x); read(y); read(c);
- add(x+pw, y, 1, c);
- }
- mcf(s, t+pw);
- return 0;
- }
Description
Input
Output
Sample Input
1 2 1
1 3 1
2 4 1
3 4 1
4 5 1
4 6 1
2 5 5
3 6 6
5 7 1
6 7 1
Sample Output
HINT
对于30%的数据,N ≤ 20,M ≤ 120。
对于100%的数据,N ≤ 200,M ≤ 20000。
Source
【BZOJ】1877: [SDOI2009]晨跑(最小费用最大流)的更多相关文章
- BZOJ 1877: [SDOI2009]晨跑( 最小费用最大流 )
裸的费用流...拆点, 流量限制为1, 最后的流量和费用即答案. ------------------------------------------------------------------- ...
- [SDOI2009]晨跑[最小费用最大流]
[SDOI2009]晨跑 最小费用最大流的板子题吧 令 \(i'=i+n\) \(i -> i'\) 建一条流量为1费用为0的边这样就不会对答案有贡献 其次是对 \(m\) 条边建 \(u'-& ...
- 【BZOJ1877】[SDOI2009]晨跑 最小费用最大流
[BZOJ1877][SDOI2009]晨跑 Description Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑.仰卧起坐等 等,不过到目前为止,他坚持下来的只有晨跑. 现 ...
- BZOJ 1877: [SDOI2009]晨跑(费用流)
看到要求两个量就下意识的想到了费用流= =,先把一个点拆成两个点就能够解决一个的只经过一次的限制 CODE: #include<cstdio>#include<iostream> ...
- BZOJ 1877: [SDOI2009]晨跑 费用流
1877: [SDOI2009]晨跑 Description Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑.仰卧起坐等 等,不过到目前为止,他坚持下来的只有晨跑. 现在给出一 ...
- BZOJ 3438 小M的作物 & BZOJ 1877 [SDOI2009]晨跑
我由衷地为我的朋友高兴.哈哈,yian,当你nick name破百上千时,再打“蒟蒻”就会被打的. 好的,说正事吧.请注意,这还是题解.但我发现,网络流实在是太套路了(怪不得这两年几乎销声匿迹).我们 ...
- BZOJ-1877 晨跑 最小费用最大流+拆点
其实我是不想做这种水题的QWQ,没办法,剧情需要 1877: [SDOI2009]晨跑 Time Limit: 4 Sec Memory Limit: 64 MB Submit: 1704 Solve ...
- bzoj 1877 [SDOI2009]晨跑(最小费用最大流)
Description Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑.仰卧起坐等 等,不过到目前为止,他坚持下来的只有晨跑. 现在给出一张学校附近的地图,这张地图中包含N个十 ...
- 1877. [SDOI2009]晨跑【费用流】
Description Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑.仰卧起坐等 等,不过到目前为止,他 坚持下来的只有晨跑. 现在给出一张学校附近的地图,这张地图中包含N个 ...
- BZOJ 1877 [SDOI2009]晨跑(多条不交叉最短路)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1877 [题目大意] 找出最多有几条点不重复的从1到N的路,并且要求在满足这个条件的情况 ...
随机推荐
- 【Spring】Spring系列2之bean的配置
2.bean的配置 2.1.IOC概述 2.2.bean的获取 2.3.依赖注入方式 2.4.属性注入细节 内部bean,不需要ID,ID无效,外部不能引用: 2.5.集合属性注入 2.6.使用p命名 ...
- 74 使用BitSet输出数组中的重复元素
[本文链接] http://www.cnblogs.com/hellogiser/p/using-bitset-to-print-duplicate-elements-of-array.html [题 ...
- 41.把数组排成最小的数[Sort array to smallest value]
[题目] 输入一个正整数数组,将它们连接起来排成一个数,输出能排出的所有数字中最小的一个.例如输入数组{3,32, 321},则输出这两个能排成的最小数字321323.请给出解决问题的算法,并证明该 ...
- 15.含有指针成员的类的拷贝[ClassCopyConstructorWithPointerMember]
[题目] 下面是一个数组类的声明与实现.请分析这个类有什么问题,并针对存在的问题提出几种解决方案. C++ Code 123456789101112131415161718192021222324 ...
- 惊魂web应用宕机记一次网站的紧急恢复
这次网站的故障出现的比较突然,没有任何防备,有种突如其来的感觉.这是一台阿里云服务器,采用wdcp的nginx+apache+mysql的方式运行.一位同事在对web目录进行压缩后,由于web目录有很 ...
- 【python】a[::-1]翻转
设有一个元组或者列表 a = (1,2,3,4) b = [1,2,3,4] 则a[::-1]和b[::-1]的含义是将元组或列表的内容翻转 a[::-1] # 结果为(4,3,2,1) b[::-1 ...
- 解决 mysql 启动报错--发现系统错误2,系统找不到指定的文件
HKEY_LOCAL_MACHINE-SYSTEM-CurrentControlSet-services-mysql(服务名)-ImagePath 更改为(自己的):"C:\Program ...
- Ajax中的XMLHttpRequest对象详解
XMLHttpRequest对象是Ajax技术的核心.在Internet Explorer 5中,XMLHttpRequest对象以ActiveX对象引入,被称之为XMLHTTP,它是一种支持异步请求 ...
- Linus的redHat在root出现错误:-bash: addgroup: command not found
在root下无法完成添加用户组操作,如下: [root@host8 ~]# addgroup hadoop-bash: addgroup: command not found 这是的,root下竟然不 ...
- SQL 函数
一.数学函数 数学函数主要用于处理数字,包括整型.浮点数等. ABS(x) 返回x的绝对值 SELECT ABS(-1) -- 返回1 CEIL(x),CEILING(x) 返回大于或等于x的最小整数 ...