USACO 2008 March Gold cowjog
来源:互联网 发布:java面试会考哪些算法 编辑:程序博客网 时间:2024/04/20 20:09
Calculate the Kth shortest path in a Directed Acyclic Graph(DAG). Although there exists a complicated way to calculate the Kth shortest Path in Arbitrary Graph, we can get the answer using DP by taking advantage of the property of DAG. A DP way is available because the DAG naturally has got an order which we use frequently.
We just maintain an array of size K for each node, representing first K shortest path from source to that node. When we computing a node I, we just consider all the nodes that leads an edge to I. All we need to do is merging the arrays to get the first K items, then we fill them in the array of Node I.
Note: there may be multiple edges between two nodes.
Here is the code:
- #include <iostream>
- using namespace std;
- const int N = 1001;
- int ad[N][N], len[N][N], d[N][110];
- int pt[N];
- int n, m, kth;
- int main()
- {
- int i, j, k, t;
- freopen("cowjog.in", "r", stdin);
- freopen("cowjog.out", "w", stdout);
- scanf("%d%d%d",&n, &m, &kth);
- for(; m; m--)
- {
- scanf("%d%d%d",&i, &j, &k);
- ad[j][++ad[j][0]] = i;
- len[j][ad[j][0]] = k;
- }
- memset(d, -1, sizeof(d));
- d[n][0] = 0;
- for(i = n - 1; i >= 1; i--)
- {
- memset(pt, 0, sizeof(pt));
- for(int cnt = 0; cnt < kth; cnt++)
- {
- int tmp = -1, minD = INT_MAX;
- for(j = 1; j <= ad[i][0]; j++)
- {
- k = ad[i][j], t = len[i][j];
- if(d[k][pt[j]] != -1 && t + d[k][pt[j]] < minD)
- {
- minD = t + d[k][pt[j]];
- tmp = j;
- }
- }
- if(minD != INT_MAX)
- {
- d[i][cnt] = minD;
- pt[tmp]++;
- }
- }
- }
- for(i = 0; i < kth; i++)
- printf("%d/n",d[1][i]);
- scanf("%d",&n);
- return 0;
- }
- USACO 2008 March Gold cowjog
- [USACO 2008 March Gold] 土地购买
- POJ 2393 USACO March Gold
- 【USACO 2011 March Gold】切蛋糕
- 【USACO 2013 March Gold】奶牛逃跑
- USACO 2012 March Gold Large Banner
- BSOJ: 3748 【USACO 2006 March Gold】Milk Team Select产奶比赛
- USACO2012 March Gold Large Banner
- [USACO NOV06 GOLD]路障
- [usaco gold 2016.1]无线电通信
- [USACO GOLD] 选做
- BZOJ1591 USACO 2008 Dec Gold 4.Largest Fence Solution
- 【POJ3666】【USACO 2008 Feb Gold】 2.Cow Game 动规
- 【USACO 2008 Mar Gold】 3.Pearl Pairing 贪心 pq
- 【USACO 2008 Nov Gold】 1.Mixed Up Cows 状压DP、
- 【BZOJ1229】【USACO 2008 Nov Gold】 4.Toys sadstory 三分+贪心
- USACO 2008 Nov Gold 1.Mixed Up Cows
- USACO 2008 Jan Gold 3.Cell Phone Network 树形dp
- 致父亲
- 学而时习之,不亦乐乎;有朋自远方来,不亦乐乎
- 深入浅出JBoss Seam
- linux填空
- Hibernate SQL方言
- USACO 2008 March Gold cowjog
- 帧内预测与帧间预测
- 在iis中注册asp.net 2.0 (zz)
- Hibernate学习笔记
- 五种提高 SQL 性能的方法
- Linux选择题
- Struts入门教程(一)
- Struts入门教程(二)
- 论坛E-mail验正的正则表达式升级