USACO 香甜的黄油
来源:互联网 发布:英雄联盟网络连接失败 编辑:程序博客网 时间:2024/04/30 02:38
题目描述 Description
农夫John发现做出全威斯康辛州最甜的黄油的方法:糖。把糖放在一片牧场上,他知道N(1<=N<=500)只奶牛会过来舔它,这样就能做出能卖好价钱的超甜黄油。当然,他将付出额外的费用在奶牛上。
农夫John很狡猾。像以前的Pavlov,他知道他可以训练这些奶牛,让它们在听到铃声时去一个特定的牧场。他打算将糖放在那里然后下午发出铃声,以至他可以在晚上挤奶。
农夫John知道每只奶牛都在各自喜欢的牧场(一个牧场不一定只有一头牛)。给出各头牛在的牧场和牧场间的路线,找出使所有牛到达的路程和最短的牧场(他将把糖放在那)。
输入格式:
第一行:三个数:奶牛数N,牧场数(2<=P<=800),牧场间道路数C(1<=C<=1450)
第二行到第N+1行: 1到N头奶牛所在的牧场号
第N+2行到第N+C+1行: 每行有三个数:相连的牧场A、B,两牧场间距离D(1<=D<=255),当然,连接是双向的。
输出格式:
一行 输出奶牛必须行走的最小的距离和。
输入样例:
3 4 52341 2 11 3 52 3 72 4 33 4 5
输出样例:
8
这题乍一看好像是树的重心什么的乱搞,然而掐指一算数据范围发现原来可以每个点都跑一遍spfa 233,于是就变成了水题qwq。
#include<iostream>#include<cstdio>#include<deque>using namespace std;const int size = 200010;int head[size],next[size],dist[size];bool use[size];struct dc{ int t,d;}l[size];int tot = 1;int n,m,p;void build(int f,int t,int d){ l[tot].t = t; l[tot].d = d; next[tot] = head[f]; head[f] = tot ++;}int num[size];deque < int > q;void spfa(int s){ for(int i = 1 ; i <= p ; i ++) dist[i] = 214748364; dist[s] = 0; use[s] = 1; q.push_back(s); while(!q.empty()) { int f = q.front(); q.pop_front(); use[f] = 0; for(int i = head[f] ; i ; i = next[i]) { int t = l[i].t; if(dist[t] > dist[f] + l[i].d) { dist[t] = dist[f] + l[i].d; if(!use[t]) { use[t] = 1; if(!q.empty()) { if(dist[t] < dist[q.front()]) q.push_front(t);// else if(t > q.front())// q.push_front(t); else q.push_back(t); } else q.push_back(t); } } } }}int main(){ scanf("%d%d%d",&n,&p,&m); for(int i = 1 ; i <= n ; i ++) { int pos; scanf("%d",&pos); num[pos] ++; } for(int i = 1 ; i <= m ; i ++) { int f,t,d; scanf("%d%d%d",&f,&t,&d); build(f,t,d); build(t,f,d); } int ans = 214748364; for(int i = 1 ; i <= p ; i ++) { spfa(i); int tot = 0; for(int j = 1 ; j <= p ; j ++) { tot += dist[j]*num[j]; } ans = min(ans,tot); } cout<<ans; return 0;}
0 0
- USACO 香甜的黄油
- 题解:[USACO 3.2] 香甜的黄油
- (1070): 【USACO TRAINING】香甜的黄油
- USACO 3.2.6 香甜的黄油
- [Usaco-3.2.6] Sweet Butter香甜的黄油
- 【USACO题库】3.2.6 Sweet Butter香甜的黄油
- USACO 3.2 Sweet Butter 香甜的黄油 (Bellman DIJ SPFA)
- JZOJ1287.【USACO题库】3.2.6 Sweet Butter香甜的黄油
- 【USACO题库】3.2.6 Sweet Butter香甜的黄油
- 香甜的黄油
- 香甜的黄油
- 香甜的黄油
- 香甜的黄油
- 【9008】香甜的黄油
- [spfa]香甜的黄油
- Codevs2038香甜的黄油题解
- 洛谷1828 香甜的黄油
- 【USACO3.2.6】香甜的黄油
- STC15F2KA60S2芯片粉尘传感器的应用
- mac os 升级到10.11后,Xcode问题
- ActiveX控件开发小结
- 认识JS
- 最简实例说明wait、notify、notifyAll的使用方法
- USACO 香甜的黄油
- Python多线程学习
- C++标准库头文件
- javascript条件语句
- 关于STC15F2K60S2看门狗的应用
- js快速判断IE浏览器(兼容IE10与IE11)
- Ubuntu下关于crontab的使用详解
- STC15F2K60S2内E2PROM应用
- SQL语言的应用