bzoj 1927 网络流
首先我们可以知道这道题中每个点只能经过一次,那么我们引入附加源汇source,sink,那么我们可以将每个点拆成两个点,分别表示对于图中这个节点我们的进和出,那么我们可以连接(source,i,1,0),(i+n,sink,1,0),然后对于可以直接到达的点我们可以连接(source,i+n,1,cost)这个代表我们可以从任意一个点到达这个点,对于星球之间的连边我们可以连接(x,y+n,1,cost),代表我们可以从这个星球到另一个星球,因为我们考虑每个点只经过一次,所以可以这样构图,我们并不关心路径的具体方案,只关心这个点会被进一次,出一次。
/**************************************************************
Problem: 1927
User: BLADEVIL
Language: C++
Result: Accepted
Time:2388 ms
Memory:1540 kb
****************************************************************/
//By BLADEVIL
#include <cstdio>
#include <algorithm>
#include <cstring>
#define maxn 2010
#define maxm 40010
using namespace std;
int n,m,l,source,sink,ans;
int last[maxn],pre[maxm],other[maxm],len[maxm],cost[maxm];
int d[maxn],que[maxn*],vis[maxn],father[maxn];
void connect(int a,int b,int c,int d)
{
pre[++l]=last[a];
last[a]=l;
other[l]=b;
len[l]=c;
cost[l]=d;
//if (c) printf("|%d %d %d\n",a,b,d);
}
bool spfa()
{
memset(d,0x3f,sizeof d);
int h=,t=,cur;
que[]=source; d[source]=;
while (h<t)
{
cur=que[++h];
vis[cur]=;
for (int q=last[cur];q;q=pre[q])
{
if (!len[q]) continue;
if (d[other[q]]>d[cur]+cost[q])
{
father[other[q]]=q;
d[other[q]]=d[cur]+cost[q];
if (!vis[other[q]])
{
que[++t]=other[q];
vis[other[q]]=;
}
}
}
}
return d[]!=d[sink];
}
void update()
{
int cur=sink,low=<<;
while (cur!=source)
{
low=min(low,len[father[cur]]);
cur=other[father[cur]^];
}
cur=sink;
while (cur!=source)
{
ans+=cost[father[cur]];
len[father[cur]]-=low;
len[father[cur]^]+=low;
cur=other[father[cur]^];
}
//printf("%d\n",ans);
}
int main() {
scanf("%d%d",&n,&m);
source=(n<<)+; sink=source++; l=;
for (int i=;i<=n;i++) {
int x;
scanf("%d",&x);
connect(source,i+n,,x); connect(i+n,source,,-x);
}
for (int i=;i<=n;i++) connect(source,i,,),connect(i,source,,);
for (int i=;i<=m;i++) {
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
if (x>y) swap(x,y);
connect(x,y+n,,z); connect(y+n,x,,-z);
}
for (int i=;i<=n;i++) connect(i+n,sink,,),connect(sink,i+n,,);
while (spfa()) update();
printf("%d\n",ans);
return ;
}
bzoj 1927 网络流的更多相关文章
- Bzoj 1927: [Sdoi2010]星际竞速(网络流)
1927: [Sdoi2010]星际竞速 Time Limit: 20 Sec Memory Limit: 259 MB Description 10年一度的银河系赛车大赛又要开始了.作为全银河最盛大 ...
- bzoj 1927 [Sdoi2010]星际竞速——网络流
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1927 每个点拆点保证只经过一次. 主要是如果经过了这个点,这个点应该向汇点流过去表示经过了它 ...
- BZOJ 1927: [Sdoi2010]星际竞速
1927: [Sdoi2010]星际竞速 Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 2051 Solved: 1263[Submit][Stat ...
- BZOJ 1927 星际竞速(最小费用最大流)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1927 题意:一个图,n个点.对于给出的每条边 u,v,w,表示u和v中编号小的那个到编号 ...
- BZOJ 1927: [Sdoi2010]星际竞速 费用流
1927: [Sdoi2010]星际竞速 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...
- bzoj 1927 [Sdoi2010]星际竞速(最小费用最大流)
1927: [Sdoi2010]星际竞速 Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 1576 Solved: 954[Submit][Statu ...
- BZOJ 1927 星际竞速
http://www.lydsy.com/JudgeOnline/problem.php?id=1927 思路:把一个点拆成两个点, S->i 费用0,流量1 (代表这个点可以移动到其他点所必备 ...
- BZOJ 1927: [Sdoi2010]星际竞速(最小费用最大流)
拆点,费用流... ----------------------------------------------------------------------------- #include< ...
- BZOJ 1927: [Sdoi2010]星际竞速 [上下界费用流]
1927: [Sdoi2010]星际竞速 题意:一个带权DAG,每个点恰好经过一次,每个点有曲速移动到他的代价,求最小花费 不动脑子直接上上下界费用流过了... s到点连边边权为曲速的代价,一个曲速移 ...
随机推荐
- ServiceMessage
<?php class ServiceMessage { private $errorCode = array( '1000' => "系统错误", '1001' =& ...
- Agile.Net 组件式开发平台 - 驱动开发示例
首先讲一下概念,此驱动非彼驱动.在Agle.Net中我们将组件规划成两种类型,一种是基于业务的窗体组件,一种是提供扩展功能的驱动组件. 打个比方例如一般系统中需要提供身份证读卡功能,然而市面上有很多种 ...
- 每天网络半小时(MAC数据包在哪里合并的)
ip_deliver_local函数中函数中完成合并 听过netfilter框架中也会 因为net_filter框架需要感知到第四层的信息,但是单个数据包是无法感知到这些信息的,所以需要在netfil ...
- HTML5拖拽练习
HTML5提供专门的拖拽与拖放的API,以后实现这类效果就不必乱折腾了 相关属性和事件如下: 1.DataTransfer 对象:退拽对象用来传递的媒介,使用一般为Event.dataTransfer ...
- jzoj3865[JSOI2014]士兵部署
‘ 数据范围:n,m<=10^5,传送门:https://jzoj.net/senior/#main/show/3865 感觉jzoj好高明啊,就是访问不太稳定. 首先题意中被n个点控制的区域相 ...
- P1825 [USACO11OPEN]玉米田迷宫Corn Maze
题目描述 This past fall, Farmer John took the cows to visit a corn maze. But this wasn't just any corn m ...
- wmware的vmnet0、vmnet1、vmnet8
用vmware安装虚拟机后会出现三种网卡: 1.vmnet0:桥接网卡,虚拟机相当于一台实体机,可以自用访问与被访问及上网. 在桥接模式下,VMware虚拟出来的操作系统就像是局域网中的一独立的主机, ...
- 【刷题】BZOJ 1143 [CTSC2008]祭祀river
Description 在遥远的东方,有一个神秘的民族,自称Y族.他们世代居住在水面上,奉龙王为神.每逢重大庆典, Y族都会在水面上举办盛大的祭祀活动.我们可以把Y族居住地水系看成一个由岔口和河道组成 ...
- 洛谷4578 & LOJ2520:[FJOI2018]所罗门王的宝藏——题解
https://www.luogu.org/problemnew/show/P4578 https://loj.ac/problem/2520 有点水的. 先转换成图论模型,即每个绿宝石,横坐标向纵坐 ...
- nodejs创建多层目录
1. fs.mkdir不能一次创建多层目录,必须先创建上层目录,再创建下层目录 //同步 fs.mkdirSync("./tmp/"); fs.mkdirSync("./ ...