当前位置:网站首页>Luogu p2939 [usaco09feb]revamping trails G
Luogu p2939 [usaco09feb]revamping trails G
2022-06-27 05:17:00 【q779】
Luogu P2939 [USACO09FEB]Revamping Trails G Answer key
Topic link :P2939 [USACO09FEB]Revamping Trails G
The question :
John has a total of N N N A ranch . from M M M A dusty path connects . The path allows two-way traffic . Every morning John comes from the pasture 1 1 1 Set off for the pasture N N N Go and examine the cow .
It takes time to cross each trail . John is going to upgrade K K K A path , Make it a highway . The traffic on the highway is almost instantaneous , Therefore, the traffic time of expressway is 0 0 0.
Please help John decide which paths to upgrade , Make him from 1 1 1 Ranch No. to No N N N It takes the shortest time to pasture No .
1 ≤ n ≤ 1 0 4 , 1 ≤ m ≤ 5 × 1 0 4 , 1 ≤ k ≤ 20 1 \le n \le 10^4,~1\le m \le 5 \times 10^4 ,~ 1\le k \le 20 1≤n≤104, 1≤m≤5×104, 1≤k≤20
Consider building a layered graph to run the shortest path
First build k + 1 k+1 k+1 The same graph
The first i i i The node of the layer u i u_i ui towards The first i + 1 i+1 i+1 Layer of v i v_i vi Even the edge
Indicates that it has been used i i i Time modification , Now it's number one i + 1 i+1 i+1 Time modification , The change is ( u , v ) (u,v) (u,v) This side
Then run a single source shortest circuit
Note that arrays with edges are usually opened M × ( K + 1 ) × 4 M \times (K+1) \times 4 M×(K+1)×4
Then there may be a time when you can't get there k k k The situation where the edge is reached
So put every i n in in and ( i + 1 ) n (i+1)n (i+1)n Even the edge
The final answer is d k n + n d_{kn+n} dkn+n
Be careful d d d The array should be opened long long
Time complexity O ( k n log k m ) O(kn\log km) O(knlogkm)
Code :
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iomanip>
#include <queue>
using namespace std;
// #define int long long
typedef long long ll;
#define INF 0x3f3f3f3f3f3f3f3f
#define N (int)(1e4+5)
#define K (int)(22)
#define M (int)(5e4+5)
struct Edge{
int u,v,w,next;}e[M*K*4];
struct node{
int u; ll dis;};
ll d[N*K];
bool operator<(node a,node b){
return a.dis>b.dis;}
int n,m,k,pos=1,head[N*K],vis[N*K];
priority_queue<node> q;
void addEdge(int u,int v,int w)
{
e[++pos]={
u,v,w,head[u]};
head[u]=pos;
}
void dijkstra(int st)
{
memset(d,0x3f,sizeof(d));
d[st]=0; q.push({
st,0});
while(!q.empty())
{
int u=q.top().u;q.pop();
if(vis[u])continue;
vis[u]=1;
for(int i=head[u]; i; i=e[i].next)
{
int v=e[i].v,w=e[i].w;
if(d[v]>d[u]+w)
{
d[v]=d[u]+w;
q.push({
v,d[v]});
}
}
}
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen("check.in","r",stdin);
// freopen("check.out","w",stdout);
cin >> n >> m >> k;
for(int i=1,u,v,w; i<=m; i++)
{
cin >> u >> v >> w;
addEdge(u,v,w);addEdge(v,u,w);
for(int j=1; j<=k; j++)
{
addEdge(j*n+u,j*n+v,w);
addEdge(j*n+v,j*n+u,w);
addEdge((j-1)*n+u,j*n+v,0);
addEdge((j-1)*n+v,j*n+u,0);
}
}
for(int i=1; i<=k; i++)
addEdge((i-1)*n+n,i*n+n,0);
dijkstra(1);
cout << d[k*n+n] << '\n';
return 0;
}
Reprint please explain the source
边栏推荐
- Edge loads web pages in IE mode - edge sets ie compatibility
- Ad22 Gerber files Click to open the Gerber step interface. Official solutions to problems
- 013 basics of C language: C pointer
- Obtenir le volume du système à travers les plateformes de l'unit é
- 网易云音乐params和encSecKey参数生成代码
- 双位置继电器RXMVB2 R251 204 110DC
- ES6 0622 III
- 双位置继电器DLS-34A DC0.5A 220VDC
- 微服务系统设计——统一鉴权服务设计
- Comprehensive application of OpenCV in contour detection and threshold processing
猜你喜欢

导航【机器学习】

leetcode298周赛记录

STM32 reads IO high and low level status

Microservice system design -- distributed transaction service design

AD22 gerber files 点开 gerber steup 界面 有问题 官方解决方法

Pycharm 中 Terminal 无法进入 venv 环境的问题

认知篇----2022高考志愿该如何填报

快速排序(非遞歸)和歸並排序

Niuke practice 101-c reasoning clown - bit operation + thinking

双位置继电器RXMVB2 R251 204 110DC
随机推荐
jq怎么获取元素的id名
Comprehensive application of OpenCV in contour detection and threshold processing
Avoid asteroids
高翔slam14讲-笔记1
C语言实现定时器
The most detailed download tutorial of MySQL
es6 0622三
快速排序(非递归)和归并排序
清华大学开源软件镜像站网址
差点因为 JSON.stringify 丢了奖金...
微信小程序刷新当前页面
stm32读取IO高低电平状态
洛谷P4683 [IOI2008] Type Printer 题解
【FPGA】基于bt1120时序设计实现棋盘格横纵向灰阶图数据输出
leetcode298周赛记录
golang hello 安装环境异常【已解决】
Some articles about component packaging and my experience
Halon common affine transformation operators
认知篇----2022高考志愿该如何填报
leetcode-20. Valid parentheses -js version