题目

传送门

题解

第一次一遍就AC一道bzoj上的题,虽然是一道水题。。。

我们做一边最短路,求出每个点的dist,然后再做一次类似spfa的操作,求出每个点是否可以用于建图。

在新图上拆点跑一边dinic就好辣。

代码

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
#define ll long long
const ll inf = 100000000000;
const int maxn = 1000;
struct edge {
int from;
int to;
ll value;
};
vector<edge> edges;
vector<int> G[maxn];
vector<edge> gg[maxn];
void add(int from, int to, ll value) {
gg[from].push_back((edge){from, to, value});
}
void add_edge(int from, int to, ll value) {
edges.push_back((edge){from, to, value});
edges.push_back((edge){to, from, 0});
int m = edges.size();
G[from].push_back(m - 2);
G[to].push_back(m - 1);
}
ll dist[maxn];
int iter[maxn];
int n, m;
ll c[maxn];
int st, t;
void spfa(int s) {
for (int i = 1; i <= n; i++)
dist[i] = inf;
queue<int> q;
bool inq[maxn];
memset(inq, 0, sizeof(inq));
q.push(s);
dist[s] = 0;
inq[s] = 1;
while (!q.empty()) {
int u = q.front();
q.pop();
inq[u] = 0;
for (int i = 0; i < gg[u].size(); i++) {
edge &e = gg[u][i];
if (dist[e.to] > dist[u] + e.value) {
dist[e.to] = dist[u] + e.value;
if (!inq[e.to]) {
q.push(e.to);
inq[e.to] = 1;
}
}
}
}
}
void build_graph(int s, int e) {
queue<int> q;
q.push(s);
bool inq[maxn];
memset(inq, 0, sizeof(inq));
inq[s] = 1;
int vis[maxn];
memset(vis, 0, sizeof(vis));
vis[s] = 1;
add_edge(st, s, inf);
add_edge(e + n, t, inf);
add_edge(s, s + n, inf);
add_edge(e, e + n, inf);
while (!q.empty()) {
int u = q.front();
q.pop();
inq[u] = 0;
for (int i = 0; i < gg[u].size(); i++) {
edge &ee = gg[u][i];
if (dist[ee.to] == dist[u] + ee.value) { add_edge(u + n, ee.to, inf);
if (!vis[ee.to] && ee.to != e) {
add_edge(ee.to, ee.to + n, c[ee.to]);
vis[ee.to] = 1;
}
if (!inq[ee.to]) {
q.push(ee.to);
inq[ee.to] = 1;
}
}
}
}
}
void bfs(int s) {
memset(dist, -1, sizeof(dist));
dist[s] = 0;
queue<int> q;
q.push(s);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = 0; i < G[u].size(); i++) {
edge &e = edges[G[u][i]];
if (e.value > 0 && dist[e.to] == -1) {
dist[e.to] = dist[u] + 1;
q.push(e.to);
}
}
}
}
ll dfs(int s, int t, ll flow) {
if (s == t)
return flow;
for (int &i = iter[s]; i < G[s].size(); i++) {
edge &e = edges[G[s][i]];
if (dist[e.to] > dist[s] && e.value > 0) {
int d = dfs(e.to, t, min(flow, e.value));
if (d > 0) {
e.value -= d;
edges[G[s][i] ^ 1].value += d;
return d;
}
}
}
return 0;
}
ll dinic(int s, int t) {
ll flow = 0;
while (1) {
bfs(s);
if (dist[t] == -1)
return flow;
memset(iter, 0, sizeof(iter));
ll d;
while (d = dfs(s, t, inf))
flow += d;
}
}
int main() {
// freopen("input", "r", stdin);
scanf("%d %d", &n, &m);
for (int i = 1; i <= m; i++) {
int x, y;
ll z;
scanf("%d %d %lld", &x, &y, &z);
add(x, y, z);
add(y, x, z);
}
for (int i = 1; i <= n; i++)
scanf("%lld", &c[i]);
spfa(1);
st = 0, t = n + n + 1;
build_graph(1, n); ll ans = dinic(st, t);
printf("%lld\n", ans);
}

Excited!

