USACO 2009 Feb Gold 3.Revamping Trails
来源:互联网 发布:2017护肤品数据 编辑:程序博客网 时间:2024/05/17 02:14
Description
Farmer John dutifully checks on the cows every day. He traverses so me 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.
Input
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
Output
- Line 1: The length of the shortest path after revamping no more than
K edges
Sample Input
4 4 11 2 102 4 101 3 13 4 100
Sample Output
1
Key To Problem
小小的图论+二维dpdis[u][num]表示到点u,翻新num条道路所需要走的最短的时间dis[to][num]=dis[u][num]+s;dis[to][num+1]=dis[u][num];需要用dijkstra+堆优化,据说SPFA超时,并没有尝试。
CODE
#include<queue>#include<vector>#include<cstdio>#include<cstring>#include<algorithm>#define N 10010using namespace std;vector<int>V[N];vector<int>pre[N];struct node{ int u,num,s; bool friend operator <(node x,node y) { return x.s>y.s; }};int n,m,l;int dis[N][30];bool used[N][30];void dijkstra(int u){ memset(dis,0x3f,sizeof(dis)); priority_queue<node>Q; node k; k.u=u,k.s=0,k.num=0; Q.push(k); dis[u][0]=0; while(Q.size()) { k=Q.top(); Q.pop(); if(used[k.u][k.num]) continue; used[k.u][k.num]=1; int num=k.num; for(int i=0;i<V[k.u].size();i++) { int to=V[k.u][i]; int s=pre[k.u][i]; if(dis[to][num]>dis[k.u][num]+s) { dis[to][num]=dis[k.u][num]+s; node p; p.u=to,p.num=num,p.s=dis[to][num]; Q.push(p); } if(num+1<=l&&dis[to][num+1]>dis[k.u][num]) { dis[to][num+1]=dis[k.u][num]; node p; p.u=to,p.num=num+1,p.s=dis[to][num+1]; Q.push(p); } } }}int main(){// freopen("revamp.in","r",stdin);// freopen("revamp.out","w",stdout); scanf("%d%d%d",&n,&m,&l); for(int i=1;i<=m;i++) { int x,y,z; scanf("%d%d%d",&x,&y,&z); V[x].push_back(y); pre[x].push_back(z); V[y].push_back(x); pre[y].push_back(z); } dijkstra(1); printf("%d\n",dis[n][l]); return 0;}
- USACO 2009 Feb Gold 3.Revamping Trails
- BZOJ1579 USACO 2009 Feb Gold 3.Revamping Trails Solution
- USACO 2009 Feb Revamping Trails 道路翻新
- 【USACO】2009 Feb Revamping Trails 道路翻新
- [Usaco2009 Feb]Revamping Trails 堆优化 Dijkstra
- BZOJ1597 [Usaco2009 Feb]Revamping Trails 道路升级
- BZOJ1579: [Usaco2009 Feb]Revamping Trails 道路升级
- BZOJ1579: [Usaco2009 Feb]Revamping Trails 道路升级
- 【bzoj1579】[Usaco2009 Feb]Revamping Trails 道路升级
- bzoj 1579: [Usaco2009 Feb]Revamping Trails 道路升级
- BZOJ1577 USACO 2009 Feb Gold 1.Fair Shuttle Solution
- BZOJ1578 USACO 2009 Feb Gold 2.Stock Market
- USACO 2009 Feb Gold 1.Fair Shuttle 贪心
- BZOJ 1579 Revamping Trails
- bzoj1579 [Usaco2009 Feb]Revamping Trails 道路升级 分层图dij+heap
- BZOJ 1579: [Usaco2009 Feb]Revamping Trails 道路升级 分层图最短路
- bzoj1579 [Usaco2009 Feb]Revamping Trails 道路升级(分层图最短路)
- POJ 3269 USACO FEB GOLD newbarn
- LocalBroadcastManager 的实现原理,还是 Binder?
- 我觉得还是好好写博客
- 密码宝贝教你追女七大招
- linux下实现mysql主主互备模式配置过程
- 设置linux开机自动挂载硬盘
- USACO 2009 Feb Gold 3.Revamping Trails
- Linux命令(5)
- 面试题:堆和队列
- linux tomcat 8080访问不了
- 开发笔记-Hibernate
- C++ Primer 5e chapter 10.1
- 数组排序(插入、选择、希尔、堆、归并、快速、冒泡)
- java的自动装箱与拆箱
- HDU 5366 The mook jong