[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 ...
随机推荐
- Lucene初识
1.概述 1.1 Lucene是apache软件基金会4 jakarta项目组的一个子项目: 是一个开放源代码的全文检索引擎工具包: 但它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了 ...
- mysql 语句执行的过程
客户端发送一条查询给服务器: 服务器先检查查询缓存,如果命中了缓存,则立刻返回存储在缓存中的结果.否则进入下一阶段. 服务器段进行SQL解析.预处理,在优化器生成对应的执行计划: mysql根据优化器 ...
- (转)Python格式化字符 %s %d %f
Python格式化字符 %s %d %f 原文:http://blog.csdn.net/huangfu77/article/details/54807835 格式 描述%% 百分号标记 #就是输出一 ...
- Android 开发手记一NDK编程实例
在Android上,应用程序的开发,大部分基于Java语言来实现.要使用c或是c++的程序或库,就需要使用NDK来实现.NDK是Native Development Kit的简称.它是一个工具集,集成 ...
- mvp需要加上单利模式
最大的中介者,需要设置成单利模式
- The Definitive C++ Book Guide and List--reference
http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list Reference Style - All ...
- bzoj 4543: [POI2014]Hotel加强版
Description 给出一棵树求三元组 \((x,y,z)\,,x<y<z\) 满足三个点两两之间距离相等,求三元组的数量 Solution 考虑暴力 \(DP\) 设 \(f[i][ ...
- jqGrid -treeGrid 按需加载
Load Rows On Demand (AJAX) 参考:http://www.guriddo.net/demo/treegridjs/
- docker-compose 安装solr+ikanalyzer
docker-compose.yml version: '3.1' services: solr: image: solr restart: always container_name: solr p ...
- Linux.net && mono
资料: http://www.cnblogs.com/xiaodiejinghong/archive/2013/04/01/2994216.html http://www.cnblogs.com/sh ...