[ABC232G] Modulo Shortest Path
Problem Statement
We have a directed graph with $N$ vertices, called Vertex $1$, Vertex $2$, $\ldots$, Vertex $N$.
For each pair of integers such that $1 \leq i, j \leq N$ and $i \neq j$, there is a directed edge from Vertex $i$ to Vertex $j$ of weight $(A_i + B_j) \bmod M$. (Here, $x \bmod y$ denotes the remainder when $x$ is divided by $y$.)
There is no edge other than the above.
Print the shortest distance from Vertex $1$ to Vertex $N$, that is, the minimum possible total weight of the edges in a path from Vertex $1$ to Vertex $N$.
Constraints
- $2 \leq N \leq 2 \times 10^5$
- $2 \leq M \leq 10^9$
- $0 \leq A_i, B_j < M$
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
$N$ $M$
$A_1$ $A_2$ $\ldots$ $A_N$
$B_1$ $B_2$ $\ldots$ $B_N$
Output
Print the minimum possible total weight of the edges in a path from Vertex $1$ to Vertex $N$.
Sample Input 1
4 12
10 11 6 0
8 7 4 1
Sample Output 1
3
Below, $i \rightarrow j$ denotes the directed edge from Vertex $i$ to Vertex $j$.
Let us consider the path $1$ $\rightarrow$ $3$ $\rightarrow$ $2$ $\rightarrow$ $4$.
- Edge $1\rightarrow 3$ weighs $(A_1 + B_3) \bmod M = (10 + 4) \bmod 12 = 2$,
- Edge $3 \rightarrow 2$ weighs $(A_3 + B_2) \bmod M = (6 + 7) \bmod 12 = 1$,
- Edge $2\rightarrow 4$ weighs $(A_2 + B_4) \bmod M = (11 + 1) \bmod 12 = 0$.
Thus, the total weight of the edges in this path is $2 + 1 + 0 = 3$.
This is the minimum possible sum for a path from Vertex $1$ to Vertex $N$.
Sample Input 2
10 1000
785 934 671 520 794 168 586 667 411 332
363 763 40 425 524 311 139 875 548 198
Sample Output 2
462
一道优化建图题.
观察到这里的取模最多只会减一次,所以与其说是取模,不如说当 \(a_i+b_j\ge m\) 时,代价减去 \(m\)。
先看如果没有取模,怎么做。一个经典的拆点,把一个点拆成内点和外点,外点向内点连代价为 \(b_i\) 的边,内点向外点连 \(a_i\) 的边。然后随便用0边把外点连起来。
但这样很明显不好扩展。注意到一件事,如果 \(a_i+b_j\ge m\),将点按照 \(b\) 排序后,\(k>j\) 的点的代价都要减去 \(m\)。所以将所有点按照 \(b\) 排序,然后仍然拆成内点和外点,外点从点 \(i\) 连向 \(i+1\),代价 \(b_{i+1}-b_i\)。内外点之间连 0 边。这个构图非常巧妙,如果从点 \(i\) 的内点向某一个点连了一条边权为 \(a_i+b_j-m\) 的边,这个内点到达任何一个 \(k>j\),都相当于有一条边权为 \(a_i+b_k-m\) 的边。对于一个内点,他先朝 \(1\) 的外点连一条边权为 \(a_i+b_1\) 的边,在二分出第一个 \(a_i+b_j\ge m\) 的点,连一条边权为 \(a_i+b_j-m\) 的边,就可以达到题目中的效果。
但这样好像有些点同时连了 \(a_i+b_j\) 和 \(a_i+b_j-m\) 的边?但其实不影响答案。因为题目求最短路。
所有的边权为正,可以跑dij.
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=4e5+5;
int n,b[N],e_num,m,u,v,hd[N],vis[N];
LL dis[N];
struct node{
int a,b,id;
bool operator<(const node&n)const{
return b<n.b;
}
}a[N];
struct edge{
int v,nxt,w;
}e[N<<3];
struct dian{
int v;
LL w;
bool operator<(const dian&d)const{
return w>d.w;
}
};
priority_queue<dian>q;
void add_edge(int u,int v,int w)
{
// printf("%d %d %d\n",u,v,w);
e[++e_num]=(edge){v,hd[u],w};
hd[u]=e_num;
}
void dijkstra(int s)
{
q.push((dian){s,0});
memset(dis,0x7f,sizeof(dis));
dis[s]=0;
while(!q.empty())
{
int k=q.top().v;
q.pop();
if(vis[k])
continue;
vis[k]=1;
for(int i=hd[k];i;i=e[i].nxt)
{
if(dis[e[i].v]>dis[k]+e[i].w)
{
dis[e[i].v]=dis[k]+e[i].w;
q.push((dian){e[i].v,dis[e[i].v]});
}
}
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&a[i].a);
for(int i=1;i<=n;i++)
scanf("%d",&a[i].b),a[i].id=i;
sort(a+1,a+n+1);
for(int i=1;i<=n;i++)
b[i]=a[i].b;
for(int i=1;i<=n;i++)
{
// b[i]=a[i].b;
if(a[i].id==1)
u=i;
else if(a[i].id==n)
v=i;
add_edge(i+n,i,0);
add_edge(i,n+1,b[1]+a[i].a);
int k=lower_bound(b+1,b+n+1,m-a[i].a)-b;
// printf("%d %d\n",k,a[i].a);
if(k<=n)
add_edge(i,k+n,a[i].a+b[k]-m);
}
// printf("%d %d\n",u,v);
for(int i=1;i<n;i++)
add_edge(i+n,i+n+1,b[i+1]-b[i]);
dijkstra(u);
// for(int i=1;i<=n+n;i++)
// printf("%lld ",dis[i]);
printf("%lld",dis[v]);
return 0;
}
[ABC232G] Modulo Shortest Path的更多相关文章
- hdu-----(2807)The Shortest Path(矩阵+Floyd)
The Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- zoj 2760 How Many Shortest Path 最大流
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 Given a weighted directed graph ...
- The Shortest Path in Nya Graph
Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...
- hdu 3631 Shortest Path(Floyd)
题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...
- Shortest Path(思维,dfs)
Shortest Path Accepts: 40 Submissions: 610 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: ...
- Shortest Path
Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- (中等) HDU 4725 The Shortest Path in Nya Graph,Dijkstra+加点。
Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...
- 【ZOJ2760】How Many Shortest Path
How Many Shortest Path 标签: 网络流 描述 Given a weighted directed graph, we define the shortest path as th ...
- [Swift]LeetCode847. 访问所有节点的最短路径 | Shortest Path Visiting All Nodes
An undirected, connected graph of N nodes (labeled 0, 1, 2, ..., N-1) is given as graph. graph.lengt ...
- OSPF(Open Shortest Path First)
1.概述 路由协议OSPF全称为Open Shortest Path First,也就开放的最短路径优先协议,因为OSPF是由IETF开发的,所以所有厂商都可以用. OSPF的流量使用IP协议号. O ...
随机推荐
- 【题解】Educational Codeforces Round 142(CF1792)
没有手速,再加上被 E 卡了,废掉了. A.GamingForces 题目描述: Monocarp 正在玩电脑游戏.他打算杀死 \(n\) 个怪兽,第 \(i\) 个的血量为 \(h_i\). Mon ...
- 15.3K Star,超好用的开源协作式数字白板:tldraw
大家好,我是TJ 今天给大家推荐一个开源协作式数字白板:tldraw. tldraw的编辑器.用户界面和其他底层库都是开源的,你可以在它的开源仓库中找到它们.它们也在NPM上分发,提供开发者使用.您可 ...
- R3300L, Q7 ATV Android9固件
R3300L, Q7 ATV Android9固件 固件来源 https://www.znds.com/tv-1239603-1-1.html 之前在恩山上发布过1080p安卓6固件 https:// ...
- 为什么 Rust 备受开发者青睐?
引子 作为一名敏锐的前端开发者,您可能早已对 Rust 有所耳闻,毕竟近几年,使用 Rust 开发的前端构建工具每经发布,其卓越的性能数据总是能带来社区的一阵惊叹. 图片来源:https://swc. ...
- 0 基础晋级 Serverless 高手课 — 初识 Serverless(上)
应用 - 无服务器 2017- 2006 函数即服务 类似 云计算 (Serverless) faas 函数服务 + 后端数据库 账号服务 弹性,按量 服务器,客户端的终结 - 弹性 - 按量 优点 ...
- 二进制枚举&爆搜DFS
给定一个如下图所示的全圆量角器. 初始时,量角器上的指针指向刻度 0. 现在,请你对指针进行 n 次拨动操作,每次操作给定一个拨动角度 ai,由你将指针拨动 ai 度,每次的拨动方向(顺时针或逆时针) ...
- 【问题复盘】在Ubuntu 20.04下安装OFED驱动
复盘:在Ubuntu 20.04下安装OFED驱动 起因 最近收到两台服务器,都搭载了ConnectX-5 EX网卡.由于供应商预装了Ubuntu 20.04操作系统,而我们的后端代码也是基于Ubun ...
- 【爬虫实战】用Python采集任意小红书笔记下的评论,爬了10000多条,含二级评论!
目录 一.爬取目标 二.爬虫代码讲解 2.1 分析过程 2.2 爬虫代码 三.演示视频 一.爬取目标 您好!我是@马哥python说 ,一名10年程序猿. 我们继续分享Python爬虫的案例,今天爬取 ...
- 7/10 luoguRound 10 庆典 div3T1
#include<bits/stdc++.h> using namespace std; int m,n,x; int arr[100005]; int maxi = -1,maxn = ...
- idea的git插件,可以显示每一行代码的git版本记录,很好用
再给大家推荐一款idea的git插件----GitToolBox,可以显示每一行代码的git版本记录,很好用 效果图如下 可以在光标所在行代码的后面显示git的版本记录信息(提交的用户名,提交的时间等 ...