USACO Section 3.2: Sweet Butter
这题我自己是用邻接矩阵+dijskstra方法来求的,第九个例子TLE。网上看了别人的代码,是用邻接表+BFS来完成。
这里可以学到两个小技巧,邻接表的表示方法和INT_MAX的表示方法。
/* ID:yingzho1 LANG:C++ TASK:butter */ #include<fstream> #include<cstring> #include<queue> #include <limits> using namespace std; ifstream fin("butter.in"); ofstream fout("butter.out"); ; struct vertex { int end,len; }; vertex adj[MAX][MAX]; }, cowpos[]={}, n, p, c; }; int distances[MAX]; int search(int start) { memset(pushed, , sizeof(pushed)); ; k <= p; k++) distances[k] = numeric_limits<int>::max(); queue<int> Q; Q.push(start); pushed[start] = true; distances[start] = ; while(!Q.empty()) { int x = Q.front(); Q.pop(); pushed[x] = false; ; j < cnt[x]; j++) { if(distances[x]+adj[x][j].len < distances[adj[x][j].end]) { distances[adj[x][j].end] = distances[x]+adj[x][j].len; if(!pushed[adj[x][j].end]) { Q.push(adj[x][j].end); pushed[adj[x][j].end] = true; } } } } ; ; i<=n; i++) { ; else ans+=distances[cowpos[i]]; } return ans; } int main() { memset(cnt, , sizeof(cnt)); fin>>n>>p>>c; ; i<=n; i++) fin>>cowpos[i]; , s, t, value; i <= c; i++) { fin>>s>>t>>value; adj[s][cnt[s]].end = t; adj[s][cnt[s]].len = value; cnt[s]++; adj[t][cnt[t]].end = s; adj[t][cnt[t]].len = value; cnt[t]++; } int res, mins = numeric_limits<int>::max(); ; i <= p; i++) { res = search(i); ) mins = res; } fout<<mins<<endl; ; }
USACO Section 3.2: Sweet Butter的更多相关文章
- 【USACO 3.2】Sweet Butter(最短路)
题意 一个联通图里给定若干个点,求他们到某点距离之和的最小值. 题解 枚举到的某点,然后优先队列优化的dijkstra求最短路,把给定的点到其的最短路加起来,更新最小值.复杂度是\(O(NElogE) ...
- USACO 3.2.6 Sweet Butter 香甜的黄油(最短路)
Description 农夫John发现做出全威斯康辛州最甜的黄油的方法:糖.把糖放在一片牧场上,他知道N(1<=N<=500)只奶牛会过来舔它,这样就能做出能卖好价钱的超甜黄油.当然,他 ...
- 洛谷P1828 香甜的黄油 Sweet Butter
P1828 香甜的黄油 Sweet Butter 241通过 724提交 题目提供者JOHNKRAM 标签USACO 难度普及+/提高 提交 讨论 题解 最新讨论 我的SPFA为什么TLE.. 为 ...
- Sweet Butter 香甜的黄油
Sweet Butter 香甜的黄油 题目大意:m个点,n头奶牛,p条边,每一头奶牛在一个点上,一个点可以有多只奶牛,求这样一个点,使得所有奶牛到这个点的距离之和最小. 注释:n<=500 , ...
- 【香甜的黄油 Sweet Butter】
[香甜的黄油 Sweet Butter] 洛谷P1828 https://www.luogu.org/problemnew/show/P1828 JDOJ 1803 https://neooj.com ...
- P1828 香甜的黄油 Sweet Butter 最短路 寻找一个点使得所有点到它的距离之和最小
P1828 香甜的黄油 Sweet Butter 闲来无事 写了三种最短路(那个Floyed是不过的) 题目描述 农夫John发现做出全威斯康辛州最甜的黄油的方法:糖.把糖放在一片牧场上,他知道N(1 ...
- USACO Section 3.2 香甜的黄油 Sweet Butter
本题是多源最短路问题 但使用弗洛伊德算法会超时 而因为边数目比较少 所以用队列优化后的迪杰斯特拉算法可以通过 #include<iostream> #include<cstring& ...
- USACO Section 1.3 题解 (洛谷OJ P1209 P1444 P3650 P2693)
usaco ch1.4 sort(d , d + c, [](int a, int b) -> bool { return a > b; }); 生成与过滤 generator&& ...
- USACO Section 3.3: Riding the Fences
典型的找欧拉路径的题.先贴下USACO上找欧拉路径的法子: Pick a starting node and recurse on that node. At each step: If the no ...
随机推荐
- CentOS6.5 安装JDK1.7详细步骤参考
一般情况下,我们都要将linux自带的OPENJDK卸载掉,然后安装SUN的JDK. 首先查看Linux自带的JDK是否已安装. 输入如下命令,查看已经安装的JAVA版本信息. 输入如下命令,查看JD ...
- android开发 缩放到指定比例的尺寸
一种通过matrix矩阵缩放: //使用Bitmap加Matrix来缩放 public static Drawable resizeImage(Bitmap bitmap, int w, int h) ...
- Netsharp FAQ
1.启动服务器时候不能监听端口,出现如下错误: 回答: 这种问题应该是在win7环境下才有,是没有权限,要以管理员身份运行Netsharp.Elephant.Q.exe. 2.启动服务器的时候,服务端 ...
- HDU 5446 Unknown Treasure Lucas+中国剩余定理
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 Unknown Treasure 问题描述 On the way to the next se ...
- linux 操作
正在运行的内核和系统信息 # uname -a # 获取内核版本(和BSD版本) # lsb_release -a # 显示任何 LSB 发行版版本信息 # cat /etc/SuSE-release ...
- idea新建项目完整过程
参看下面博客 http://www.cnblogs.com/cnjava/archive/2013/01/29/2881654.html 突然,感觉idea其实挺麻烦的: 一.junit test做起 ...
- WPF 与Surface 2.0 SDK 亲密接触 - ScatterView 数据绑定篇
与我们常用的一些WPF 控件相同,ScatterView 控件也支持数据绑定功能.本篇将演示如何利用ScatterView 绑定Win7 系统中的样例图片,并且每张图片会以独立的ScatterView ...
- uialertview 改变文字显示位置
- (void)willPresentAlertView:(UIAlertView *)alertView{ UIView * view = [alertView.subviews objectAtI ...
- win8系统输入法设置
Windows 8系统自带微软拼音简捷输入法,无论是在Windows的开始屏幕新界面中还是Windows传统桌面里,按Shift键或者直接点击屏幕上的"中/英"标识即可切换中英文输 ...
- 3[doses] ------一种诡异的写法
在 head first c 的第60页,有这么一道题: 一个富翁因为服药过度而死亡. 下面是自动服药器的代码: #include <stdio.h> int main(void) { , ...