当前位置:网站首页>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
边栏推荐
- RTP 发送PS流工具(已经开源)
- Microservice system design -- microservice monitoring and system resource monitoring design
- 006 C language foundation: C storage class
- Microservice system design - service fusing and degradation design
- Neo4j community conflicts with neo4j desktop
- Edge loads web pages in IE mode - edge sets ie compatibility
- 躲避小行星游戏
- 《数据库原理与应用》第一版 马春梅……编著 期末复习笔记
- When STM32 turns off PWM output, it is a method to fix IO output at high or low level.
- RTP sending PS stream tool (open source)
猜你喜欢
随机推荐
什么是BFC?有什么用?
Redis高可用集群(哨兵、集群)
双位置继电器JDP-1440/DC110V
微信小程序WebSocket使用案例
RTP sending PS stream tool (open source)
牛客练习赛101-C 推理小丑---位运算+思维
Web3 has not been implemented yet, web5 suddenly appears!
双位置继电器HJWS-9440
微服务系统设计——服务链路跟踪设计
012 C language foundation: C array
双位置继电器RXMD2-1MRK001984 DC220V
019 C语言基础:C预处理
Flink生产问题(1.10)
双位置继电器XJLS-8G/220
《数据库原理与应用》第一版 马春梅……编著 期末复习笔记
机械转码日记【17】模板,STL简介
【622. 设计循环队列】
使用域名转发mqtt协议,避坑指南
011 C language basics: C scope rules
Microservice system design -- message caching service design







