[CQOI2015] 网络吞吐量 - 最大流,最短路
在第i个点只能选A[i]次的情况下,能选出多少条1-n的最短路
Solution
我们造出最短路DAG,然后对每个点拆点限流,跑最大流即可
双向边警告!(有悖直觉
#include <bits/stdc++.h>
using namespace std;
#define int long long
namespace sp {
// Interfaces: n, g[][], spt[], d[]
const int N = 200005;
int n;
struct edge {int u,v,w;};
vector<pair<int, int> >g[N];
vector<edge> spt;
int d[N], v[N];
void make(int t1, int t2, int t3) {
g[t1].push_back(make_pair(t2, t3));
}
void reset_graph() {
for (int i = 0; i <= n; i++)
g[i].clear();
}
void reset_solver() {
memset(d, 0x3f, sizeof d);
memset(v, 0x00, sizeof v);
}
void solve(int v0) {
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >q;
reset_solver();
d[v0] = 0;
q.push(make_pair(0,v0));
while (q.size()) {
pair<int, int> p = q.top();
int dis = p.first;
int pos = p.second;
q.pop();
v[pos] = 1;
for (int i = 0; i < g[pos].size(); i++) {
int x = g[pos][i].first;
int y = g[pos][i].second;
if (d[x] > d[pos] + y) {
d[x] = d[pos] + y;
if (!v[x]) q.push(make_pair(d[x], x));
}
}
}
for(int i=1;i<=n;i++) {
int p=i;
for(int j=0;j<g[p].size();j++) {
int q=g[p][j].first, w=g[p][j].second;
if(d[q]==d[p]+w) spt.push_back((edge){p,q,w});
}
}
}
}
namespace flow {
const int maxn = 200005;
const int inf = 1e+14;
int dis[maxn], ans, cnt = 1, s, t, pre[maxn * 10], nxt[maxn * 10], h[maxn], v[maxn * 10];
std::queue<int> q;
void make(int x, int y, int z) {
pre[++cnt] = y, nxt[cnt] = h[x], h[x] = cnt, v[cnt] = z;
pre[++cnt] = x, nxt[cnt] = h[y], h[y] = cnt;
}
bool bfs() {
memset(dis, 0, sizeof dis);
q.push(s), dis[s] = 1;
while (!q.empty()) {
int x = q.front();
q.pop();
for (int i = h[x]; i; i = nxt[i])
if (!dis[pre[i]] && v[i])
dis[pre[i]] = dis[x] + 1, q.push(pre[i]);
}
return dis[t];
}
int dfs(int x, int flow) {
if (x == t || !flow)
return flow;
int f = flow;
for (int i = h[x]; i; i = nxt[i])
if (v[i] && dis[pre[i]] > dis[x]) {
int y = dfs(pre[i], min(v[i], f));
f -= y, v[i] -= y, v[i ^ 1] += y;
if (!f)
return flow;
}
if (f == flow)
dis[x] = -1;
return flow - f;
}
int solve(int _s,int _t) {
s=_s;
t=_t;
ans = 0;
for (; bfs(); ans += dfs(s, inf));
return ans;
}
}
const int N = 5005;
int n,m,t1,t2,t3,lim[N];
signed main() {
scanf("%lld%lld",&n,&m);
sp::n=n;
for(int i=1;i<=m;i++) {
scanf("%lld%lld%lld",&t1,&t2,&t3);
sp::make(t1,t2,t3);
sp::make(t2,t1,t3);
}
sp::solve(1);
//for(int i=1;i<=n;i++) cout<<sp::d[i]<<" ";
//cout<<endl;
for(int i=0;i<sp::spt.size();i++) {
int u=sp::spt[i].u, v=sp::spt[i].v, w=sp::spt[i].w;
flow::make(2*u,2*v-1,1e+14);
}
for(int i=1;i<=n;i++) {
scanf("%lld",&lim[i]);
flow::make(2*i-1,2*i,lim[i]);
}
cout<<flow::solve(2,2*n-1);
}
我的模板丑的不行了,我得去改造模板了
[CQOI2015] 网络吞吐量 - 最大流,最短路的更多相关文章
- 【BZOJ3931】[CQOI2015]网络吞吐量 最大流
[BZOJ3931][CQOI2015]网络吞吐量 Description 路由是指通过计算机网络把信息从源地址传输到目的地址的活动,也是计算机网络设计中的重点和难点.网络中实现路由转发的硬件设备称为 ...
- BZOJ 3931: [CQOI2015]网络吞吐量 最大流
3931: [CQOI2015]网络吞吐量 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/p ...
- BZOJ 3931 / Luogu P3171 [CQOI2015]网络吞吐量 (最大流板题)
题面 中文题目,不解释: BZOJ传送门 Luogu传送门 分析 这题建图是显然的,拆点后iii和i′i'i′连容量为吞吐量的边,根据题目要求,111和nnn的吞吐量看作∞\infty∞. 然后用di ...
- BZOJ 3931: [CQOI2015]网络吞吐量( 最短路 + 最大流 )
最短路 + 最大流 , 没什么好说的... 因为long long WA 了两次.... ------------------------------------------------------- ...
- bzoj 3931: [CQOI2015]网络吞吐量 -- 最短路+网络流
3931: [CQOI2015]网络吞吐量 Time Limit: 10 Sec Memory Limit: 512 MB Description 路由是指通过计算机网络把信息从源地址传输到目的地址 ...
- BZOJ 3931: [CQOI2015]网络吞吐量
3931: [CQOI2015]网络吞吐量 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1555 Solved: 637[Submit][Stat ...
- 3931: [CQOI2015]网络吞吐量
3931: [CQOI2015]网络吞吐量 链接 分析: 跑一遍dijkstra,加入可以存在于最短路中的点,拆点最大流. 代码: #include<cstdio> #include< ...
- bzoj千题计划136:bzoj3931: [CQOI2015]网络吞吐量
http://www.lydsy.com/JudgeOnline/problem.php?id=3931 在最短路网络上跑最大流 #include<queue> #include<c ...
- bzoj3931: [CQOI2015]网络吞吐量(spfa+网络流)
3931: [CQOI2015]网络吞吐量 题目:传送门 题解: 现在有点难受....跳了一个多钟...菜啊... 题意都把做法一起给了....最短路+网路流啊. 不想说话...记得开long lon ...
随机推荐
- 【爬虫】 爬虫请求json数据,返回乱码问题的解决
from django.http import JsonResponse from rest_framework.utils import json from utils import request ...
- StackExchange.Redis 之 String 类型示例
String类型很简单,就不做示例演示了,这里只贴出Helper类 /// <summary> /// 判断key是否存在 /// </summary> /// <par ...
- ELK学习003:Elasticsearch启动常见问题
一.Caused by: java.lang.RuntimeException: can not run elasticsearch as root 这个错误,是因为使用root用户启动elastic ...
- Django3的安装以及web项目的创建
cmd 直接输入:pip install -i https://pypi.douban.com/simple django 2.检测是否安装成功:用到的命令:import django ,检测版本 ...
- Python和Anoconda和Pycharm联合使用教程
简介 Python是一种跨平台的计算机程序设计语言.是一种面向对象的动态类型语言,最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越多被用于独立的.大型项目的开发. ...
- 【01】HTML_day01_01-前言&WEB标准
typora-copy-images-to: media 第01阶段.前端基础.认识WEB 基础班学习目标 目标: 能根据psd文件,用HTML+CSS 布局出符合W3C规范的网页. 网站首页 列表页 ...
- webpack打包进行丑化压缩遇到(TypeError Cannot read property 'compilation' of undefined)问题
今天再重新配置老项目node打包环境的时候遇到了一个问题. 在打包的时候报: TypeError: Cannot read property 'compilation' of undefined 错误 ...
- Ant Design 方法默认传值,加上其他参数
前端填坑之路Ant Design里面的一些触发方法,如OnChange,OnSelect等等,当你触发时,该时间会自动传一些值给方法. 这是Select里面的onChange调用,在红框中,他会自动传 ...
- Leetcode 与树(TreeNode )相关的题解测试工具函数总结
最近在剑指Offer上刷了一些题目,发现涉及到数据结构类的题目,如果想在本地IDE进行测试,除了完成题目要求的算法外,还需要写一些辅助函数,比如树的创建,遍历等,由于这些函数平时用到的地方比较多,并且 ...
- Git分支的管理
(一)查看分支 1.查看本地分支:git branch 2.查看远程分支:git branch -r 3.查看所有分支:git branch -a (二)创建分支 1.创建本地分支:git branc ...