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 ...
随机推荐
- php图形图像处理之生成验证码
\(^o^)/~ 现在网上越来越离不开验证码了,不知道小伙伴们知不知利用php的GD库就可以生成验证码,Σ(⊙▽⊙"a ...... 首先介绍几个需要用的函数. 1.imagesetpixe ...
- 解决a different object with the same identifier value was already associated with the session错误
[转]解决a different object with the same identifier value was already associated with the session错误 这个错 ...
- C++ 11 新特性
C++11新特性: 1.auto 2.nullptr 3.for 4.lambda表达式 5.override ...
- 【BZOJ】【1552】【Cerc2007】robotic sort / 【3506】【CQOI2014】排序机械臂
Splay 离散化+Splay维护序列…… 好吧主要说一下我做这道题遇到的几个错误点: 1.离散化 2.由于找到的这个数的位置一定是大于等于 i 的,所以其实在把它splay到根以后,i 结点只能sp ...
- Application, JDBC, 数据库连接池, Session, 数据库的关系
RT,这几个东东已经困扰我很长一段时间了... 这次争取把她们理清楚了! 参考资料: 1. 数据库连接池:http://www.cnblogs.com/shipengzhi/archive/2011/ ...
- 用HAProxy和KeepAlived构建高可用的反向代理
用HAProxy和KeepAlived构建高可用的反向代理 用HAProxy和KeepAlived构建高可用的反向代理 前言对于访问量较大的网站来说,随着流量的增加单台服务器已经无法处理所有的请求 ...
- 疯狂java讲义——继承
本文章只是记录我在学习疯狂java讲义里面,对之前java知识查缺补漏进行的总结. 方法重写 方法重写要遵循"两同两小一大"规则."两同"即方法名相同.形参列表 ...
- Firefly卡牌手游《暗黑世界V1.5》服务器端源码+GM管理后台源码
http://www.9miao.com/content-6-304.html Firefly卡牌手游<暗黑世界V1.5>服务器端源码+GM管理后台源码 关于<暗黑世界V1.5> ...
- 关于c语言中的字符数组和字符串指针
先看代码: #include <stdio.h> int main(void) { ] = "; char * strTmp = "abcdefg"; int ...
- POJ 1503 Integer Inquiry(大数相加,java)
题目 我要开始练习一些java的简单编程了^v^ import java.io.*; import java.util.*; import java.math.*; public class Main ...