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到点连边边权为曲速的代价,一个曲速移 ...
随机推荐
- 个人github blog环境设置
每个人都想拥有自己的网站,但是大部分比较屌丝,不想花钱租赁服务器,哈哈,屌丝有屌丝办法.github应该都听说过吧,github.io提供了此功能,而且使用github来管理自己的代码,如果你有域名, ...
- 【APS.NET Core】- 应用程序Startup类介绍
转自:https://www.cnblogs.com/stulzq/p/7845026.html Startup类配置服务和应用程序的请求管道. Startup 类 ASP.NET Core应用程序需 ...
- CentOS修改DNS、IP地址、网关
一.CentOS 修改DNS 修改对应网卡的DNS的配置文件 # vi /etc/resolv.conf 修改以下内容 nameserver 8.8.8.8 #google域名服务器 nameserv ...
- project之chrome.exe
查看chrome.exe的以来文件可以得到下面这个列面,大部分是在%systemroot%/system32下面的系统dll文件,只有两个是chromium自己生成的:base.dll, conten ...
- 如何用grep命令同时显示匹配行上下的n行 (美团面试题目)
标准unix/linux下的grep通过以下参数控制上下文 grep -C 5 foo file 显示file文件中匹配foo字串那行以及上下5行grep -B 5 foo file 显示foo及前5 ...
- Redis Cluster实现原理
一.Redis Cluster主要特性和设计 集群目标 1)高性能和线性扩展,最大可以支撑到1000个节点:Cluster架构中无Proxy层,Master与slave之间使用异步replic ...
- WPF 分页控件的实现 -用户控件
效果图:
- P1135 奇怪的电梯
题目描述 呵呵,有一天我做了一个梦,梦见了一种很奇怪的电梯.大楼的每一层楼都可以停电梯,而且第i层楼(1<=i<=N)上有一个数字Ki(0<=Ki<=N).电梯只有四个按钮:开 ...
- CentOS 文本操作命令
1.cat 用于查看纯文本文件,显示行号,加-n参数,适合内容较少的情况 2.more 用于查看纯文本文件,适合内容较多的情况 3.less 用于查看纯文本文件,可以上下翻页 4.head 用于查看纯 ...
- URAL.1033 Labyrinth (DFS)
URAL.1033 Labyrinth (DFS) 题意分析 WA了好几发,其实是个简单地DFS.意外发现这个俄国OJ,然后发现ACRUSH把这个OJ刷穿了. 代码总览 #include <io ...