USACO 香甜的黄油

来源:互联网 发布:英雄联盟网络连接失败 编辑:程序博客网 时间:2024/04/30 02:38

题目描述 Description

农夫John发现做出全威斯康辛州最甜的黄油的方法:糖。把糖放在一片牧场上,他知道N(1<=N<=500)只奶牛会过来舔它,这样就能做出能卖好价钱的超甜黄油。当然,他将付出额外的费用在奶牛上。
农夫John很狡猾。像以前的Pavlov,他知道他可以训练这些奶牛,让它们在听到铃声时去一个特定的牧场。他打算将糖放在那里然后下午发出铃声,以至他可以在晚上挤奶。
农夫John知道每只奶牛都在各自喜欢的牧场(一个牧场不一定只有一头牛)。给出各头牛在的牧场和牧场间的路线,找出使所有牛到达的路程和最短的牧场(他将把糖放在那)。

输入格式:

第一行:三个数:奶牛数N,牧场数(2<=P<=800),牧场间道路数C(1<=C<=1450)
第二行到第N+1行: 1到N头奶牛所在的牧场号
第N+2行到第N+C+1行: 每行有三个数:相连的牧场A、B,两牧场间距离D(1<=D<=255),当然,连接是双向的。

输出格式:

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

输入样例:

3 4 52341 2 11 3 52 3 72 4 33 4 5

输出样例:

8

这题乍一看好像是树的重心什么的乱搞,然而掐指一算数据范围发现原来可以每个点都跑一遍spfa 233,于是就变成了水题qwq。

#include<iostream>#include<cstdio>#include<deque>using namespace std;const int size = 200010;int head[size],next[size],dist[size];bool use[size];struct dc{    int t,d;}l[size];int tot = 1;int n,m,p;void build(int f,int t,int d){    l[tot].t = t;    l[tot].d = d;    next[tot] = head[f];    head[f] = tot ++;}int num[size];deque < int > q;void spfa(int s){    for(int i = 1 ; i <= p ; i ++)        dist[i] = 214748364;    dist[s] = 0;    use[s] = 1;    q.push_back(s);    while(!q.empty())    {        int f = q.front();        q.pop_front();        use[f] = 0;        for(int i = head[f] ; i ; i = next[i])        {            int t = l[i].t;            if(dist[t] > dist[f] + l[i].d)            {                dist[t] = dist[f] + l[i].d;                if(!use[t])                {                    use[t] = 1;                    if(!q.empty())                    {                        if(dist[t] < dist[q.front()])                            q.push_front(t);//                      else if(t > q.front())//                          q.push_front(t);                        else                            q.push_back(t);                    }                    else                        q.push_back(t);                }            }        }    }}int main(){    scanf("%d%d%d",&n,&p,&m);    for(int i = 1 ; i <= n ; i ++)    {        int pos;        scanf("%d",&pos);        num[pos] ++;    }    for(int i = 1 ; i <= m ; i ++)    {        int f,t,d;        scanf("%d%d%d",&f,&t,&d);        build(f,t,d);        build(t,f,d);    }    int ans = 214748364;    for(int i = 1 ; i <= p ; i ++)    {        spfa(i);        int tot = 0;        for(int j = 1 ; j <= p ; j ++)        {            tot += dist[j]*num[j];        }        ans = min(ans,tot);    }    cout<<ans;    return 0;}
0 0
原创粉丝点击