英文原题 中文题译
大意:农场之间有路构成稀疏无向图,若干奶牛分散在不同农场,要让奶牛到某个农场集合,如何代价最小。
稀疏图上求到所有点对的最短路径,用Bellman Ford算法的队列实现(SPFA),其思想是:开始时队列中只有源节点,不断取出队列中的首节点,并对其相邻节点检查是否可用该节点更新其距离,若可,且相邻节点不在队中,则将其入队。这和宽度优先搜索很像,所不同的是每个节点可以多次入队。
在求得最短路径之后计算集合的代价取最小值即可。
/* ID: blackco3 TASK: butter LANG: C++ */ #include <iostream> #include <memory.h> using namespace std; #define _max_farm_ 800 #define _max_path_ 1450 #define _max_cow_ 500 struct t_path { int v1, v2, dis ; } pathes[_max_path_]; struct t_adj { int to, dis ; t_adj( int t=0, int d=0 ) : to(t), dis(d) {} ; } adj_buf[_max_path_<<1] ; struct t_farm { int n_adj ; t_adj *adj ; } farms[_max_farm_]; int cows[_max_cow_], n_cow, n_farm, n_path; int sdis[_max_farm_], queue[_max_farm_], in_que[_max_farm_] ; void Bellman_Ford_SPFA( int source ){ memset( sdis, 0x7f, sizeof(int)*n_farm ); memset( in_que, 0, sizeof(int)*n_farm ) ; int *head=queue, *tail=queue; sdis[source]=0 , in_que[source]=1, *(tail++)=source ; do{ register int cfarm= *head ; in_que[cfarm]=0, head = (head-queue)==_max_farm_-1 ? queue : head+1; for( t_adj *p_adj=farms[cfarm].adj; p_adj!=farms[cfarm].adj+farms[cfarm].n_adj; p_adj++ ){ if( p_adj->dis + sdis[cfarm] >= sdis[p_adj->to] ) continue ; sdis[p_adj->to] = p_adj->dis + sdis[cfarm] ; if( !in_que[p_adj->to] ){ *tail=p_adj->to , in_que[p_adj->to]=1 ; tail = (tail-queue)==_max_farm_-1 ? queue : tail+1; } } }while( head!=tail ); } int main() { freopen("butter.in", "r", stdin); freopen("butter.out", "w", stdout); cin >> n_cow >> n_farm >> n_path ; for( int i=0; i<n_cow; i++ ) cin >> cows[i], cows[i]-- ; memset( farms, 0, sizeof(t_farm)*n_farm ) ; for( t_path *pc=pathes; pc!=pathes+n_path; pc++ ){ cin >> pc->v1 >> pc->v2 >> pc->dis ; farms[--pc->v1].n_adj++, farms[--pc->v2].n_adj++ ; } t_adj *p_adj=adj_buf; for( int i=0; i<n_farm; i++ ) farms[i].adj=p_adj, p_adj+=farms[i].n_adj, farms[i].n_adj=0 ; for( t_path *pc=pathes; pc!=pathes+n_path; pc++ ){ int v1=pc->v1, v2=pc->v2, dis=pc->dis ; farms[v1].adj[ farms[v1].n_adj++ ]=t_adj(v2,dis); farms[v2].adj[ farms[v2].n_adj++ ]=t_adj(v1,dis); } int min_total=0x7fffffff ; for( int i=0; i<n_farm; i++ ){ Bellman_Ford_SPFA(i); int total=0 ; for( int j=0; j<n_cow; j++ ) total += sdis[ cows[j] ] ; min_total = min_total>total ? total : min_total ; } cout << min_total << endl ; return 0; }
发表评论
-
USACO Training Section 4.2 Cowcycles
2010-01-31 21:11 883英文原题 中文题译 ... -
USACO Training Section 4.2 Job Processing
2010-01-30 17:31 1133英文原题 中文题译 大意: 有N个工件,每个工件要经 ... -
USACO Training Section 4.2 Drainage Ditches ISAP非递归和多路增广递归
2010-01-29 19:39 1849郁闷。不小心覆盖了重 ... -
USACO Training Section 4.2 The Perfect Stall 匈牙利算法的递归和非递归实现
2010-01-28 21:21 1645英文原题 中文题译 ... -
USACO Training Section 4.1 Cryptcowgraphy 奶牛密码
2010-01-27 20:58 1195英文原题 中文题译 大意: 奶牛们要从农场逃跑 ... -
USACO Training Section 4.1 Beef McNuggets
2010-01-26 21:37 968英文原题 中文题译 大意: 给定N个正整数, ... -
USACO Training Section 4.1 Fence Loops
2010-01-24 20:14 1064英文原题 大意: 农夫布朗的牧场上的篱笆已经失 ... -
USACO Training Section 3.4 Closed Fences
2010-01-23 17:50 1403英文原题 题意 一个 ... -
USACO Training Section 3.4 American Heritage
2010-01-21 23:19 771英文原题 大意:有一个由最多26个大写字母构成的二叉树 ... -
USACO Training Section 3.4 Raucous Rockers
2010-01-21 23:09 798英文原题 大意:有S首歌,要放到D个CD里。每首歌有一个 ... -
USACO Training Section 3.4 Electric Fence
2010-01-21 12:57 970英文原题 大意:给定一个三角形(0,0),(m,n),( ... -
USACO Training Section 3.3 Riding the Fences
2010-01-20 23:38 1206英文原题 中文题译 经典的求欧拉路径:给定最多两个奇 ... -
USACO Training Section 3.3 Shopping Offers
2010-01-19 22:18 915英文原题 中文题译 这是个与日常生活相关的题。商场促销 ... -
USACO Training Section 3.3 A Game
2010-01-19 20:54 1093英文原题 有如下一个双人游戏:N(2 <= N & ... -
USACO Training Section 3.3 Home on the Range
2010-01-19 19:36 772英文原题 中文题译 大意:给定一个01矩阵,计算其中全为 ... -
USACO Training Section 3.3 Camelot
2010-01-19 03:39 1231英文原题 中文题译 ... -
USACO Training Section 3.2 Magic Squares
2010-01-18 23:11 930英文原题 中文题译 大意:有人发明了一种有8个块三种变换 ... -
USACO Training Section 3.2 Feed Ratios
2010-01-18 20:52 1307英文原题 中文题译 大意:给出整数a[i][j]和 ... -
USACO Training Section 3.2 Spinning Wheels
2010-01-18 19:37 868英文原题 中文题译 大意:有五个纺车飞轮,每个都有最多五 ... -
USACO Training Section 3.2 Stringsobits
2010-01-18 01:04 990英文原题 中文题译 大意:求至多有L个1的第i个N位二进 ...
相关推荐
ACM----USACO Training(解题博客网),提供了USACO Training解题的代码,可以参考一下
自己写的usaco_training带代码。供参考,有一道题是cheat的。自己看吧。
usaco测试数据+标程 usaco的section1到section5的所有测试数据 以及标准程序
usaco解题报告,就是usaco.training.gateway上面的题目全解
USACO题解+代码+翻译,好东西,超级齐全,对大家帮助不小,特别是现在nocow挂了
usaco section2.3--section5.5源程序。。。。。。。。。。。。。。。。
[USACO 1.1.4]破碎的项链.cpp
3.2 Section 3.2 3.3 Section 3.3 3.4 Section 3.4 4 Chapter4 4.1 Section 4.1 4.2 Section 4.2 4.3 Section 4.3 4.4 Section 4.4 5 Chapter5 5.1 Section 5.1 5.2 Section 5.2 5.3 Section 5.3 5.4 Section 5.4 ...
USACO全部译题 USACO Training Program Gateway
USACO_培训USACO_培训Ride.java-> Gift1.java->
因为 10=2*5,所以每有一个 0 就有一对 2*5=10 出现,反之,如果这个数的质因数分解没有成对的 2,5,我们就可以简单的对 10 求模,而不用管前面
USACO training 教程翻译合集(清晰明确)
USACO培训页面美国计算机奥林匹克训练页2015年6月17日开始
USACO的第三章的题目sweet buter的代码,已经通过code blocks编译
One of the answer of the USACO training exercises.
usaco 合集,包括英文原题和中文译题,测试数据以及答案,很全啊!usaco 合集usaco 合集usaco 合集usaco 合集
usaco历年测试数据
usaco 2010-2011 nov news,喜欢usaco的朋友可以看看
某些USACO题目的答案,很详细,代码清晰结构良好,算法高效易于调试
USACO题集及答案