• codevs 2038 香甜的黄油x+luogu P1828 x


    题目描述 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

    见描述

    分类标签 Tags

    思路:

      (对不起啊大佬!数组开小然后各种TLE,RE...我我我...一直70...然后就仅仅只是将数组改大而已qwq,就A了...)

      言归正传,思路就是:

      每个相同牧场出来的奶牛走的最优路径只有一条,所以不需要算多次,所以将位于同一起点的奶牛个数记录下来,在每个牧场都跑一遍spfa,寻找最小值.

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue> 
    #define Maxn 9999
    #define MMM 0x7fffffff
    
    using namespace std;
    
    int n,p,c,tot,ans=MMM;
    int num_qwq,head[Maxn],dist[Maxn];
    bool pd[Maxn];
    int cow,cishu[Maxn];
    /*
    神奇!
    因为每个相同牧场出来的奶牛走的最优路径只有一条,
    所以不需要算多次
    */
    queue<int>q;
    
    struct cower
    {
        int next,to,lenth;
    } qwq[Maxn];///邻接表 
    
    void add(int from,int to,int lenth)
    {
        qwq[++num_qwq].next=head[from];
        qwq[num_qwq].to=to;
        qwq[num_qwq].lenth=lenth;
        head[from]=num_qwq;
    }
    
    void sweetbutter(int s)
    {
        while(!q.empty()) q.pop();
        dist[s]=0;
        q.push(s);
        pd[s]=1;
        int father,son;
        while(!q.empty())
        {
            father=q.front();
            for(int p=head[father]; p!=-1; p=qwq[p].next)
            {
                son=qwq[p].to;
                int w=qwq[p].lenth;
                if(dist[father]+w<dist[son])
                {
                    dist[son]=dist[father]+w;
                    if(!pd[son])
                    {
                        q.push(son);
                        pd[son]=1;
                    }
                }
            }
            pd[father]=0;
            q.pop();
        }
    }
    
    int main()
    {
        scanf("%d %d %d",&n,&p,&c);
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&cow);
            cishu[cow]++;///记录奶牛出现在各个牧场的次数
        }
        memset(head,-1,sizeof(head));
        int a,b,d;
        for(int i=1; i<=c; i++)
        {
            scanf("%d %d %d",&a,&b,&d);
            add(a,b,d);
            add(b,a,d);
        }
        for(int i=1; i<=p; i++)
        {
            fill(dist,dist+Maxn,MMM);
            sweetbutter(i);
            tot=0;
            for(int j=1; j<=p; j++)///这样可以快好多!!! 
                tot+=dist[j]*cishu[j];
            if(tot<ans) ans=tot;
        }
        printf("%d",ans);
        return 0;
    }
    View Code

    如果运气好也是错,那我倒愿意错上加错!

    ❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀

  • 相关阅读:
    fetch与xhr的对比
    使用 fetch
    数组的剩余方法
    slice方法与splice方法
    php命名空间使用
    PHP面向对象编程学习之对象基础
    ubuntu下lamp环境配置及将window代码迁移至linux系统
    ThinkPHP中使用ajax接收json数据的方法
    给js文件传递参数
    JavaScript和php常用语法——切割字符串
  • 原文地址:https://www.cnblogs.com/zxqxwnngztxx/p/6696191.html
Copyright © 2020-2023  润新知