[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 ...
随机推荐
- Unity 代码控制游戏对象是父物体的第多少个子对象
一个canvas下的游戏对象,排列顺序越往下,渲染顺序就越靠后,就会覆盖在先前的图形上.也就是说,运行游戏后,物体的渲染顺序是一个一个计算的. Transform.SetSiblingIndex(in ...
- C++的一些编程规范
新规范的目标: 让代码排错更加简单 程序员专心于业务逻辑 将一些错误交给编译器处理 提高代码可维护性 逐步实现插件化 编码 使用array(QT下用QVarLengthArray)代替和vector代 ...
- 第十七章:使用media插件来播放声音
前面已经讲过如何基于push notification自定义sound,但是还是存在几个问题: IOS app在前台运行的时候,如何播放sound?因为这个时候push notification是不起 ...
- 1.1 js基础
2.代码从上往下,从左往右执行. 函数声明在哪里不重要,重要的是在哪里调用. undefined 未定义 3.数据类型 12,5 number 'abc' 字 ...
- pat00-自测5. Shuffling Machine (20)
00-自测5. Shuffling Machine (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Sh ...
- Docker进阶
Docker进阶 Docker容器命名与重命名 命名 docker run -it -p 80:80 --name containerName img:tag /bin/bash 重命名 docker ...
- C# 类型、对象、线程栈和托管堆在运行时的关系
我们将讨论类型.对象.线程栈和托管堆在运行时的相互关系,假定有以下两个类定义: internal class Employee { public int GetYearsEmplo ...
- 对while;do while;for三种循环语句的理解与区分。
while:先判断表达式的值,在表达式值为真的情况下执行循环语句,直到表达式值为假结束循环: while(循环条件) { 循环体. } do-while:先执行循环体语句一次,再判别表达式的值,在表达 ...
- MongoDB windows基础配置及集群搭建
由于公司业务的发展MSSQL已处于瓶颈.因为没钱买牛逼服务器只能靠软件来实现最大优化了.原来的系统架构如下图:
- 2015-08-19(i++与++i的思考)
今天看到一个很有趣的东西,关于表达式优先级的问题.如下. ; int j=(i++)+(i++); j的值是多少? 分析:编译器是从左往右编译也就是说编译器先做(i++) 1.(i++)由于是++所以 ...