[USACO09FEB]改造路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
约翰一共有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
输入输出样例
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.
建立分层图。
f[u][t]表示在节点u时已经免费乘坐t次的最少花
费。照样跑最短路。
枚举与u相连的所有节点v,w(u,v)表示权值。
若t<k:
f[v][t+1]=min(f[v][t+1],f[u][t])
对于所有:
f[v][t]=min(f[v][t],f[u][t]+w(u,v))
不过这题数据大一些,要用优先队列优化SPFA
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
struct Node
{
int next,to,dis;
}edge[];
struct XXX
{
int dis,x,k;
friend bool operator<(XXX a,XXX b)
{return a.dis>b.dis;}
};
int num,head[];
int dist[][];
int n,m,k,S,T;
int ans;
bool vis[][];
void add(int u,int v,int d)
{
num++;
edge[num].next=head[u];
head[u]=num;
edge[num].to=v;
edge[num].dis=d;
}
void SPFA()
{int i;
priority_queue<XXX>Q;
Q.push((XXX){,S,});
dist[S][]=;
while (Q.empty()==)
{
XXX u=Q.top();
Q.pop();
for (i=head[u.x];i;i=edge[i].next)
{int v=edge[i].to;
if (dist[v][u.k]>dist[u.x][u.k]+edge[i].dis)
{
dist[v][u.k]=dist[u.x][u.k]+edge[i].dis;
Q.push((XXX){dist[v][u.k],v,u.k});
}
if (u.k+<=k&&dist[v][u.k+]>dist[u.x][u.k])
{
dist[v][u.k+]=dist[u.x][u.k];
Q.push((XXX){dist[v][u.k+],v,u.k+});
}
}
}
}
int main()
{int i,u,v,c;
cin>>n>>m>>k;
memset(dist,/,sizeof(dist));
S=;T=n;
for (i=;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&c);
add(u,v,c);
add(v,u,c);
}
SPFA();
cout<<dist[n][k];
}
[USACO09FEB]改造路Revamping Trails的更多相关文章
- P2939 [USACO09FEB]改造路Revamping Trails
P2939 [USACO09FEB]改造路Revamping Trails 同bzoj2763.不过dbzoj太慢了,bzoj又交不了. 裸的分层图最短路. f[i][j]表示免费走了j条路到达i的最 ...
- 洛谷 P2939 [USACO09FEB]改造路Revamping Trails 题解
P2939 [USACO09FEB]改造路Revamping Trails 题目描述 Farmer John dutifully checks on the cows every day. He tr ...
- [USACO09FEB] 改造路Revamping Trails | [JLOI2011] 飞行路线
题目链接: 改造路 飞行路线 其实这两道题基本上是一样的,就是分层图的套路题. 为什么是分层图呢?首先,我们的选择次数比较少,可以把这几层的图建出来而不会爆空间.然后因为选择一个边权为0的路线之后我们 ...
- 洛谷P2939 [USACO09FEB]改造路Revamping Trails
题意翻译 约翰一共有\(N\))个牧场.由\(M\)条布满尘埃的小径连接.小径可 以双向通行.每天早上约翰从牧场\(1\)出发到牧场\(N\)去给奶牛检查身体. 通过每条小径都需要消耗一定的时间.约翰 ...
- 洛谷 P2939 [USACO09FEB]改造路Revamping Trails
题意翻译 约翰一共有N)个牧场.由M条布满尘埃的小径连接.小径可 以双向通行.每天早上约翰从牧场1出发到牧场N去给奶牛检查身体. 通过每条小径都需要消耗一定的时间.约翰打算升级其中K条小径,使之成为高 ...
- [USACO09FEB]改造路Revamping Trails 分层最短路 Dijkstra BZOJ 1579
题意翻译 约翰一共有N)个牧场.由M条布满尘埃的小径连接.小径可 以双向通行.每天早上约翰从牧场1出发到牧场N去给奶牛检查身体. 通过每条小径都需要消耗一定的时间.约翰打算升级其中K条小径,使之成为高 ...
- 洛谷P2939 [USACO09FEB]改造路Revamping Trails(最短路)
题目描述 Farmer John dutifully checks on the cows every day. He traverses some of the M (1 <= M <= ...
- LUOGU P2939 [USACO09FEB]改造路Revamping Trails
题意翻译 约翰一共有N)个牧场.由M条布满尘埃的小径连接.小径可 以双向通行.每天早上约翰从牧场1出发到牧场N去给奶牛检查身体. 通过每条小径都需要消耗一定的时间.约翰打算升级其中K条小径,使之成为高 ...
- 【luogu P2939 [USACO09FEB]改造路Revamping Trails】 题解
题目链接:https://www.luogu.org/problemnew/show/P2939 本来说是双倍经验题,跟飞行路线一样的,结果我飞行路线拿deque优化SPFA过了这里过不了了. 所以多 ...
随机推荐
- c语言的第三次---单程循环结构
一.PTA实验作业 题目1 1.代码 int N,i; double height; //height代表身高 char sex; //代表男女性别 scanf("%d",& ...
- C语言第六周博客作业--数据类型
一.PTA实验作业 题目1: 7-6 掉入陷阱的数字 1. 本题PTA提交列表 2.设计思路 定义变量N,i,g=1表示位数,a表示各位数字相加的和,b=0,j,N1,c,d用于储存N do{ for ...
- 敏捷冲刺每日报告四(Java-Team)
第四天报告(10.28 周六) 团队:Java-Team 成员: 章辉宇(284) 吴政楠(286) 陈阳(PM:288) 韩华颂(142) 胡志权(143) github地址:https://gi ...
- PTA題目的處理(二)
題目7-1 計算分段函數[1] 1.實驗代碼 #include <stdio.h> int main() { float x,y; scanf("%f",&x) ...
- verilog学习笔记(3)_task/case小例子及其tb
module ex_case `timescale lns/1ns module ex_case( input wire rst_n, input wire sclk, output reg [7:0 ...
- SUN平台服务器光纤共享存储互斥失败如何恢复数据?
服务器数据恢复故障描述: 服务器最初的设计思路为将两台SPARC SOLARIS系统通过光纤交换机共享同一存储作为CLUSTER使用,正常情况下A服务器工作,当A服务器发生故障宕机后即可将其关机然后开 ...
- XML之自动生成类,添加,修改,删除类的属性
1. class ClassHelperDemo { public static void Main() { #region 演示一:动态生成类. //生成一个类t. Type t = ClassHe ...
- sublime的使用技巧
ctr+shift+d是复制当前行当下一行2.使用Sublime text 3 编写代码是一种享受,使用Sublime text 3 格式化HTML代码,需要安装插件,具体安装步骤如下:1.打开菜单- ...
- mingw打dll ,lib包命令和调用
1,下面的命令行将这个代码编译成 dll. gcc mydll.c -shared -o mydll.dll -Wl,--out-implib,mydll.lib 其中 -shared 告诉gcc d ...
- ELK学习总结(2-1)mavel -》sense 和 索引初始化
1.安装 sudo elasticsearch/bin/plugin -install elasticsearch/mavel/latest http://localhost:9200/_plugi ...