"Shortest" pair of paths[题解]
"Shortest" pair of paths
题目大意
给出 \(n\) 个点,\(m\) 条边,除第一个点和最后一个点外,其他所有的点都只能被经过一次,要求找到两条从第一个点到最后一个点的路径,使其长度和最小。
分析
拿到这道题,通过观察数据范围和问题模式,其实很容易想到这是一道可以通过网络流来解决的问题,网络流确实非常擅长通过连边的流量限制来表示这种对经过次数的限制。
本题就是一个非常明显的最小费用流,其建图套路也比较容易想到:
拆点,将每个点拆成入点和出点,除了第一个点与最后一个点,点 \(i\) 与 \(i'\) 间建立一条流量为 \(1\) 、 费用为 \(0\) 的边,而点 \(1\) 与点 \(n\) 则需要建立一条流量为 \(2\) 的边,费用为 \(0\) 。
建立一个超级源点和一个超级汇点,超级源点向点 \(1\) 连接一条流量为 \(+\infty\) 、费用为 \(0\) 的边,同理,点 \(n\) 向超级汇点建立一条流量为 \(+\infty\) 、费用为 \(0\) 的边。
对于给出的每一条边,设由 \(i\) 通往 \(j\) ,花费为 \(val\) ,则由 \(i'\) 向 \(j\) 连接一条流量为 \(1\) ,费用为 \(val\) 的边。
思考这样做的正确性,拆点显然维护了对于每个点只能被经过一次的性质,不难发现这样找出来的两条路径除了点 \(1\) 与点 \(n\) 外经过的其他点都一定不会重复,与题意相符,这样建出来的图配上最小费用流的模板是能够通过这道题的。
CODE
#include <bits/stdc++.h>
using namespace std;
const int N=2e3+10,M=2e4+10,INF=0x3f3f3f3f;
int T,n,m,s,_s,t,maxf,maxc;
inline int read()
{
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
int tot=-1,v[2*M+4*N],w[2*M+4*N],p[2*M+4*N],nex[2*M+4*N],first[2*M+4*N];
inline void Add(int x,int y,int z,int c)
{
nex[++tot]=first[x];
first[x]=tot;
v[tot]=y,w[tot]=z,p[tot]=c;
}
bool vis[2*N];
int pre[2*N],dis[2*N],Min[2*N];
inline bool SPFA()
{
memset(dis,0x3f,sizeof(dis));
memset(vis,false,sizeof(vis));
queue<int> q;
q.push(s);
vis[s]=true,dis[s]=0,Min[s]=INF;
while(!q.empty()){
int now=q.front(); q.pop();
vis[now]=false;
for(register int i=first[now];i!=-1;i=nex[i]){
int to=v[i];
if(!w[i]) continue;
if(dis[to]>dis[now]+p[i]){
dis[to]=dis[now]+p[i];
Min[to]=min(Min[now],w[i]);
pre[to]=i;
if(!vis[to]) q.push(to),vis[to]=true;
}
}
}
return dis[t]!=INF;
}
inline void EK()
{
while(SPFA()){
maxf+=Min[t];
maxc+=dis[t]*Min[t];
int temp=t,i;
while(temp!=s){
i=pre[temp];
w[i]-=Min[t];
w[i^1]+=Min[t];
temp=v[i^1];
}
}
}
int main()
{
while(1){
T++;
memset(first,-1,sizeof(first));
tot=1;maxf=maxc=0;
n=read(),m=read();
if(!n&&!m) break;
s=0,t=2*n+1;
Add(s,1,INF,0),Add(1,s,0,0);
Add(1,1+n,2,0),Add(1+n,1,0,0);
Add(n,2*n,2,0),Add(2*n,n,0,0);
Add(2*n,t,INF,0),Add(t,2*n,0,0);
for(register int i=1;i<=m;i++){
int x=read(),y=read(),z=read();
x++,y++;
Add(x+n,y,1,z),Add(y,x+n,0,-z);
}
for(register int i=2;i<n;i++) Add(i,i+n,1,0),Add(i+n,i,0,0);
EK();
if(maxf!=2) printf("Instance #%d: Not possible\n",T);
else printf("Instance #%d: %d\n",T,maxc);
}
return 0;
}
"Shortest" pair of paths[题解]的更多相关文章
- POJ3068:"Shortest" pair of paths——题解
http://poj.org/problem?id=3068 题目大意: 从0-n-1找到两条边和点都不相同(除了0和n-1外)的最小费用路径. ——————————————————————————— ...
- 2018.06.27"Shortest" pair of paths(费用流)
"Shortest" pair of paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1589 A ...
- POJ3068 "Shortest" pair of paths 【费用流】
POJ3068 "Shortest" pair of paths Description A chemical company has an unusual shortest pa ...
- poj 3068 "Shortest" pair of paths
"Shortest" pair of paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1407 ...
- UVALive - 2927 "Shortest" pair of paths(最小费用最大流)题解
题意:有n个机器,机器之间有m条连线,我们需要判断机器0到n-1是否存在两条线路,存在输出最小费用. 思路:我们把0连接超级源点,n-1连接超级汇点,两者流量都设为2,其他流量设为1,那么只要最后我们 ...
- POJ 3068 "Shortest" pair of paths(费用流)
[题目链接] http://poj.org/problem?id=3068 [题目大意] 给出一张图,要把两个物品从起点运到终点,他们不能运同一条路过 每条路都有一定的费用,求最小费用 [题解] 题目 ...
- POJ3068 "Shortest" pair of paths
嘟嘟嘟 题目大意:一个有向图,每一条边有一个边权,求从节点\(0\)到\(n - 1\)的两条不经过同一条边的路径,并且边权和最小. 费用流板子题. 发个博客证明一下我写了这题. #include&l ...
- [poj] 3068 "Shortest" pair of paths || 最小费用最大流
[原题](http://poj.org/problem?id=3068) 给一个有向带权图,求两条从0-N-1的路径,使它们没有公共点且边权和最小 . //是不是像传纸条啊- 是否可行只要判断最后最大 ...
- UVALIVE 2927 "Shortest" pair of paths
裸的费用流.一开始因为这句话还觉得要拆点 样例行不通不知道这句话干啥用的.Further, the company cannot place the two chemicals in same dep ...
随机推荐
- GO语言常用标准库04---flag读取命令行参数
package main import ( "flag" "fmt" "math" "os" ) /* go build ...
- 巧用Reflections库实现包扫描
1.需求 需要扫描某个包中的某个接口的实现类的需求 2.maven 依赖引入 <dependency> <groupId>org.reflections</groupId ...
- AICompiler动态shape编译框架
AICompiler动态shape编译框架 移动互联网的兴起,不仅产生了海量数据,也对人机交互有了新的定义.企业如何动态处理不同规格图片数据,如何更灵活处理不同长度的对话语料等等,提升企业运营效率,争 ...
- DLPack构建跨框架的深度学习编译器
DLPack构建跨框架的深度学习编译器 Tensorflow,PyTorch和ApacheMxNet等深度学习框架提供了一个功能强大的工具包,可用于快速进行原型设计和部署深度学习模型.易用性通常是以碎 ...
- iOS视频硬编码技术
iOS视频硬编码技术 一.iOS视频采集硬编码 基本原理 硬编码 & 软编码 硬编码:通过系统自带的Camera录制视频,实际上调用的是底层的高清编码硬件模块,即显卡,不使用CPU,速度快 软 ...
- MindSpore静态图语法支持
MindSpore静态图语法支持 概述 在Graph模式下,Python代码并不是由Python解释器去执行,而是将代码编译成静态计算图,然后执行静态计算图. 关于Graph模式和计算图,可参考文档: ...
- 在NVIDIA-Jetson平台上构建智能多媒体服务器
在NVIDIA-Jetson平台上构建智能多媒体服务器 Building a Multi-Camera Media Server for AI Processing on the NVIDIA Jet ...
- 腾讯云 K8S 集群实战 Service Mesh—Linkerd2 & Traefik2 部署 emojivoto 应用
Linkerd 是 Kubernetes 的服务网格. 它通过为您提供运行时调试(runtime debugging).可观察性(observability).可靠性(reliability)和安全性 ...
- Mac设置charles证书信任
- SpringBoot——配置类实现WebMvcConfigurer接口来配置拦截器、view-controller、视图解析器等
目的:为了保留SpringBoot对SpringMVC自动配置,另外我们还想要做一些自己拓展的功能 如何做扩展? 以配置view-controller实现跳转为例: 原先在SpringMvc中我们写v ...