【codevs 2038】香甜的黄油

来源:互联网 发布:中心机房云计算 编辑:程序博客网 时间:2024/05/08 14:40

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
见描述

emmmmmmm
总和是所有有奶牛的牧场到源点的最短路之和
有奶牛!
注意答案ans的更新

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <queue>using namespace std;const int MAXN = 200005;int nn,n,m,tot = 0,f,t,v,x;int first[MAXN],nxt[MAXN],dis[MAXN],cnt[MAXN];bool used[MAXN];long long ans,ansss = 0;struct edge{    int f,t,v;}l[MAXN];void build(int f,int t,int v){    l[++ tot] = (edge){f,t,v};    nxt[tot] = first[f];    first[f] = tot;}deque < int > q;void spfa(int s){    while(!q.empty()) q.pop_front();    memset(dis,0x3f,sizeof(dis));    memset(used,0,sizeof(used));    used[s] = true; dis[s] = 0;    q.push_back(s);    while(!q.empty()){        int u = q.front();        q.pop_front();        used[u] = false;        for(int i = first[u]; i != -1; i = nxt[i]){            int w = l[i].t;            if(dis[w] > dis[u] + l[i].v){                dis[w] = dis[u] + l[i].v;                if(used[w]) continue;                if(q.empty()) q.push_front(w);                else if(dis[w] < dis[q.front()])q.push_front(w);                else q.push_back(w);                used[w] = true;            }        }    }    return;}int main(){    memset(first,0xff,sizeof(first));    memset(cnt,0,sizeof(cnt));    scanf("%d %d %d",&nn,&n,&m);    for(int i = 1; i <= nn; i ++)   scanf("%d",&cnt[i]);    for(int i = 1; i <= m; i ++){        scanf("%d %d %d",&f,&t,&v);build(f,t,v);build(t,f,v);    }    for(int i = 1; i <= n; i ++){        spfa(i); ansss = 0;        for(int j = 1; j <= nn; j ++)            ansss += dis[cnt[j]];        if(i == 1) ans = ansss;         else ans = min(ansss,ans);    }    printf("%lld\n",ans);    return 0;}