[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[ ...
随机推荐
- GUI练习——列出指定目录内容
需求: 一个窗体里.在文本框输入路径后,摁回车键或者点击"转到"按钮后: 若路径合法,程序会自动在文本域里显示该路径下的文件目录:若路径非法,则弹出对话框,告之你路径非法.点击&q ...
- JQuery实战学习--在dreamweaver 8中配置Jquery自动提示
最近在学习jQuery,然后在网上找到了自动提示的方法,记之. 1,首先下载jQuery_API.mxp这个扩展文件. 2,打开DW,点击命令-->扩展管理-->文件-->安装扩展, ...
- 从零开始PHP学习 - 第三天
写这个系列文章主要是为了督促自己 每天定时 定量消化一些知识! 同时也为了让需要的人 学到点啥~! 本人技术实在不高!本文中可能会有错误!希望大家发现后能提醒一下我和大家! 偷偷说下 本教程最后的目 ...
- codeforces 589F. Gourmet and Banquet 二分+网络流
题目链接 给你n种菜, 每一种可以开始吃的时间不一样, 结束的时间也不一样. 求每种菜吃的时间都相同的最大的时间.时间的范围是0-10000. 看到这个题明显可以想到网络流, 但是时间的范围明显不允许 ...
- mysql数据库指令导入导出
mysql数据库导出使用 /mysql/bin/mysqldump指令 数据库导出 导出指令: [mysql安装目录]/bin/mysqldump -u[username] -p[password] ...
- 转:angular的decorator方法
AngularJS实例 – 装饰$log 在AngularJS中,我们可以使用Angular内置或者自定义的services,在应用的各个部分之间分享数据和方法.假设你已经定义了一个service,但 ...
- Html 小插件6 百度新闻插件
新闻免费代码"http://news.baidu.com/newscode.html ,便可在输入希望订阅的关键词后,根据相关选项的设置,百度便非常快的在当前页面的文本编辑框内生成相关代码. ...
- Thread和ExecutorService(一)
ExecutorService是Java并发包里面的一个线程池服务 线程池的优点 复用已有的线程,消除任务中创建线程的开销 通过优化线程的数量,提高性能,保证处理器一直处于忙的状态,而不必创建这么 ...
- linux下安装python3.3.4
下载安装包 # wget http://www.python.org/ftp/python/3.3.4/Python-3.3.4.tgz 解压 # tar -xzvf Python-3.3.4.tgz ...
- 飘逸的python - 用urlparse从url中抽离出想要的信息
最近有个需求,要检测配置中的那些url的域名是否都正常,即是否都能ping通. 不过配置中url格式是这样的 http://www.something.com:1234/ . 要ping的是www.s ...