POJ 1258 Agri-Net
Agri-Net
64-bit integer IO format: %lld Java class name: Main
Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.
The distance between any two farms will not exceed 100,000.
Input
Output
Sample Input
4 0 4 9 21 4 0 8 17 9 8 0 16 21 17 16 0
Sample Output
28
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 10000; 18 struct arc{ 19 int u,v,w; 20 arc(int x = 0,int y = 0,int z = 0){ 21 u = x; 22 v = y; 23 w = z; 24 } 25 }; 26 arc e[maxn]; 27 int uf[maxn],n,m; 28 int Find(int x){ 29 if(x != uf[x]) 30 uf[x] = Find(uf[x]); 31 return uf[x]; 32 } 33 bool cmp(const arc &x,const arc &y){ 34 return x.w < y.w; 35 } 36 int kruskal(){ 37 for(int i = 0; i <= n; i++) uf[i] = i; 38 sort(e,e+m,cmp); 39 int sum = 0; 40 for(int i = 0; i < m; i++){ 41 int tx = Find(e[i].u); 42 int ty = Find(e[i].v); 43 if(tx != ty){ 44 sum += e[i].w; 45 uf[tx] = ty; 46 } 47 } 48 return sum; 49 } 50 int main() { 51 int i,j,w,u,v; 52 while(~scanf("%d",&n)){ 53 for(m = i = 0; i < n; i++){ 54 for(j = 0; j < n; j++){ 55 scanf("%d",&w); 56 if(j > i) e[m++] = arc(i+1,j+1,w); 57 } 58 } 59 printf("%d\n",kruskal()); 60 } 61 return 0; 62 }
来个优先队列优化的prim。。。哈哈
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 10000; 18 struct arc { 19 int to,w; 20 arc(int x = 0,int y = 0) { 21 to = x; 22 w = y; 23 } 24 }; 25 vector<arc>g[maxn]; 26 priority_queue< pii, vector< pii >,greater< pii > >q; 27 int n,d[maxn]; 28 bool done[maxn]; 29 int prim(){ 30 for(int i = 0; i <= n; i++){ 31 d[i] = INF; 32 done[i] = false; 33 } 34 int ans = d[1] = 0; 35 while(!q.empty()) q.pop(); 36 q.push(make_pair(d[1],1)); 37 while(!q.empty()){ 38 int u = q.top().second; 39 int w = q.top().first; 40 q.pop(); 41 if(done[u]) continue; 42 done[u] = true; 43 ans += w; 44 for(int i = 0; i < g[u].size(); i++){ 45 if(d[g[u][i].to] > g[u][i].w){ 46 d[g[u][i].to] = g[u][i].w; 47 q.push(make_pair(g[u][i].w,g[u][i].to)); 48 } 49 } 50 } 51 return ans; 52 } 53 int main() { 54 int i,j,w; 55 while(~scanf("%d",&n)) { 56 for(i = 0; i <= n; i++) g[i].clear(); 57 for(i = 1; i <= n; i++) { 58 for(j = 1; j <= n; j++) { 59 scanf("%d",&w); 60 if(i != j) g[i].push_back(arc(j,w)); 61 } 62 } 63 printf("%d\n",prim()); 64 } 65 return 0; 66 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。