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 ...
随机推荐
- 向Array中添加二分插入排序
二分插入排序思路 先在有序区通过二分查找的方法找到移动元素的起始位置,然后通过这个起始位置将后面所有的元素后移. 二分插入排序实现 Function.prototype.method = functi ...
- ueditor:原谅我这一生不羁放纵爱独特
客户指明道姓需要使用百度编辑器,好吧,虽然自从李彦宏把一个好好的千千静听搞得节操尽碎之后,我就对百度的东西毫无好感,但是客户是上帝嘛,不就一个文本编辑器嘛,弄之,始料未及的是,就是这样一个简单的文本编 ...
- 【Sort Colors】cpp
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 转载------------------关于android的一些技巧
Android eclipse中程序调试 一:断点调试 用eclipse开发android程序的时,跟VS一样是可以断点单步调试的.步骤如下.1 设置断点:在编码窗体的左边框上用鼠标双击,或者右键点击 ...
- 小技巧--字符串输入从a[1]开始
char a[100],b[100]; cin>>a>>(b+1);//cin: abcd abcd cout<<a[1]<<endl<<b ...
- 以前用过Extjs技术的开发人员在学习Extjs4时需要注意的问题
以前学习过Extjs的同学,在学习Extjs4的时候需要注意几个关键改变: 1.Extjs4的新的类系统. 2.Extjs4中MVC思路 3.Extjs4中的新的命名规范(结合新的MV ...
- maven ArtifactTransferException: Failure to transfer
我在使用Myeclipse碰见这个错误,我没有用伺服,直接连接到maven中心. ArtifactTransferException: Failure to transfer org.apache.h ...
- if-else的优化举例
共有部分: String bookFrom = null; String sheetFrom = null; String bookTo = null; String sheetTo = null; ...
- jetty启动报错Unsupported major.minor version 51.0
主要是JDK版本的问题,需要将Eclipse的Jdk版本设置为1.7的才可以,编译级别也设置为1.7,然后删除maven项目路径,D:\WORK\workspace\xxx\target下的所有文件, ...
- uiview 单边圆角或者单边框
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(120, 10, 80, 80)]; view2.backgroundColor = ...