5098: Sweet Butter spfa

发布时间 2023-03-30 18:10:26作者: CRt0729

描述

 

 

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 that 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.

 

 

输入

 

 

  • 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).

 

 

输出

 

 

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

 

 

 

样例输入

 

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

样例输出

 8

提示

 

INPUT DETAILS:

This diagram shows the connections geometrically:

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

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.

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int inf = 1e6;
 4 struct node{
 5     int v,w;
 6 };
 7 int num[1005]; //记录第i个牧场奶牛的个数
 8 int dis[1005],vis[3000],ans; //dis起点到i的最短距离
 9 int n,p,c; 
10 vector<pair<int,int> > g[3005];
11 void spfa(int st)
12 {
13     memset(vis,0,sizeof(vis));
14     for(int i=1;i<=p;i++)
15     {
16         dis[i] = inf;
17     }
18     dis[st] = 0;vis[st] = 1;
19     queue<int> q;
20     q.push(st);
21     while(!q.empty())
22     {
23         int x = q.front();q.pop();
24         vis[x] = 0;
25         for(int i=0;i<g[x].size();i++)
26         {
27             int v = g[x][i].first,w = g[x][i].second;
28             if(dis[x]+w<dis[v])
29             {
30                 dis[v] = dis[x]+w;
31                 if(!vis[v])
32                 {
33                     vis[v] = 1;
34                     q.push(v);
35                 }
36             }
37         }
38     } 
39 }
40 int main()
41 {
42     cin>>n>>p>>c;
43     for(int i=1;i<=n;i++)
44     {
45         int x;cin>>x;num[x]++; 
46     } 
47     for(int i=1;i<=c;i++)
48     {
49         int x,y,z;cin>>x>>y>>z;
50         g[x].push_back(make_pair(y,z));
51         g[y].push_back(make_pair(x,z));
52     }
53     ans = 0x3f3f3f3f;
54     for(int i=1;i<=c;i++)
55     {
56         spfa(i);
57         int tmp = 0;
58         for(int i=1;i<=p;i++)
59             tmp += num[i]*dis[i];
60         ans = min(ans,tmp);
61     }
62     cout<<ans;
63      return 0;
64 }