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

 #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. 重复ID的记录,只显示其中1条

    --重复ID的记录,只显示其中1条 --生成原始表 select * into #tempTable from ( select '1' as id ,'a' as name union all se ...

  2. 105. Construct Binary Tree from Preorder and Inorder Traversal

    Given preorder and inorder traversal of a tree, construct the binary tree. ============== 基本功: 利用前序和 ...

  3. Android四:sqllite

    1.扩展类SQLiteOpenHelper xxSQLiteHelper extends SQLiteOpenHelper public xxSQLiteHelper(Context context, ...

  4. iphone Dev 开发实例9:Create Grid Layout Using UICollectionView in iOS 6

    In this tutorial, we will build a simple app to display a collection of recipe photos in grid layout ...

  5. [物理学与PDEs]第2章习题参考解答

    [物理学与PDEs]第2章习题1 无旋时的 Euler 方程 [物理学与PDEs]第2章习题2 质量力有势时的能量方程 [物理学与PDEs]第2章习题3 Laplace 方程的 Neumann 问题 ...

  6. [物理学与PDEs]书中一些对数学研究有用的引理

    P 35--38 1.  若 ${\bf B}$ 为横场 ($\Div{\bf B}=0\ra {\bf k}\cdot {\bf B}=0\ra $ 波的振动方向与传播方向平行), 则 $$\bex ...

  7. js方法收藏

    1.验证非负数字 //onfocusout="checkQty(this);" function checkQty(obj) { //排除0开头的非法输入 if (obj.valu ...

  8. JAVA 数组实例-求学生成绩的最大成绩,获取数组中的最大值、最小值

    实例: import java.util.*; //求学生最大成绩 public class Test{ public static void main(String[] args){ System. ...

  9. webuploader在同一个页面支持多个按钮实例

    之前在时候用到webuploader ,起初是支持单实例,后来要求支持多实例. webuploder API网址,如果不懂我说的可以去查看http://fex.baidu.com/webuploade ...

  10. mysql索引之四(索引使用注意规则:索引失效--存在索引但不使用索引)

    但是如果是同样的sql如果在之前能够使用到索引,那么现在使用不到索引,以下几种主要情况: 1. 随着表的增长,where条件出来的数据太多,大于15%,使得索引失效(会导致CBO计算走索引花费大于走全 ...