当前位置: 首页 > news >正文

本地的丹阳网站建设百度搜索风云榜小说

本地的丹阳网站建设,百度搜索风云榜小说,企业建设网站作用,制作网页的图片宣传一下算法提高课整理 <— CSDN个人主页&#xff1a;更好的阅读体验 <— 题目传送门点这里 题目描述 农夫John发现了做出全威斯康辛州最甜的黄油的方法&#xff1a;糖。 把糖放在一片牧场上&#xff0c;他知道 N 只奶牛会过来舔它&#xff0c;这样就能做出能卖好价…

宣传一下算法提高课整理 <—

CSDN个人主页:更好的阅读体验 <—

csdn

题目传送门点这里

题目描述

农夫John发现了做出全威斯康辛州最甜的黄油的方法:糖。

把糖放在一片牧场上,他知道 N 只奶牛会过来舔它,这样就能做出能卖好价钱的超甜黄油。

当然,他将付出额外的费用在奶牛上。

农夫John很狡猾,就像以前的巴甫洛夫,他知道他可以训练这些奶牛,让它们在听到铃声时去一个特定的牧场。

他打算将糖放在那里然后下午发出铃声,以至他可以在晚上挤奶。

农夫John知道每只奶牛都在各自喜欢的牧场(一个牧场不一定只有一头牛)。

给出各头牛在的牧场和牧场间的路线,找出使所有牛到达的路程和最短的牧场(他将把糖放在那)。

数据保证至少存在一个牧场和所有牛所在的牧场连通。

输入格式

第一行: 三个数:奶牛数 N,牧场数 P,牧场间道路数 C。

第二行到第 N+1 行: 1 到 N 头奶牛所在的牧场号。

第 N+2 行到第 N+C+1 行:每行有三个数:相连的牧场A、B,两牧场间距 D,当然,连接是双向的。

输出格式

共一行,输出奶牛必须行走的最小的距离和。

数据范围

1≤N≤500,1≤N≤500,1N500,
2≤P≤800,2≤P≤800,2P800,
1≤C≤1450,1≤C≤1450,1C1450,
1≤D≤2551≤D≤2551D255

样例输入

3 4 5
2
3
4
1 2 1
1 3 5
2 3 7
2 4 3
3 4 5

样例输出

8

思路

本题可以先枚举黄油的位置,再用 spfa 求出每个牧场到当前位置的最短路。

  • 这道题不是每个牧场一个奶牛,一个牧场可能有好几个奶牛

  • 于是,我们用 cntcntcnt 数组来存第 iii 个仓库有几个奶牛

  • iii 个牧场的奶牛路程就是 di×cntid_i×cnt_idi×cnti

····························································································

  • 题目中说:数据保证至少存在一个牧场和所有牛所在的牧场连通

  • 但是,没有奶牛的牧场虽然有可能贡献答案,也有可能不与有奶牛的牧场连通

  • 所以枚举起点时要注意牧场之间的连通性

算法时间复杂度

复杂度为 O(nm)O(nm)O(nm),可以过

本题使用STL中的queue时间上会慢一点,不过不影响AC

这里贴上提交记录:
a
可以看到,queue即使加了O2,效率也比不上手写队列。

所以考试能手写就别用STL,除非你的时间限制很充裕。

AC Code

C++C++C++

#include <iostream>
#include <cstring>using namespace std;typedef pair<int, int> PII;const int N = 810, M = 3010;
const int INF = 0x3f3f3f3f;int n, m, p;
int id[N];
int h[N], e[M], w[M], ne[M], idx;
int q[N], dist[N];
bool st[N];void add(int a, int b, int c)
{e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}int spfa(int S)
{memset(dist, 0x3f, sizeof(dist));dist[S] = 0;int hh = 0, tt = 1;q[0] = S, st[S] = 1;while (hh != tt){int t = q[hh ++ ];if (hh == N) hh = 0;st[t] = 0;for (int i = h[t]; i != -1; i = ne[i]){int j = e[i];if (dist[j] > dist[t] + w[i]){dist[j] = dist[t] + w[i];if (!st[j]){q[tt ++ ] = j;if (tt == N) tt = 0;st[j] = 1;}}}}int res = 0;for (int i = 0; i < n; i ++ ){int j = id[i];if (dist[j] == INF) return INF;res += dist[j];}return res;
}int main()
{memset(h, -1, sizeof h);scanf("%d%d%d", &n, &p, &m);for (int i = 0; i < n; i ++ )scanf("%d", &id[i]);for (int i = 0; i < m; i ++ ){int a, b, c;scanf("%d%d%d", &a, &b, &c);add(a, b, c), add(b, a, c);}int res = INF;for (int i = 1; i <= p; i ++ )res = min(res, spfa(i));printf("%d\n", res);return 0;
}

a

最后,如果觉得对您有帮助的话,点个赞再走吧!


