【Luogu】P2469星际竞速(费用流)
费用流,类似最小路径覆盖。
从起点向i连一条容量1费用0的边,从i'向终点连一条容量1费用0的边;
从起点向i'连一条容量1费用为瞬移的边,从i向j'连一条容量1费用为边权的边。
然后跑就可以了。
#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cstring>
#include<algorithm>
#include<queue>
#define maxn 200020
using namespace std;
inline long long read(){
long long num=,f=;
char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') f=-;
ch=getchar();
}
while(isdigit(ch)){
num=num*+ch-'';
ch=getchar();
}
return num*f;
} struct Edge{
int next,from,to,val,dis;
}edge[maxn];
int head[maxn],num;
inline void addedge(int from,int to,int val,int dis){
edge[++num]=(Edge){head[from],from,to,val,dis};
head[from]=num;
}
inline void add(int from,int to,int val,int dis){
addedge(from,to,val,dis);
addedge(to,from,,-dis);
} inline int count(int i){ return i&?i+:i-; } int dis[maxn];
int pre[maxn];
bool vis[maxn];
int Start,End; int spfa(){
memset(dis,/,sizeof(dis)); dis[Start]=;
memset(pre,,sizeof(pre));
queue<int>q; q.push(Start);
while(!q.empty()){
int from=q.front(); q.pop(); vis[from]=;
//printf("%d\n",from);
for(int i=head[from];i;i=edge[i].next){
int to=edge[i].to;
if(edge[i].val<=||dis[to]<=dis[from]+edge[i].dis) continue;
dis[to]=dis[from]+edge[i].dis;
pre[to]=i;
if(vis[to]) continue;
vis[to]=; q.push(to);
}
}
if(pre[End]==) return ;
int now=End;
while(now!=Start){
int ret=pre[now];
edge[ret].val--; edge[count(ret)].val++;
now=edge[ret].from;
}
return dis[End];
} int main(){
int n=read(),m=read();
End=n*+;
for(int i=;i<=n;++i){
add(Start,i+n,,read());
add(Start,i,,);
add(i+n,End,,);
}
for(int i=;i<=m;++i){
int from=read(),to=read(),val=read();
if(from>to) swap(from,to);
add(from,to+n,,val);
}
int ans=;
while(){
int now=spfa();
if(now==) break;
ans+=now;
}
printf("%d",ans);
return ;
}
【Luogu】P2469星际竞速(费用流)的更多相关文章
- BZOJ 1927: [Sdoi2010]星际竞速 费用流
1927: [Sdoi2010]星际竞速 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...
- Luogu2469 SDOI2010 星际竞速 费用流
传送门 发现它的本质是求一个费用最小的路径覆盖 最小路径覆盖是网络流23题中的一个比较典型的模型 所以考虑相似的建边 因为每一个点要恰好经过一次,是一个有上下界的网络流,故拆点,星球\(i\)拆成\( ...
- BZOJ 1927 星际竞速(费用流)
考虑费用流,题目要求走n个点都走完且恰好一次,显然流量的限制为n. 建立源点s和汇点t,并把每个星球拆成两个点i和i',分别表示已到达该点和经过该点. 对于能力爆发,建边(s,i',1,w). 对应高 ...
- BZOJ 1927: [Sdoi2010]星际竞速(费用流)
传送门 解题思路 仿照最小路径覆盖问题,用费用流解决此题.最小路径覆盖问题是拆点连边后用\(n-\)最大匹配,这里的话也是将每个点拆点,源点向入点连流量为\(1\),费用为\(0\)的边,向出点连流量 ...
- [SDOI2010]星际竞速——费用流
类似于最短路的网络流,而且还要保证每个点经过一次,拆点就比较方便了. 连边怎么连?要保证最大流是n(每个点经过一次)还要能从直接跳转 将每个点拆点.源点向每个点的入点连一条容量为1费用为0的边.源点向 ...
- 洛谷P2469 星际竞速
上下界费用流比较无脑,提供一种更巧妙的费用流,无需上下界. #include <cstdio> #include <algorithm> #include <queue& ...
- P2469 [SDOI2010]星际竞速(费用流)
P2469 [SDOI2010]星际竞速 最小路径覆盖问题 每个星球必须恰好去一次,而每次高速航行都是从一个星球到另一个星球. 那么高速航行的起点可以保证被去过 高速航行和空间跳跃可以是互相独立的 将 ...
- bzoj 1927 [Sdoi2010]星际竞速(最小费用最大流)
1927: [Sdoi2010]星际竞速 Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 1576 Solved: 954[Submit][Statu ...
- BZOJ 1927: [Sdoi2010]星际竞速(最小费用最大流)
拆点,费用流... ----------------------------------------------------------------------------- #include< ...
随机推荐
- POJ-2195 Going Home---KM算法求最小权值匹配(存负边)
题目链接: https://vjudge.net/problem/POJ-2195 题目大意: 给定一个N*M的地图,地图上有若干个man和house,且man与house的数量一致.man每移动一格 ...
- Python-Boolean operation
一.布尔运算符 1.x and y: if x is false, then x, else y 2.x or y: if x is false, then y, else x 3.not x: if ...
- 快速排序算法思路分析和C++源代码(递归和非递归)
快速排序由于排序效率在同为O(N*logN)的几种排序方法中效率较高,因此经常被采用,再加上快速排序思想----分治法也确实实用,因此很多软件公司的笔试面试喜欢考这个. 快速排序是C.R.A.Hoar ...
- shiro学习记录(二)
1 在项目中应用shiro框架进行认证 第一步:引入shiro框架相关的jar <!-- 引入shiro框架的依赖 --> <dependency> <groupId&g ...
- vue-cli npm run build 打包问题 webpack@3.6
1, vue-router 路由 有两个模式 (mode) hash (默认模式) 使用URL来模拟一个完整的URL 但是没个URL都会带上 "#/'' 支持所有浏览器 这个模式使用 red ...
- LIS最长上升子序列模板
LIS n2解法: #include<iostream> #include<cstdio> using namespace std; int n,ans; ],f[]; int ...
- Django之视图和URL配置
1.在创建项目时,Django会自动创建URL配置,在urls.py文件中 文件的默认内容如下所示: """mysite URL Configuration The ur ...
- ES6-总结
在最近进行的项目中,已经全面使用到ES6,这里对ES6进行整理总结.用得比较多的是带*的内容,这些语法.新增类型.模块调用等从代码量上.可读性上.操作上给项目带来了不少便利. 1.语法 1.1.命 ...
- [BZOJ2331]地板(插头DP)
Description lxhgww的小名叫"小L",这是因为他总是很喜欢L型的东西.小L家的客厅是一个的矩形,现在他想用L型的地板来铺满整个客厅,客厅里有些位置有柱子,不能铺地板 ...
- KMP的正确使用法_x新疆网络赛Query on a string
Query on a string 题意,给定一个大字符串,给定一个小模式串,定义 两种不同的任务模式,分别是查询和更改: 查询对应区间内,有多少个匹配到位的数字: 修改某一位的某一个字母. 于是直觉 ...