本题是多源最短路问题 但使用弗洛伊德算法会超时 而因为边数目比较少 所以用队列优化后的迪杰斯特拉算法可以通过

 #include<iostream>
 #include<cstring>
 #include<queue>
 #include<vector>
 using namespace std;
 <<;
 struct Edge{
     int from,to,dist;
 };
 struct Node{
     int u,d;
     bool operator<(const Node& n)const{
         return d>n.d;
     }
 };
 vector<Edge> edges;
 +],cnt[+],v[+];
 int N,M,C;
 vector<+];
 void DJSTL(int s){
     ;i<=N;i++) d[i]=inf,v[i]=;
     d[s]=;
     priority_queue<Node> Q;
     Q.push((Node){s,});
     while(!Q.empty()){
         Node x=Q.top();Q.pop();
         int u=x.u;
         v[u]=;
         ;i<G[u].size();i++){
             Edge& e=edges[G[u][i]];
             if(v[e.to]) continue;
             if(d[u]+e.dist<d[e.to]){
                 d[e.to]=d[u]+e.dist;
                 Q.push((Node){e.to,d[e.to]});
             }
         }
     }

 }
 int main()
 {
     memset(cnt,,sizeof(cnt));
     int cow,from,to,dist;
     cin>>C>>N>>M;
     while(C--){
         cin>>cow;
         cnt[cow]++;
     }
     while(M--){
         cin>>from>>to>>dist;
         edges.push_back((Edge){from,to,dist});
         edges.push_back((Edge){to,from,dist});
         int m=edges.size();
         G[);
         G[to].push_back(m-);
     }
     int ans=inf;
     ;i<=N;i++){
         ;
         DJSTL(i);
         ;i<=N;i++) tans+=d[i]*cnt[i];
         ans=min(ans,tans);
     }
     cout<<ans;
     ;
 }

USACO Section 3.2 香甜的黄油 Sweet Butter的更多相关文章

  1. 洛谷P1828 香甜的黄油 Sweet Butter

    P1828 香甜的黄油 Sweet Butter 241通过 724提交 题目提供者JOHNKRAM 标签USACO 难度普及+/提高 提交  讨论  题解 最新讨论 我的SPFA为什么TLE.. 为 ...

  2. 【香甜的黄油 Sweet Butter】

    [香甜的黄油 Sweet Butter] 洛谷P1828 https://www.luogu.org/problemnew/show/P1828 JDOJ 1803 https://neooj.com ...

  3. P1828 香甜的黄油 Sweet Butter 最短路 寻找一个点使得所有点到它的距离之和最小

    P1828 香甜的黄油 Sweet Butter 闲来无事 写了三种最短路(那个Floyed是不过的) 题目描述 农夫John发现做出全威斯康辛州最甜的黄油的方法:糖.把糖放在一片牧场上,他知道N(1 ...

  4. [最短路]P1828 香甜的黄油 Sweet Butter

    题目描述 农夫John发现做出全威斯康辛州最甜的黄油的方法:糖.把糖放在一片牧场上,他知道N(1<=N<=500)只奶牛会过来舔它,这样就能做出能卖好价钱的超甜黄油.当然,他将付出额外的费 ...

  5. P1828 香甜的黄油 Sweet Butter

    对于这道洛谷ac而我整了一下午的codevs的题,我也是很绝望啊. 原因是队列数组开小了我勒个去???我说STL怎么能过 题目描述 农夫John发现做出全威斯康辛州最甜的黄油的方法:糖.把糖放在一片牧 ...

  6. [LUOGU] P1828 香甜的黄油 Sweet Butter

    题目描述 农夫John发现做出全威斯康辛州最甜的黄油的方法:糖.把糖放在一片牧场上,他知道N(1<=N<=500)只奶牛会过来舔它,这样就能做出能卖好价钱的超甜黄油.当然,他将付出额外的费 ...

  7. P1828 香甜的黄油 Sweet Butter (spfa)

    题目描述 农夫John发现做出全威斯康辛州最甜的黄油的方法:糖.把糖放在一片牧场上,他知道N(1<=N<=500)只奶牛会过来舔它,这样就能做出能卖好价钱的超甜黄油.当然,他将付出额外的费 ...

  8. P1828 [USACO3.2]香甜的黄油 Sweet Butter

    题目描述 农夫$John$发现做出全威斯康辛州最甜的黄油的方法:糖.把糖放在一片牧场上,他知道$N(1\leqslant N\leqslant 500)$只奶牛会过来舔它,这样就能做出能卖好价钱的超甜 ...

  9. 香甜的黄油 Sweet Butter

    原题链接:https://www.luogu.org/problem/show?pid=1828#sub 经典的最短路问题. 各位不要被题目条件迷惑了,牧场想象成点,道路想象成边,奶牛所在的位置想象成 ...

随机推荐

  1. Highcharts 功能强大、开源、美观、图表丰富、兼容绝大多数浏览器的纯js图表库

    http://www.hcharts.cn/index.php 暂无介绍,等待后续补充

  2. [linux basic]基础--信号

    线程->信号信号,是unix和linux系统响应某些条件而产生的一个事件.接收到该信号的进程会相应地采取一些行动.raise生成表示一个信号的产生catch捕获表示接受到一个信号的产生:信号是由 ...

  3. 3. redis的超时,事务,watch

    一. 键的生存时间 expire设置一个已经存在的key的生存时间,到时间后redis自动删除这个 命令 expire 设置生存时间(单位/秒) pexpire设置生存时间(单位/毫秒) ttl/pt ...

  4. (C# Debug)A first chance exception of type 'System.ArgumentException' occurred in System.Data.dll

    Debug 模式下运行程序的时候,Output 窗口出来个错误“A first chance exception of type 'System.ArgumentException' occurred ...

  5. HDU 1671 Phone List (Trie·数组实现)

    链接:http://blog.csdn.net/acvay/article/details/47089657 题意  给你一组电话号码  判断其中是否有某个电话是另一个电话的前缀 字典树的基础应用   ...

  6. protobuf使用说明

    1..proto文件为要生成.java文件的模板文件,其中包含名称空间.文件名等信息2.cmd中进入当前目录D:\JAVA\protoc-2.5.0-win323.运行 protoc.exe --ja ...

  7. 如何使用javadoc

    package com.frank.chapter1; // object.Documentation1.java // TIJ4 Chapter Object, Exercise 13 - 1 /* ...

  8. code md5

    using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptograph ...

  9. log4Net使用 z

    http://logging.apache.org/log4net/1.Log4net主要结构分析  1.1 Logger:是应用程序与Log4net进行交互的主要模块,它也是log4net生成log ...

  10. 利用 Gitbook 生成文档中心站点

    经过一个多月,Bugtags 最近上线了自己的文档站点:docs.bugtags.com,在这里你可以找到 Bugtags 集成.使用相关的绝大部分问题. 在这之前我们使用的是第三方提供的帮助中心产品 ...