USACO Section 3.2 Sweet Butter(SPFA)

Sweet Butter

Greg Galperin -- 2001

Farmer John has discovered the secret to making the sweetest butter in all of Wisconsin: sugar. By placing a sugar cube out in the pastures, he knows the N (1 <= N <= 500) cows will lick it and thus will produce super-sweet butter which can be marketed at better prices. Of course, he spends the extra money on luxuries for the cows.

FJ is a sly farmer. Like Pavlov of old, he knows he can train the cows to go to a certain pasture when they hear a bell. He intends to put the sugar there and then ring the bell in the middle of the afternoon so that the evening's milking produces perfect milk.

FJ knows each cow spends her time in a given pasture (not necessarily alone). Given the pasture location of the cows and a description of the paths the connect the pastures, find the pasture in which to place the sugar cube so that the total distance walked by the cows when FJ rings the bell is minimized. FJ knows the fields are connected well enough that some solution is always possible.

PROGRAM NAME: butter

INPUT FORMAT

  • Line 1: Three space-separated integers: N, the number of pastures: P (2 <= P <= 800), and the number of connecting paths: C (1 <= C <= 1,450). Cows are uniquely numbered 1..N. Pastures are uniquely numbered 1..P.
  • Lines 2..N+1: Each line contains a single integer that is the pasture number in which a cow is grazing. Cow i's pasture is listed on line i+1.
  • Lines N+2..N+C+1: Each line contains three space-separated integers that describe a single path that connects a pair of pastures and its length. Paths may be traversed in either direction. No pair of pastures is directly connected by more than one path. The first two integers are in the range 1..P; the third integer is in the range (1..225).

SAMPLE INPUT (file butter.in)

3 4 5
2
3
4
1 2 1
1 3 5
2 3 7
2 4 3
3 4 5

INPUT DETAILS

This diagram shows the connections geometrically:

          P2  
 P1 @--1--@ C1
     \    |\
      \   | \
       5  7  3
        \ |   \
         \|    \ C3
       C2 @--5--@
          P3    P4

OUTPUT FORMAT

  • Line 1: A single integer that is the minimum distance the cows must walk to a pasture with a sugar cube.

SAMPLE OUTPUT (file butter.out)

8

OUTPUT DETAILS:

Putting the cube in pasture 4 means: cow 1 walks 3 units; cow 2 walks 5
units; cow 3 walks 0 units -- a total of 8.
题意:FJ有 n 头奶牛和 p 个牧场,牧场间一个有 c 条路,n 头奶牛居住在牧场中,每个牧场中的奶牛数量不一,求FJ想在这 p 个牧场中选一个集合点,要满足所以奶牛都到这个集合点的集合所花的距离和最小。
分析:枚举 p 个牧场,然后SPFA;
View Code
/*
  ID: dizzy_l1
  LANG: C++
  TASK: butter
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#define MAXN 501
#define MAXP 801
#define INF 10000000

using namespace std;

struct edge{int v,w;};
vector<edge>e[MAXP];
int d[MAXP],a[MAXN];
bool visited[MAXP];

void SPFA(int u)
{
    int v,w;
    vector<edge>::iterator it;
    queue<int>Q;
    memset(visited,false,sizeof(visited));
    d[u]=0;
    visited[u]=true;
    Q.push(u);
    while(!Q.empty())
    {
        u=Q.front();
        for(it=e[u].begin();it!=e[u].end();it++)
        {
            v=it->v;w=it->w;
            if(d[v]>d[u]+w)
            {
                d[v]=d[u]+w;
                if(!visited[v])
                {
                    Q.push(v);
                    visited[v]=true;
                }
            }
        }
        Q.pop();
        visited[u]=false;
    }
}

int main()
{
    freopen("butter.in","r",stdin);
    freopen("butter.out","w",stdout);
    int n,p,c,u,v,w,i;
    edge t;
    while(scanf("%d%d%d",&n,&p,&c)==3)
    {
        for(i=0;i<n;i++) scanf("%d",&a[i]);
        for(i=0;i<=p;i++) e[i].clear();
        for(i=0;i<c;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            t.v=v;t.w=w;
            e[u].push_back(t);
            t.v=u;
            e[v].push_back(t);
        }
        int ans=INF,s;
        for(u=1;u<=p;u++)
        {
            for(v=1;v<=p;v++) d[v]=INF;
            SPFA(u);
            s=0;
            for(v=0;v<n;v++) s+=d[a[v]];
            if(ans>s) ans=s;
        }
        printf("%d\n",ans);
    }
    return 0;
}
posted @ 2012-09-13 21:49  mtry  阅读(285)  评论(0编辑  收藏  举报