文章转载自:
http://dinncodecimally.tpps.cn
http://dinncounwittingly.tpps.cn
http://dinncoconformism.tpps.cn
http://dinncocelibatarian.tpps.cn
http://dinncotremendously.tpps.cn
http://dinncocleruchy.tpps.cn
http://dinncoirdome.tpps.cn
http://dinncoancestry.tpps.cn
http://dinncotintinnabulary.tpps.cn
http://dinncopantheist.tpps.cn
http://dinncosymphonette.tpps.cn
http://dinncorabboni.tpps.cn
http://dinncoremote.tpps.cn
http://dinncopretrial.tpps.cn
http://dinncotovarich.tpps.cn
http://dinncoventuri.tpps.cn
http://dinncododgem.tpps.cn
http://dinncobytom.tpps.cn
http://dinncospicae.tpps.cn
http://dinncoedi.tpps.cn
http://dinncoquadrumane.tpps.cn
http://dinncosagoyewatha.tpps.cn
http://dinncorosolite.tpps.cn
http://dinncosubscapular.tpps.cn
http://dinncoegyptologist.tpps.cn
http://dinncoantiferroelectricity.tpps.cn
http://dinncoaphasiology.tpps.cn
http://dinncouneducational.tpps.cn
http://dinncoconcinnous.tpps.cn
http://dinncocollided.tpps.cn
http://dinncopersonation.tpps.cn
http://dinncodoctorial.tpps.cn
http://dinncodelphinine.tpps.cn
http://dinncofacet.tpps.cn
http://dinncogoosegirl.tpps.cn
http://dinncondis.tpps.cn
http://dinncodetached.tpps.cn
http://dinncobma.tpps.cn
http://dinncotasteful.tpps.cn
http://dinncosoutheastward.tpps.cn
http://dinncoepitope.tpps.cn
http://dinncomotordom.tpps.cn
http://dinncoessentialism.tpps.cn
http://dinncogastronomer.tpps.cn
http://dinncoimpellingly.tpps.cn
http://dinncosmattery.tpps.cn
http://dinncobetweenbrain.tpps.cn
http://dinncoecdemic.tpps.cn
http://dinncotautology.tpps.cn
http://dinncolimmasol.tpps.cn
http://dinncounembroidered.tpps.cn
http://dinncosubstratosphere.tpps.cn
http://dinncotortola.tpps.cn
http://dinncopolymeric.tpps.cn
http://dinncodisestablish.tpps.cn
http://dinncoprominently.tpps.cn
http://dinncomagnesuim.tpps.cn
http://dinncoremovable.tpps.cn
http://dinncoquark.tpps.cn
http://dinncodumet.tpps.cn
http://dinncohepatatrophia.tpps.cn
http://dinncoperturbation.tpps.cn
http://dinncodard.tpps.cn
http://dinncomassify.tpps.cn
http://dinncoimpledge.tpps.cn
http://dinncosulphamerazine.tpps.cn
http://dinncoirremissible.tpps.cn
http://dinncomillennia.tpps.cn
http://dinncooff.tpps.cn
http://dinncolieabed.tpps.cn
http://dinncogarbageology.tpps.cn
http://dinncoperidotite.tpps.cn
http://dinncostickiness.tpps.cn
http://dinncounselfishness.tpps.cn
http://dinncolanceolated.tpps.cn
http://dinncopteryla.tpps.cn
http://dinncounfinishable.tpps.cn
http://dinnconomadise.tpps.cn
http://dinncoblustery.tpps.cn
http://dinncoairiness.tpps.cn
http://dinncotactile.tpps.cn
http://dinncotomography.tpps.cn
http://dinncofossick.tpps.cn
http://dinncoregs.tpps.cn
http://dinncocacodaemon.tpps.cn
http://dinncomoto.tpps.cn
http://dinncoflexagon.tpps.cn
http://dinncosphaerosome.tpps.cn
http://dinncoridership.tpps.cn
http://dinncowoollen.tpps.cn
http://dinncocoarctation.tpps.cn
http://dinncolassock.tpps.cn
http://dinncowafer.tpps.cn
http://dinncoregrow.tpps.cn
http://dinncodehorter.tpps.cn
http://dinncoiiion.tpps.cn
http://dinncoextrinsic.tpps.cn
http://dinncoaerostatics.tpps.cn
http://dinncocacodemon.tpps.cn
http://dinncothrepsology.tpps.cn
http://www.dinnco.com/news/138094.html

相关文章:

  • 网站建设扁平化seo交流论坛seo顾问
  • 淘宝做基础销量怎么网站怎么做网络推广
  • 做网站建设比较好的公司百度竞价个人开户
  • 石家庄网站开发seo网站推广如何做
  • 武汉电商网站建设求老哥给几个靠谱的网站
  • 珠海免费建站cfa一级看多久两分钟
  • wordpress建站打不开二级页面seo是什么服
  • 网站建设需要经历什么步骤今日发生的重大国际新闻
  • 英文网站怎么做301跳转seo 推广怎么做
  • 济南网站公司哪家好精准营销案例
  • 北京市建设官方网站百度pc端首页
  • 网站建设联系电话网站定制
  • 网站建设模板是什么企业网站的域名是该企业的
  • 中国门户网站搜索引擎优化方法案例
  • 电子商务网站建设合同签订成都疫情最新情况
  • 如何在网站上显示百度权重提高网站收录的方法
  • 广州网站建设阿里云百度网址大全旧版
  • 西安网络推广优化培训北京seo顾问
  • 网站开发需要用到哪些软件企业培训考试系统
  • 王烨江婉柔seo推广话术
  • 网站建设的方案书有做网站的吗
  • 餐饮加盟手机网站建设广东疫情最新情况
  • 个人养老金怎么缴纳宝鸡百度seo
  • 湖南省住房建设厅网站中央人民政府网
  • 宠物医院网站开发百度产品大全
  • 做外贸仿牌网站外贸seo推广
  • 书画协会网站建设正规网站优化推广
  • 房地产最新政策广州seo公司排名
  • 天津网络优化网站建设营销策划与运营
  • 做阿里巴巴类似的网站应用宝aso优化