[bzoj3931][CQOI2015]网络吞吐量——最短路+网络流的更多相关文章

  1. bzoj 3931: [CQOI2015]网络吞吐量 -- 最短路+网络流

    3931: [CQOI2015]网络吞吐量 Time Limit: 10 Sec  Memory Limit: 512 MB Description 路由是指通过计算机网络把信息从源地址传输到目的地址 ...

  2. bzoj3931: [CQOI2015]网络吞吐量(spfa+网络流)

    3931: [CQOI2015]网络吞吐量 题目:传送门 题解: 现在有点难受....跳了一个多钟...菜啊... 题意都把做法一起给了....最短路+网路流啊. 不想说话...记得开long lon ...

  3. 【BZOJ-3931】网络吞吐量 最短路 + 最大流

    3931: [CQOI2015]网络吞吐量 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1228  Solved: 524[Submit][Stat ...

  4. bzoj3931: [CQOI2015]网络吞吐量

    将最短路图找出来,跑maxflow即可.有注意到数据范围.然后输出的时候%dWA了三次QAQ... #include<cstdio> #include<cstring> #in ...

  5. BZOJ 3931: [CQOI2015]网络吞吐量( 最短路 + 最大流 )

    最短路 + 最大流 , 没什么好说的... 因为long long WA 了两次.... ------------------------------------------------------- ...

  6. bzoj千题计划136:bzoj3931: [CQOI2015]网络吞吐量

    http://www.lydsy.com/JudgeOnline/problem.php?id=3931 在最短路网络上跑最大流 #include<queue> #include<c ...

  7. 【bzoj3931】[CQOI2015]网络吞吐量 最短路+最大流

    题目描述 路由是指通过计算机网络把信息从源地址传输到目的地址的活动,也是计算机网络设计中的重点和难点.网络中实现路由转发的硬件设备称为路由器.为了使数据包最快的到达目的地,路由器需要选择最优的路径转发 ...

  8. 【最短路】【最大流】bzoj3931 [CQOI2015]网络吞吐量

    跑出最短路图,然后把结点拆点跑最大流. #include<cstdio> #include<queue> #include<cstring> #include< ...

  9. BZOJ3931 [CQOI2015]网络吞吐量(最大流)

    没啥好说的,有写过类似的,就是预处理出最短路上的边建容量网络. #include<cstdio> #include<cstring> #include<queue> ...

随机推荐

  1. Android 上能提高学习工作效率的应用

    在知乎上有朋友问 Android 上能提高学习.工作效率的应用有哪些?我给他们的推荐获得了最多赞同.以后会不断完善更新此贴. Any.do :规划日程,各平台都有. Evernote:记笔记,各平台都 ...

  2. JQuery JTable根据某行的某个值来设置行的背景颜色

    目录 描述 处理方法 参考 描述 某个表的数据是用JQuery的JTable插件进行展示的.现在需求是:当表中的master字段为true时,就将对应的整行的背景颜色设置为浅蓝色. 处理方法 在fie ...

  3. fork开源代码后如何基于某个tag建立自己的branch

    应用场景: 在github上fork一个自己想看的开源项目,想基于某个tag来写一些测试demo,然后可以做到版本控制. 方法: //克隆 git clone xxxxx.git //查看tag gi ...

  4. SpringBoot 入门学习(HelloWord)

    前置知识 1.会利用 maven 构建项目 2.了解 Spring 注解 3.了解 RESTful API 的基本理论 4.SpringBoot 是 SpringMVC 的升级版,但两者没有必然的联系 ...

  5. 自定义 Relam

    package org.zln.hello.realm; import org.apache.shiro.authc.*; import org.apache.shiro.realm.Realm; / ...

  6. 【bzoj1458】士兵占领 有上下界最小流

    题目描述 有一个M * N的棋盘,有的格子是障碍.现在你要选择一些格子来放置一些士兵,一个格子里最多可以放置一个士兵,障碍格里不能放置士兵.我们称这些士兵占领了整个棋盘当满足第i行至少放置了Li个士兵 ...

  7. 完全解析线程池ThreadPool原理&使用

    目录 1. 简介 2. 工作原理 2.1 核心参数 线程池中有6个核心参数,具体如下 上述6个参数的配置 决定了 线程池的功能,具体设置时机 = 创建 线程池类对象时 传入 ThreadPoolExe ...

  8. GDI绘图中的映射模式CDC::SetMapMode()

    原文链接:http://blog.csdn.net/charlessimonyi/article/details/8264572 在GDI绘图前,一般要设置映射模式.映射模式是什么呢?它是逻辑长度单位 ...

  9. [Leetcode] Linked list cycle ii 判断链表是否有环

    Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follo ...

  10. BZOJ1857 [Scoi2010]传送带 【三分法】

    题目链接 BZOJ1857 题解 画画图就发现实际上是在\(AB\)上和\(CD\)上分别选两个点\(E\),\(F\),使得\(t_{AE} + t_{EF} + t_{FD}\)最小 然后猜想到当 ...