[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[ ...
随机推荐
- TinyFox 部署在CentOS7 中测试使用
一:TinyFox介绍 TinyFox 是一款支持OWIN标准的WEB应用的高性能的HTTP服务器,是Jexus Web Server的"姊妹篇".TinyFox本身的功能是htm ...
- AOP 切面编程
简介 如果你很熟悉面向方面编程(AOP),你就会知道给代码增加“切面”可以使代码更清晰并且具有可维护性.但是AOP通常都依赖于第三方类库或者硬编码的.net特性来工作.虽然这些实现方式的好处大于它们的 ...
- Xcode 那些简单实用的插件推荐
古人云“工欲善其事必先利其器”,打造一个强大的开发环境,是立即提升自身战斗力的绝佳途径! 晾一下我的武器库,欢迎大家选用:) 全能搜索家CodePilot 2.0 ------------ ...
- IOS 特定于设备的开发:获取和使用设备姿势(通过手机方向控制3d物体显示)
利用设备的机载陀螺仪可以实现,当你旋转手机屏幕时,里面的画面不会随着视图更新而移动,以平衡物理运动. 下面例子利用少量简单的几何变换执行该操作.他建立一个运动管理器,订阅设备运动更新,然后基于运动管理 ...
- TCP的阻塞和重传
TCP的阻塞和重传 TCP的阻塞和重传机制 网络拥堵 现在网络上大部分的网络请求都是以TCP的方式进行传输的了.网络链路是固定的,各种链路情况也是不一样的.网络拥堵一直是TCP协议设计和使用的时候尽力 ...
- 个人信用卡管理 - iOS UI原型
- Codeforces 427D Match & Catch(后缀自动机)
[题目链接] http://codeforces.com/problemset/problem/427/D [题目大意] 给出一个两个字符串,求出最短且在两个字符串中唯一的公共子串. [题解] 以原字 ...
- gcc 的include path和lib path调整
`gcc -print-prog-name=cc1plus` -v `g++ -print-prog-name=cc1plus` -v ------------------------------ ...
- geometry(简单数学题)
geometry Accepts: 324 Submissions: 622 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 6553 ...
- 龟兔赛跑(DP)
龟兔赛跑 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...