EOJ:Watering Hole
Watering Hole
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submits: 120 | Accepted: 30 |
Description
Farmer John has decided to bring water to his N (1 <= N <= 300) pastures which are conveniently numbered 1..N. He may bring water to a pasture either by building a well in that pasture or connecting the pasture via a pipe to another pasture which already has water.
Digging a well in pasture i costs W_i (1 <= W_i <= 100,000).
Connecting pastures i and j with a pipe costs P_ij (1 <= P_ij <= 100,000; P_ij = P_ji; P_ii=0).
Determine the minimum amount Farmer John will have to pay to water all of his pastures.
Digging a well in pasture i costs W_i (1 <= W_i <= 100,000).
Connecting pastures i and j with a pipe costs P_ij (1 <= P_ij <= 100,000; P_ij = P_ji; P_ii=0).
Determine the minimum amount Farmer John will have to pay to water all of his pastures.
Input
* Line 1: A single integer: N
* Lines 2..N + 1: Line i+1 contains a single integer: W_i
* Lines N+2..2N+1: Line N+1+i contains N space-separated integers; the j-th integer is P_ij
* Lines 2..N + 1: Line i+1 contains a single integer: W_i
* Lines N+2..2N+1: Line N+1+i contains N space-separated integers; the j-th integer is P_ij
Output
* Line 1: A single line with a single integer that is the minimum cost of providing all the pastures with water.
Sample Input
454430 2 2 22 0 3 32 3 0 42 3 4 0
Sample Output
9
Hint
INPUT DETAILS:
There are four pastures. It costs 5 to build a well in pasture 1, 4 in pastures 2 and 3, 3 in pasture 4. Pipes cost 2, 3, and 4 depending on which pastures they connect.
OUTPUT DETAILS:
Farmer John may build a well in the fourth pasture and connect each pasture to the first, which costs 3 + 2 + 2 + 2 = 9.
There are four pastures. It costs 5 to build a well in pasture 1, 4 in pastures 2 and 3, 3 in pasture 4. Pipes cost 2, 3, and 4 depending on which pastures they connect.
OUTPUT DETAILS:
Farmer John may build a well in the fourth pasture and connect each pasture to the first, which costs 3 + 2 + 2 + 2 = 9.
_______________________________________________________________________________________________________
题解:
一开始以为是DP……
其实是图论:
把井也当成一个节点,与每个节点相连,权值就是挖井费用,最小生成树。
代码:
代码
1 #include<stdio.h>
2 int path[50005][3],flag[305],w[305],map[305][305];
3 int n,m,i,j,ans,a,b,loc,t;
4 void swap(int i,int j)
5 {
6 int temp;
7 temp=path[i][2];
8 path[i][2]=path[j][2];
9 path[j][2]=temp;
10 temp=path[i][1];
11 path[i][1]=path[j][1];
12 path[j][1]=temp;
13 temp=path[i][0];
14 path[i][0]=path[j][0];
15 path[j][0]=temp;
16 }
17
18 void qsort(int l,int r)
19 {
20 int i,j,x;
21 x=path[r][2];
22 i=l-1;
23 for (j=l;j<r;j++)
24 if (path[j][2]<x)
25 {
26 i++;
27 swap(i,j);
28 }
29 swap(i+1,r);
30 if (i+2<r) qsort(i+2,r);
31 if (i>l) qsort(l,i);
32 }
33 int main()
34 {
35 scanf("%d",&n);
36 ans=0;
37 t=0;
38 for (i=1;i<=n;i++)
39 {
40 t++;
41 path[t][0]=i;
42 path[t][1]=n+1;
43 scanf("%d",&path[t][2]);
44 }
45 for (i=1;i<=n;i++)
46 for (j=1;j<=n;j++)
47 scanf("%d",&map[i][j]);
48 for (i=1;i<n;i++)
49 for (j=i+1;j<=n;j++)
50 {
51 t++;
52 path[t][0]=i;
53 path[t][1]=j;
54 path[t][2]=map[i][j];
55 }
56 qsort(1,t);
57 for (i=1;i<=n+1;i++)
58 flag[i]=0;
59 flag[1]=1;
60 for (i=1;i<=n;i++)
61 {
62 for (j=1;j<=t;j++)
63 {
64 a=path[j][0];
65 b=path[j][1];
66 if ((flag[a]==1&&flag[b]==0)||(flag[b]==1&&flag[a]==0))
67 {
68 loc=j;
69 break;
70 }
71 }
72 a=path[loc][0];
73 b=path[loc][1];
74 flag[a]=1;
75 flag[b]=1;
76 ans=ans+path[loc][2];
77 }
78 printf("%d\n",ans);
79 return 0;
80 }