[USACO09FEB] Revamping Trails 【分层图+Dijkstra】
任意门:https://www.luogu.org/problemnew/show/P2939
Revamping Trails
题目描述
Farmer John dutifully checks on the cows every day. He traverses some of the M (1 <= M <= 50,000) trails conveniently numbered 1..M from pasture 1 all the way out to pasture N (a journey which is always possible for trail maps given in the test data). The N (1 <= N <= 10,000) pastures conveniently numbered 1..N on Farmer John's farm are currently connected by bidirectional dirt trails. Each trail i connects pastures P1_i and P2_i (1 <= P1_i <= N; 1 <= P2_i <= N) and requires T_i (1 <= T_i <= 1,000,000) units of time to traverse.
He wants to revamp some of the trails on his farm to save time on his long journey. Specifically, he will choose K (1 <= K <= 20) trails to turn into highways, which will effectively reduce the trail's traversal time to 0. Help FJ decide which trails to revamp to minimize the resulting time of getting from pasture 1 to N.
TIME LIMIT: 2 seconds
输入输出格式
输入格式:
* Line 1: Three space-separated integers: N, M, and K
* Lines 2..M+1: Line i+1 describes trail i with three space-separated integers: P1_i, P2_i, and T_i
输出格式:
* Line 1: The length of the shortest path after revamping no more than K edges
输入输出样例
4 4 1
1 2 10
2 4 10
1 3 1
3 4 100
1
说明
K is 1; revamp trail 3->4 to take time 0 instead of 100. The new shortest path is 1->3->4, total traversal time now 1.
题意概括:
给出一个 N 个节点,M 条边的无向图,可以使其中 K 条路径花费为 0 ,求从 点 1 到点 N 的最短路。
解题思路:
分层图,跑一遍优先队列(STL)优化的 Dijkstra。
分层图的含义就是把原图复制 K 层(平行宇宙概念),从下面一层有虫洞到上面一层(即花费为0),通过这种穿越 K 层图的做法达到使其中的 K 条边权值为0.
而现实代码中不需要真的建 K 个图,而是建立一个 dp[ i ][ k ] 保存 1 到 i 穿越了 k 条边的最短路(动态规划的思想,十分巧妙)。
接下来的穿越不同层可以说是状态转移:dp[ to ][ k ] = min( dp[to][k], dp[from][k] + w[edge])
dp[ to ][ k+1 ] = min( dp[ to ][ k+1 ] , dp[ from ][k] )
AC code:
//分层图最短路
#include <bits/stdc++.h>
#define LL long long int
#define INF 0x3f3f3f3f
using namespace std;
//const int INF = 127/3;
const int MAXN = 1e4+;
const int MAXK = ;
const int MAXM = 5e4+;
int N, M, K, dis[MAXN][MAXK];
int head[MAXN], node[MAXM<<], Next[MAXM<<], w[MAXM<<], cnt;
bool vis[MAXN][MAXK]; struct date
{
int u, k, val;
bool operator < (const date& a)const{ //结构体内定义优先队列优化级
return val > a.val;
}
};
priority_queue<date> Q; inline void add(int from, int to, int weight)
{
Next[++cnt] = head[from];
head[from] = cnt;
w[cnt] = weight;
node[cnt] = to;
} inline void Dijkstra(int s)
{
memset(dis, INF, sizeof(dis));
int v;
date tp;
tp.u = s, tp.k = ;
tp.val = dis[s][] = ;
Q.push(tp);
vis[s][] = true;
while(!Q.empty()){
tp = Q.top(), Q.pop();
vis[tp.u][tp.k] = ;
for(int i = head[tp.u]; i != -; i = Next[i]){
v = node[i];
if(dis[v][tp.k] > dis[tp.u][tp.k] + w[i]){
dis[v][tp.k] = dis[tp.u][tp.k] + w[i];
if(!vis[v][tp.k]){
Q.push(date{v, tp.k, dis[v][tp.k]});
vis[v][tp.k] = true;
}
}
if(tp.k+ <= K){
if(dis[v][tp.k+] > dis[tp.u][tp.k]){
dis[v][tp.k+] = dis[tp.u][tp.k];
if(!vis[v][tp.k+]){
Q.push(date{v, tp.k+, dis[v][tp.k+]});
vis[v][tp.k+] = true;
}
}
}
}
}
}
void init()
{
memset(head, -, sizeof(head));
cnt = ;
}
int main()
{
int u, v, w;
scanf("%d%d%d", &N, &M, &K);
init();
for(int i = ; i <= M; i++){
scanf("%d%d%d", &u, &v, &w);
add(u, v, w);
add(v, u, w);
}
Dijkstra();
printf("%d\n", dis[N][K]);
return ;
}
[USACO09FEB] Revamping Trails 【分层图+Dijkstra】的更多相关文章
- Bzoj 1579: [Usaco2009 Feb]Revamping Trails 道路升级 dijkstra,堆,分层图
1579: [Usaco2009 Feb]Revamping Trails 道路升级 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1573 Solv ...
- P2939 [USACO09FEB]改造路[分层图最短路]
题意翻译 约翰一共有N)个牧场.由M条布满尘埃的小径连接.小径可 以双向通行.每天早上约翰从牧场1出发到牧场N去给奶牛检查身体. 通过每条小径都需要消耗一定的时间.约翰打算升级其中K条小径,使之成为高 ...
- 【BZOJ-3627】路径规划 分层图 + Dijkstra + spfa
3627: [JLOI2014]路径规划 Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 186 Solved: 70[Submit][Status] ...
- P4568 [JLOI2011]飞行路线 / P2939 [USACO09FEB]Revamping Trails G
题目描述 Link Alice 和 Bob 现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在 \(n\) 个城市设有业务,设这些城市分别标记为 \(0\) 到 \(n-1\),一共 ...
- [USACO09FEB]改造路Revamping Trails 分层最短路 Dijkstra BZOJ 1579
题意翻译 约翰一共有N)个牧场.由M条布满尘埃的小径连接.小径可 以双向通行.每天早上约翰从牧场1出发到牧场N去给奶牛检查身体. 通过每条小径都需要消耗一定的时间.约翰打算升级其中K条小径,使之成为高 ...
- bzoj 1579: [Usaco2009 Feb]Revamping Trails 道路升级——分层图+dijkstra
Description 每天,农夫John需要经过一些道路去检查牛棚N里面的牛. 农场上有M(1<=M<=50,000)条双向泥土道路,编号为1..M. 道路i连接牛棚P1_i和P2_i ...
- HDU-3499Flight (分层图dijkstra)
一开始想的并查集(我一定是脑子坏掉了),晚上听学姐讲题才知道就是dijkstra两层: 题意:有一次机会能使一条边的权值变为原来的一半,询问从s到e的最短路. 将dis数组开成二维,第一维表示从源点到 ...
- [Usaco2009 Feb]Revamping Trails 堆优化 Dijkstra
.. 这题一眼就看出就是一个二维DP dp[i][j]表示到点i使用了j次免费边的最短距离 MD 卡SPFA.. 遂写dij. AC #include <iostream> #includ ...
- 洛谷 P2939 [USACO09FEB]改造路Revamping Trails 题解
P2939 [USACO09FEB]改造路Revamping Trails 题目描述 Farmer John dutifully checks on the cows every day. He tr ...
随机推荐
- oracle 日志文件
--Oracel Grid 11.2的Agent有多个,其中有两个最重要:orarootagent.oraagent --它们有各自的日志文件,这些Agent的日志文件位于: $grid_home/l ...
- Murano Weekly Meeting 2016.08.23
Meeting time: 2016.August.23 1:00~2:00 Chairperson: Kirill Zaitsev, from Mirantis Meeting summary: ...
- C#(Winform)的SaveFileDialog(文件保存对话框)控件使用
#region 保存对话框 private void ShowSaveFileDialog() { //string localFilePath, fileNameExt ...
- mysql中字段类型是datetime时比较大小问题
select sum(studychj) as tofflinejz from afterline where studybegin >= '2010-01-01 00:00:00' and s ...
- js 中标签的增删 方法
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Visual Studio 安装OpenCV及问题总结
1.VS安装OpenCV基本步骤 1)安装Visual Studio 下载网址https://opencv.org/releases.html# 2)安装OpenCV 下载网址https://www. ...
- Excle 导入DataSet
using System.Data.OleDb;using System.Data; public void ReadExcelFiless() { //strin ...
- 合约实战,代币合约,DAPP开发
1. ERC20标准 https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md pragma solidity ^; //定义接口 con ...
- 日期控件html
日期控件多的是,这里收录的是最简单的一种 <html> <head> <script type="text/javascript"> funct ...
- struts2====之=======初识struts
---恢复内容开始--- 1.什么是web框架? 目前应用得较多的三种服务器瑞页面描写技术就是ASP,JSP和PHP.J S P通过在HTMLJî面 文件中嵌入J a v a脚本代码,从而实现动态网页 ...