题目描述

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

约翰一共有N)个牧场.由M条布满尘埃的小径连接.小径可 以双向通行.每天早上约翰从牧场1出发到牧场N去给奶牛检查身体.

通过每条小径都需要消耗一定的时间.约翰打算升级其中K条小径,使之成为高 速公路.在高速公路上的通行几乎是瞬间完成的,所以高速公路的通行时间为0.

请帮助约翰决定对哪些小径进行升级,使他每天早上到牧场W花的时间最少.输出这个最少 的时间.

输入输出格式

输入格式:

* 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

输入输出样例

输入样例#1: 复制

4 4 1
1 2 10
2 4 10
1 3 1
3 4 100
输出样例#1: 复制

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.

如果你知道什么叫做分层图的话那就是个裸题

否则就是个神题

首先在原图上处理肯定是不好做

那么我们把图分层,具体来说建K+1张原图,图与图之间的边权为0,边为原图中的边

这样跑一个二维dijstra(真难写)就好了

#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#define Pair pair<int,int>
#define F first
#define S second
const int MAXN=1e6+;
using namespace std;
inline int read()
{
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int N,M,K;
int dis[MAXN][],vis[MAXN][];
struct node
{
int u,v,w,nxt;
}edge[MAXN];
int head[MAXN],num=;
inline void AddEdge(int x,int y,int z)
{
edge[num].u=x;edge[num].v=y;edge[num].w=z;edge[num].nxt=head[x];
head[x]=num++;
}
void Dijstra()
{
memset(dis,0xf,sizeof(dis));
dis[][]=;//第i个点,第j层
priority_queue< pair<int,Pair > > q;
q.push(make_pair(,make_pair(,)));//第一个表示点,第二个表示层
while(q.size()!=)
{
while(vis[q.top().S.F][q.top().S.S]&&q.size()>) q.pop();
Pair p=q.top().second;
vis[p.F][p.S]=;
for(int i=head[p.F];i!=-;i=edge[i].nxt)
{
int will=edge[i].v;
if(vis[will][p.S]==&&dis[will][p.S]>dis[p.F][p.S]+edge[i].w)
dis[will][p.S]=dis[p.F][p.S]+edge[i].w,
q.push(make_pair(-dis[will][p.S],make_pair(will,p.S)));
if(p.S+<=K&&vis[will][p.S+]==&&dis[will][p.S+]>dis[p.F][p.S])
dis[will][p.S+]=dis[p.F][p.S],
q.push(make_pair(-dis[will][p.S+],make_pair(will,p.S+)));
}
}
printf("%d",dis[N][K]);
}
int main()
{
memset(head,-,sizeof(head));
N=read();M=read();K=read();
for(int i=;i<=M;i++)
{
int x=read(),y=read(),z=read();
AddEdge(x,y,z);
AddEdge(y,x,z);
}
Dijstra();
return ;
}

洛谷P2939 [USACO09FEB]改造路Revamping Trails(最短路)的更多相关文章

  1. 洛谷 P2939 [USACO09FEB]改造路Revamping Trails 题解

    P2939 [USACO09FEB]改造路Revamping Trails 题目描述 Farmer John dutifully checks on the cows every day. He tr ...

  2. 洛谷P2939 [USACO09FEB]改造路Revamping Trails

    题意翻译 约翰一共有\(N\))个牧场.由\(M\)条布满尘埃的小径连接.小径可 以双向通行.每天早上约翰从牧场\(1\)出发到牧场\(N\)去给奶牛检查身体. 通过每条小径都需要消耗一定的时间.约翰 ...

  3. 洛谷 P2939 [USACO09FEB]改造路Revamping Trails

    题意翻译 约翰一共有N)个牧场.由M条布满尘埃的小径连接.小径可 以双向通行.每天早上约翰从牧场1出发到牧场N去给奶牛检查身体. 通过每条小径都需要消耗一定的时间.约翰打算升级其中K条小径,使之成为高 ...

  4. P2939 [USACO09FEB]改造路Revamping Trails

    P2939 [USACO09FEB]改造路Revamping Trails 同bzoj2763.不过dbzoj太慢了,bzoj又交不了. 裸的分层图最短路. f[i][j]表示免费走了j条路到达i的最 ...

  5. LUOGU P2939 [USACO09FEB]改造路Revamping Trails

    题意翻译 约翰一共有N)个牧场.由M条布满尘埃的小径连接.小径可 以双向通行.每天早上约翰从牧场1出发到牧场N去给奶牛检查身体. 通过每条小径都需要消耗一定的时间.约翰打算升级其中K条小径,使之成为高 ...

  6. 【luogu P2939 [USACO09FEB]改造路Revamping Trails】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2939 本来说是双倍经验题,跟飞行路线一样的,结果我飞行路线拿deque优化SPFA过了这里过不了了. 所以多 ...

  7. P2939 [USACO09FEB]改造路Revamping Trails(分层图最短路)

    传送门 完了我好像连分层图最短路都不会了……果然还是太菜了…… 具体来说就是记录一个步数表示免费了几条边,在dijkstra的时候以步数为第一关键字,距离为第二关键字.枚举边的时候分别枚举免不免费下一 ...

  8. [USACO09FEB] 改造路Revamping Trails | [JLOI2011] 飞行路线

    题目链接: 改造路 飞行路线 其实这两道题基本上是一样的,就是分层图的套路题. 为什么是分层图呢?首先,我们的选择次数比较少,可以把这几层的图建出来而不会爆空间.然后因为选择一个边权为0的路线之后我们 ...

  9. 分层图【p2939】[USACO09FEB]改造路Revamping Trails

    Description 约翰一共有N)个牧场.由M条布满尘埃的小径连接.小径可 以双向通行.每天早上约翰从牧场1出发到牧场N去给奶牛检查身体. 通过每条小径都需要消耗一定的时间.约翰打算升级其中K条小 ...

