[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[ ...
随机推荐
- 浅谈Spring(三)
一.基础Spring的标准测试 1.导入spring与junit继承的jar 2.引入注解 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfig ...
- Hibernate学习之Hibernate流程
Hibernate的核心组件 在基于MVC设计模式的JAVA WEB应用中,Hibernate可以作为模型层/数据访问层.它通过配置文件(hibernate.properties或hibernate. ...
- 在zendstudio上配置SVN
本文介绍zendstudio结合SVN的使用方法. 一.部署svn服务器 直接安装用svn软件,配置太过麻烦,用户和版本库.权限管理不太方便.推荐使用CollabNetSubversionEdge.以 ...
- php知识--递归
<?php // /* * 遍历输出文件夹中的所有内容 * @param1 string $dir,要遍历的路径 * @param2 int $level = 0,当前的级别 */ functi ...
- 【算法】计算一篇文章的单词数(C、Java语言实现)
1. C语言:一个字符一个字符的读取 (有空再贴出来) 2.Java语言:按行读取,并用正则分割成多个单词,再用MapReduce并行计算单词数 (我使用的是ieda,有些地方跟eclipse有点区别 ...
- Python数据分析扩展库
Anaconda和Python(x,y)都自带了下面的这些库. 1. NumPy 强大的ndarray和ufunc函数. import numpy as np xArray = np.ones((3, ...
- safari的调试工具
safari的调试工具默认是没有打开的设置——>偏好设置——>高级———>在菜单栏中显示开发菜单
- 在CI框架下执行存储的方法
我直接把代码摆在这里分享哈 <?php /** * * Created by JetBrains PhpStorm. * User: lsl * Date: 14-1-8 * Time: 下午2 ...
- Delphi Excel FastReport
unit Unit1; interface uses Printers,Windows, Messages, SysUtils, Variants, Classes, Graphics, Contro ...
- Codeforce 424C Magic Formulas 找规律
题目链接:http://codeforces.com/contest/424/problem/C 题意:求Q值 思路:找规律 显然能够得到一个矩阵 把这个矩阵画出来就能发现一个横向的规律和一个主对角线 ...