USACO 2009 Feb Gold 3.Revamping Trails
2015-08-21 18:33
686 查看
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 KLines 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 thanK edges
Sample Input
4 4 1 1 2 10 2 4 10 1 3 1 3 4 100
Sample Output
1
Key To Problem
小小的图论+二维dp dis[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 10010 using namespace std; vector<int>V ; vector<int>pre ; struct node { int u,num,s; bool friend operator <(node x,node y) { return x.s>y.s; } }; int n,m,l; int dis [30]; bool used [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 [l]); return 0; }
相关文章推荐
- 初学图论-Kahn拓扑排序算法(Kahn's Topological Sort Algorithm)
- 初学图论-Bellman-Ford单源最短路径算法
- 初学图论-DAG单源最短路径算法
- 初学图论-Dijkstra单源最短路径算法
- 初学图论-Dijkstra单源最短路径算法基于优先级队列(Priority Queue)的实现
- 封装好的Folyd建图,C++源码
- LCA模板
- USACO 3.2.2:Stringsobits
- 图论学习笔记之一——Floyd算法
- USACO 3.1 Shaping Regions
- 【LCA】SPOJ QTREE2
- poj 3249 Test for Job 最长路
- HDU 2544
- Timus 1557 Network Attack DFS+各种各种...
- HDU1289 Tarjan-模板题
- Poj2638 网络流+最短路+二分答案
- Aizu1311 分层图最短路 (...大概)
- 【解题报告】【USACO】酸奶工厂
- HDU 3631 Shortest Path
- 二分图匹配模板