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 ...
随机推荐
- Careercup - Microsoft面试题 - 6543214668414976
2014-05-11 02:56 题目链接 原题: Write a function called FooBar that takes input integer n and prints all t ...
- 【Count and Say】cpp
题目: The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111 ...
- Django RequestContext用法
模版中的变量由context中的值来替换,如果在多个页面模版中含有相同的变量,比如:每个页面都需要{{user}},笨办法就是在每个页面的请求视图中都把user放到context中. from d ...
- python 可变参数
原文地址:http://docs.pythontab.com/python/python3.4/controlflow.html#tut-functions 一个最不常用的选择是可以让函数调用可变个数 ...
- Ubuntu 部署 Redmine
我是在虚拟机种安装的Ubuntu,原因是装双系统太麻烦,虚拟机有问题的话删除容易. 首选保证主机中能ping 通虚拟机.我是用的桥接. 1.ubuntu安装必备的软件: sudo apt-get in ...
- jQuery(Keep for myself)
jQuery API : http://www.w3cschool.cc/manual/jquery/ 1. jQuery是一个JavaScript函数库. jQuery是一个轻量级的"写的 ...
- Net数值计算MathNet.Numerics类库
一.Net自带的数值计算:System.Numerics 1.大整数BitInteger 方法:除数和余数.最大公约数 2.复数Complex 属性:实部.虚部.量值.相位 方法:共轭.倒数 二.Ma ...
- GS玩家登录
玩家上线 这个过程看了很多很多次了,这里在看下 客户端打开,服务器收到libevent事件,然后new Channel这个过程都付给他各种指针,然后放到channel容器中 .客户端发送c2s_log ...
- On Explainability of Deep Neural Networks
On Explainability of Deep Neural Networks « Learning F# Functional Data Structures and Algorithms is ...
- ios 设计软件
briefs V1.0.5 download @ here:http://soft.macx.cn/5442.htm 密码:www.macx.cn