zoukankan      html  css  js  c++  java
  • codevs 2038 香甜的黄油 USACO

     时间限制: 1 s
     空间限制: 128000 KB
     题目等级 : 钻石 Diamond
    题目描述 Description

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

    农夫John很狡猾。他知道他可以训练这些奶牛,让它们在听到铃声时去一个特定的牧场。他打算将糖放在那里然后下午发出铃声,以至他可以在晚上挤奶。

    农夫John知道每只奶牛都在各自喜欢的牧场呆着(一个牧场不一定只有一头牛)。给出各头牛在的牧场和牧场间的路线,找出使所有牛到达的路程和最短的牧场(他将把糖放在那)。

    输入描述 Input Description

    第一行: 三个数:奶牛数N,牧场数P(2<=P<=800),牧场间道路数C(1<=C<=1450).

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

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

    输出描述 Output Description

    一行 输出奶牛必须行走的最小的距离和.

    样例输入 Sample Input
    3 4 5
    2
    3
    4
    1 2 1
    1 3 5
    2 3 7
    2 4 3
    3 4 5
    样例图形
             P2  
    P1 @--1--@ C1
            |
            | 
          5  7  3
            |   
            |     C3
          C2 @--5--@
             P3    P4
    样例输出 Sample Output
    8
    {说明: 放在4号牧场最优. }
    数据范围及提示 Data Size & Hint

    见描述

    spfa 

    inf开小了 提莫的WA了一下午。。

    屠龙宝刀点击就送

    #include <cstring>
    #include <cstdio>
    #include <queue>
    
    using namespace std;
    #define inf 1e9 
    
    bool vis[890];
    int i,ans=inf,mc[890],n,p,c,tot,head[5451],dis[890];
    struct node
    {
        int next,to,dis;
    }edge[5451];
    inline void add(int from,int to,int cd)
    {
        tot++;
        edge[tot].next=head[from];
        edge[tot].to=to;
        edge[tot].dis=cd;
        head[from]=tot;
    }
    void spfa(int k)
    {
        memset(vis,0,sizeof(vis));
        memset(dis,1,sizeof(dis));
        queue<int>q;
        q.push(k);
        dis[k]=0;
        vis[k]=1;
        while(!q.empty() )
        {
            int topp=q.front() ;
            q.pop() ;
            vis[topp]=0;
            for(i=head[topp];i;i=edge[i].next)
            {
                int v=edge[i].to;
                if(dis[v]>dis[topp]+edge[i].dis)
                {
                    dis[v]=dis[topp]+edge[i].dis;
                    if(!vis[v])
                    {
                        vis[v]=1;
                        q.push(v);
                    }
                }
            }
        }
        int tot=0;
        for(i=1;i<=n;++i) 
        tot+=dis[mc[i]];
        ans=min(ans,tot);
    }
    int main()
    {
        scanf("%d%d%d",&n,&p,&c);
        for(i=1;i<=n;++i)
        scanf("%d",&mc[i]);
        int u,v,l;
        while(c--)
        {
            scanf("%d%d%d",&u,&v,&l);
            add(u,v,l);
            add(v,u,l);
        }
        for(int j=1;j<=p;++j) 
        spfa(j);
        printf("%d",ans);
        return 0;
    }
    我们都在命运之湖上荡舟划桨,波浪起伏着而我们无法逃脱孤航。但是假使我们迷失了方向,波浪将指引我们穿越另一天的曙光。
  • 相关阅读:
    leetcode刷题-26-删除有序数组重复项
    leetcode刷题-27-移除元素
    leetcode刷题-54-螺旋矩阵
    leetcode刷题-70-爬楼梯
    leetcode刷题-442-数组中重复的数据
    leetcode刷题-945-使数组唯一的最小增量
    leetcode刷题-11-盛最多水的容器
    random.choice函数
    Rating prediction and Ranking prediction
    Dev-c++在windows环境下无法debug(调试)的解决方案
  • 原文地址:https://www.cnblogs.com/ruojisun/p/6502381.html
Copyright © 2011-2022 走看看