随机推荐

  1. Android Fragment RecycleListView

    1.新建SuperActivity package com.example.ting.criminalintentpractise; import android.os.Bundle;import a ...

  2. JVM源码分析之javaagent原理完全解读--转

    原文地址:http://www.infoq.com/cn/articles/javaagent-illustrated 概述 本文重点讲述javaagent的具体实现,因为它面向的是我们Java程序员 ...

  3. Projective Texture Mapping - 投影纹理

    昨天导师让写一个投影纹理,将一个相机渲染的图片的一部分投影到另外一个相机里面,目的是无缝的拼接. 投影纹理就和shadow map一样,都是将片元转换到另外一个相机/光源坐标系下,投影后找到对应的纹素 ...

  4. runloop源代码

    https://github.com/zzf073/runloopDemo /** *  调度例程 *  当将输入源安装到run loop后,调用这个协调调度例程,将源注册到客户端(可以理解为其他线程 ...

  5. iproute2+tc notes

    iproute2+tc notes The iproute2+tc package allows access to the variety of neat new networking featur ...

  6. 郑晔谈 Moco 框架的开发:写一个好的内部 DSL ,写一个表达性好的程序

    作者:张龙 出处:http://www.infoq.com/cn/news/2013/07/zhengye-on-moco 郑晔谈Moco框架的开发:写一个好的内部DSL,写一个表达性好的程序 作者  ...

  7. docker mysql pxc集群(percona-xtradb-cluster)

    docker pull percona/percona-xtradb-cluster docker tag percona/percona-xtradb-cluster pxc docker netw ...

  8. Shell(二)运算符

    基本运算符 Shell 和其他编程语言一样,支持多种运算符,包括: 算数运算符 关系运算符 布尔运算符 字符串运算符 文件测试运算符 原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如 ...

  9. Shell(一)变量

    一.简介 Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁.Shell 既是一种命令语言,又是一种程序设计语言. Shell 是指一种应用程序,这个应用程序提供了一个界面,用 ...

  10. code-reading-notes--libyang-1

    API struct lyd_node * lyd_parse_xml(struct ly_ctx *ctx, struct lyxml_elem **root, int options, ...) ...