[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[ ...
随机推荐
- HTML中小问题
1.a标签不添加href这个属性时,不会出现鼠标变成手型~
- DLL注入_拦截技术之Hook方式
后卫大师教你进程注入 首先提一下,由于文章完全是我手写,所以打不了太多,请包含,由于我已经提供了源代码,所以我在这里详细讲一下理论,至于想看代码的下载代码就可以了.代码中关于注入的部分做了详细的注释. ...
- codeforces 245H . Queries for Number of Palindromes 区间dp
题目链接 给一个字符串, q个询问, 每次询问求出[l, r]里有多少个回文串. 区间dp, dp[l][r]表示[l, r]内有多少个回文串. dp[l][r] = dp[l+1][r]+dp[l] ...
- poj 2584 T-Shirt Gumbo 网络流
题目链接 有5种T-shirt, n个人, 每个人可以接受某些种T-shirt, 每种T-shirt的数量已知, 问每个人能否都穿上自己能接受的T-shirt. 源点向每种T-shirt连边, 权值为 ...
- [C语言练习]学生学籍管理系统
/** * @copyright 2012 Chunhui Wang * * wangchunhui@wangchunhui.cn * * 学生学籍管理系统(12.06) */ #include &l ...
- ubuntu中查找软件的安装位置
ubuntu中的软件可通过图形界面的软件中心安装,也可以通过命令行apt-get install安装.但是安装后的软件在哪个位置呢?这点跟windows环境下安装软件的路径选择不一样.ubuntu中可 ...
- 走进Groovy (一)
一直很喜欢脚本语言,但是一直在不大的公司工作,用得一直是“高大上”的JAVA语言,在真正的项目中,没什么机会用到脚本语言.这两年,又断断续续的用了2年的Ruby,再回头继续用JAVA,说实话,真感觉J ...
- td之overflow:hidden 多余文本隐藏效果
td之overflow:hidden 多余文本隐藏效果 方法1: table-layout: fixed; width: 200px; 语法: table-layout : auto | fixed ...
- android小知识之意图(intent)
android中的意图有显示意图和隐式意图两种, 显示意图要求必须知道被激活组件的包和class 隐式意图只需要知道跳转activity的动作和数据,就可以激活对应的组件 A 主activity B ...
- 一步一步学习SignalR进行实时通信_3_通过CORS解决跨域
原文:一步一步学习SignalR进行实时通信_3_通过CORS解决跨域 一步一步学习SignalR进行实时通信\_3_通过CORS解决跨域 SignalR 一步一步学习SignalR进行实时通信_3_ ...