[BZOJ2324][ZJOI2011][最小费用最大流]营救皮卡丘
/**************************************************************
Problem: 2324
User: gaotianyu1350
Language: C++
Result: Accepted
Time:392 ms
Memory:7620 kb
****************************************************************/ #include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <iostream>
using namespace std; #define MAXN 350
#define MAXM 300000
#define INF 0x3f3f3f3f int point[MAXN], next[MAXM], v[MAXM], flow[MAXM], cap[MAXM], w[MAXM];
int lastedge[MAXN], dis[MAXN];
bool check[MAXN];
int tot = -1; int n, m, people;
int map[MAXN][MAXN]; inline void init()
{
memset(map, 0x7f, sizeof(map));
memset(point, -1, sizeof(point));
memset(next, -1, sizeof(next));
tot = -1;
} inline void addedge(int x, int y, int theCap, int theDis)
{
tot++;
next[tot] = point[x]; point[x] = tot;
v[tot] = y; flow[tot] = 0; cap[tot] = theCap; w[tot] = theDis;
tot++;
next[tot] = point[y]; point[y] = tot;
v[tot] = x; flow[tot] = 0; cap[tot] = 0; w[tot] = - theDis;
} inline int addflow(int s, int t)
{
int now = t;
int ans = INF;
while (now != s)
{
int temp = lastedge[now];
ans = min(ans, cap[temp] - flow[temp]);
now = v[lastedge[now] ^ 1];
}
now = t;
while (now != s)
{
flow[lastedge[now]] += ans;
flow[lastedge[now] ^ 1] -= ans;
now = v[lastedge[now] ^ 1];
}
return ans;
} bool spfa(int s, int t, int &maxflow, int &mincost)
{
queue<int> q;
while (!q.empty()) q.pop();
memset(dis, 0x7f, sizeof(dis));
memset(check, 0, sizeof(check));
dis[s] = 0; check[s] = true; q.push(s);
while (!q.empty())
{
int now = q.front(); q.pop();
check[now] = false;
for (int temp = point[now]; temp != -1; temp = next[temp])
if (flow[temp] < cap[temp] && dis[now] + w[temp] < dis[v[temp]])
{
dis[v[temp]] = dis[now] + w[temp];
lastedge[v[temp]] = temp;
if (!check[v[temp]])
check[v[temp]] = true, q.push(v[temp]);
}
}
if (dis[t] > INF) return false;
int add = addflow(s, t);
maxflow += add;
mincost += add * dis[t];
return true;
} inline int solve(int s, int t)
{
int maxflow = 0, mincost = 0;
while (spfa(s, t, maxflow, mincost));
/*{
int tiaoshi = 1;
tiaoshi++;
}*/
//printf("mflow :%d\n", maxflow);
return mincost;
} inline void build(int start, int end)
{
addedge(start, 0, people, 0);
for (int i = 1; i <= n; i++)
{
addedge(i, end, 1, 0);
addedge(start, i + n, 1, 0);
//addedge(i, i + n, INF, 0);
}
for (int k = 0; k <= n; k++)
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++)
if (i != j)
{
if (map[i][k] < INF && map[k][j] < INF)
map[i][j] = min(map[i][j], map[i][k] + map[k][j]);
if (k == j && i < j && map[i][j] < INF)
addedge(i == 0 ? 0 : i + n, j, INF, map[i][j]);
}
} int main()
{
//freopen("input.txt", "r", stdin);
init();
scanf("%d%d%d", &n, &m, &people);
for (int i = 1; i <= m; i++)
{
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
if (z < map[x][y])
map[x][y] = map[y][x] = z;
}
build(2 * n + 1, 2 * n + 2);
printf("%d\n", solve(2 * n + 1, 2 * n + 2));
}
[BZOJ2324][ZJOI2011][最小费用最大流]营救皮卡丘的更多相关文章
- 【BZOJ2324】[ZJOI2011]营救皮卡丘(网络流,费用流)
[BZOJ2324][ZJOI2011]营救皮卡丘(网络流,费用流) 题面 BZOJ 洛谷 题解 如果考虑每个人走的路径,就会很麻烦. 转过来考虑每个人破坏的点集,这样子每个人可以得到一个上升的序列. ...
- 【BZOJ2324】[ZJOI2011]营救皮卡丘 有上下界费用流
[BZOJ2324][ZJOI2011]营救皮卡丘 Description 皮卡丘被火箭队用邪恶的计谋抢走了!这三个坏家伙还给小智留下了赤果果的挑衅!为了皮卡丘,也为了正义,小智和他的朋友们义不容辞的 ...
- BZOJ-2324 营救皮卡丘 最小费用可行流+拆下界+Floyd预处理
准备一周多的期末,各种爆炸,回来后状态下滑巨快...调了一晚上+80%下午 2324: [ZJOI2011]营救皮卡丘 Time Limit: 10 Sec Memory Limit: 256 MB ...
- 【bzoj2324】[ZJOI2011]营救皮卡丘 最短路-Floyd+有上下界费用流
原文地址:http://www.cnblogs.com/GXZlegend/p/6832504.html 题目描述 皮卡丘被火箭队用邪恶的计谋抢走了!这三个坏家伙还给小智留下了赤果果的挑衅!为了皮卡丘 ...
- BZOJ2324 [ZJOI2011]营救皮卡丘 【费用流】
题目 皮卡丘被火箭队用邪恶的计谋抢走了!这三个坏家伙还给小智留下了赤果果的挑衅!为了皮卡丘,也为了正义,小智和他的朋友们义不容辞的踏上了营救皮卡丘的道路. 火箭队一共有N个据点,据点之间存在M条双向道 ...
- bzoj2324 [ZJOI2011]营救皮卡丘 费用流
[ZJOI2011]营救皮卡丘 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2653 Solved: 1101[Submit][Status][D ...
- bzoj 2324 [ZJOI2011]营救皮卡丘(floyd,费用流)
2324: [ZJOI2011]营救皮卡丘 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1777 Solved: 712[Submit][Stat ...
- BZOJ 2324: [ZJOI2011]营救皮卡丘( floyd + 费用流 )
昨晚写的题...补发一下题解... 把1~N每个点拆成xi, yi 2个. 预处理i->j经过编号不超过max(i,j)的最短路(floyd) S->0(K, 0), S->xi(1 ...
- BZOJ.2324.[ZJOI2011]营救皮卡丘(费用流 Floyd)
BZOJ 洛谷 首先预处理出\(dis[i][j]\),表示从\(i\)到\(j\)的最短路.可以用\(Floyd\)处理. 注意\(i,j\)是没有大小关系限制的(\(i>j\)的\(dis[ ...
随机推荐
- Yii源码阅读笔记(二十六)
Application 类中设置路径的方法和调用ServiceLocator(服务定位器)加载运行时的组件的方法注释: /** * Handles the specified request. * 处 ...
- python面向对象(一),Day6
connfigparser模块 xml模块 shutil模块以及压缩解压 subprocess模块 面向对象(上) 类和对象 onfigParser 用于对特定的配置进行操作,当前模块的名称在 pyt ...
- Optional乱用Empty之No value present
前言 看到好多文章都是推荐采用Optinal的,而经常我遇到问题的时候就想:如果设计成optional的话就不会忽略这种NullPointException错误了.然而,optional并不是想用就随 ...
- C#移位运算(左移和右移)
C#是用<<(左移) 和 >>(右移) 运算符是用来执行移位运算. 左移 (<<) 将第一个操作数向左移动第二个操作数指定的位数,空出的位置补0. 左移相当于乘. ...
- python下划线作用初识
单下划线(例:_textchar) 以单下划线做前缀的名称指定了这个名称是"私有的".在 有些 导入import * 的场景中,下一个使用你代码的人(或者你本人)会明白这个名称仅内 ...
- Android View的重绘ViewRootImpl的setView方法
博客首页:http://www.cnblogs.com/kezhuang/p/ 本篇文章来分析一下WindowManager的后续工作,也就是ViewRootImpl的setView函数的工作 /i* ...
- 【Coursera】Third Week(1)
The Early World-Wide-Web 关于CERN 欧洲核子研究组织,除了它为世界物理学所作出的卓越贡献,它还是世界上第一个网站,第一个网络服务器,第一个浏览器的诞生地. Robert C ...
- maven第二天——重要概念与其它操作
一.在eclipse中建立工程 在day01中我们搭建了eclipse的maven环境,接下来我们开始建立maven项目 1.在eclipse中建立JAVA工程 file->new->ma ...
- Zabbix应用一:Zabbix安装
1.准备zabbix依赖环境:LNMP 依赖包安装参考http://www.osyunwei.com/archives/10057.html. cmake安装mysql: cmake . -DCMAK ...
- Nginx入门篇(一)之Nginx介绍
1.简介 Nginx ("engine x") 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 服务器. Nginx 是由 Igor Sysoe ...