• USACO Section 3.2 Sweet Butter (butter)


    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.
    
    思路:我是枚举牧场,然后spfa,求最短距离。加起来就是了,唉,又用了queue
    Executing...
       Test 1: TEST OK [0.000 secs, 12928 KB]
       Test 2: TEST OK [0.000 secs, 12928 KB]
       Test 3: TEST OK [0.000 secs, 12928 KB]
       Test 4: TEST OK [0.000 secs, 12928 KB]
       Test 5: TEST OK [0.000 secs, 12928 KB]
       Test 6: TEST OK [0.011 secs, 12928 KB]
       Test 7: TEST OK [0.032 secs, 12928 KB]
       Test 8: TEST OK [0.065 secs, 12928 KB]
       Test 9: TEST OK [0.108 secs, 12928 KB]
       Test 10: TEST OK [0.119 secs, 12928 KB]
    
    All tests OK.
      1 /*
      2 ID:wuhuaju2
      3 PROG:butter
      4 LANG:C++
      5 */
      6 #include <cstdio>
      7 #include <iostream>
      8 #include <cstdlib>
      9 #include <algorithm>
     10 #include <cstring>
     11 #include <string>
     12 #include <queue>
     13 using namespace std;
     14 
     15 const int INF=1000000;
     16 const int maxp=810,maxm=1510;
     17 int d[maxp],tot[maxm],dis[maxp][maxm],poi[maxp][maxm];
     18 bool vis[maxp];
     19 int a[maxp];
     20 int n,p,m,x,y,z,ans,sum;
     21 
     22 queue<int> q;
     23 
     24 void close()
     25 {
     26     fclose(stdin);
     27     fclose(stdout);
     28     exit(0);
     29 }
     30 
     31 void spfa(int s)
     32 {
     33     memset(vis,false,sizeof(vis));
     34     while (!q.empty())
     35         q.pop();
     36     for (int i=1;i<=p;i++)
     37         d[i]=INF;
     38     d[s]=0;
     39     q.push(s);
     40     vis[s]=true;
     41     while (!q.empty())
     42     {
     43         s=q.front();
     44         q.pop();
     45         for (int i=1;i<=tot[s];i++)
     46         {
     47             if (d[poi[s][i]]>d[s]+dis[s][i])
     48             {
     49                 d[poi[s][i]]=d[s]+dis[s][i];
     50                 if (not vis[poi[s][i]])
     51                 {
     52                     q.push(poi[s][i]);
     53                     vis[poi[s][i]]=true;
     54                 }
     55             }
     56         }
     57         vis[s]=false;
     58     }
     59 }
     60 
     61 void work()
     62 {
     63 //    for (int i=1;i<=p;i++)
     64 ans=INF;
     65         for (int i=1;i<=p;i++)
     66         {
     67             sum=0;
     68             spfa(i);
     69         //    cout<<"p:"<<i<<endl;
     70             for (int j=1;j<=n;j++)
     71                 sum+=d[a[j]];
     72     //        cout<<sum<<endl;
     73             if (sum<ans)
     74                 ans=sum;
     75         }
     76         printf("%d\n",ans);
     77 }
     78 
     79 void init ()
     80 {
     81 freopen("butter.in","r",stdin);
     82 freopen("butter.out","w",stdout);
     83     scanf("%d %d %d",&n,&p,&m);
     84      memset(tot,0,sizeof(tot));
     85      for (int i=1;i<=n;i++)
     86          scanf("%d",&a[i]);
     87      for (int i=1;i<=m;i++)
     88      {
     89          scanf("%d %d %d",&x,&y,&z);
     90          tot[x]++;
     91          dis[x][tot[x]]=z;
     92          poi[x][tot[x]]=y;
     93          tot[y]++;
     94          dis[y][tot[y]]=z;
     95          poi[y][tot[y]]=x;// wu xiang tu
     96      }
     97 }
     98 
     99 int main ()
    100 {
    101     init();
    102     work();
    103     close();
    104     return 0;
    105 }
    
    
    
     
  • 相关阅读:
    #Leetcode# 876. Middle of the Linked List
    #Leetcode# 237. Delete Node in a Linked List
    #Leetcode# 234. Palindrome Linked List
    #Leetcode# 149. Max Points on a Line
    #Leetcode# 152. Maximum Product Subarray
    #Leetcode# 228. Summary Ranges
    #Leetcode# 227. Basic Calculator II
    PAT 1089 狼人杀-简单版
    linux——web安全之sql注入【预习阶段】
    linux——攻防技术介绍|主动攻击|被动攻击
  • 原文地址:https://www.cnblogs.com/cssystem/p/2910821.html
Copyright © 2020-2023  润新知