POJ 1258 Agri-Net

http://poj.org/problem?id=1258

裸求最小生成树

这句话好像没什么影响"Physically, they are limited in length to 80 characters, so some lines continue onto others"

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <queue>
 5 #define READ() freopen("in.txt", "r", stdin);
 6 #define MAXV 20007
 7 #define INF 0x3f3f3f3f
 8 using namespace std;
 9
10 typedef pair<int,int> P;
11 int N;
12 struct Edge
13 {
14     int to, cost, next;
15     Edge () {}
16     Edge(int to, int cost, int next) : to(to), cost(cost), next(next) {}
17 }edge[MAXV];
18
19 int head[MAXV];
20 int num = 0;
21
22 void Add(int from, int to, int cost)
23 {
24     edge[num] = Edge(to, cost, head[from]);
25     head[from] = num++;
26 }
27
28 int prim()
29 {
30     int res = 0;
31     int dist[MAXV];
32     bool use[MAXV];
33     priority_queue<P, vector<P>, greater<P> > que;
34     fill(dist, dist+MAXV, INF);
35     fill(use, use+MAXV, false);
36     dist[1] = 0;
37     que.push(P(dist[1], 1));
38     while (!que.empty())
39     {
40         P p = que.top();
41         que.pop();
42         int v = p.second, d = p.first;
43         if (!use[v])//**
44         {
45             res += dist[v];
46         }
47         use[v]= true;
48         if (dist[v] < d) continue;
49         int t = head[v];
50         while (t != -1)
51         {
52             Edge e = edge[t];
53             if (!use[e.to] && dist[e.to] > e.cost)//**
54             {
55                 dist[e.to] = e.cost;
56                 que.push(P(dist[e.to], e.to));
57             }
58             t = e.next;
59         }
60     }
61     return res;
62 }
63
64
65 int main()
66 {
67     READ()
68     while(~scanf("%d", &N))
69     {
70         int cost;
71         num = 0;
72         memset(head, -1, sizeof(head));
73         memset(edge, -1, sizeof(edge));
74         for (int i = 1; i <= N ;i ++)
75         {
76             for (int j = 1; j <= N; j++)
77             {
78                 scanf("%d", &cost);
79                 if (i == j) continue;
80                 Add(i, j, cost);
81                 Add(j, i, cost);
82             }
83         }
84         int ans = prim();
85         cout << ans << endl;
86     }
87
88 }
时间: 2024-10-25 22:22:56

POJ 1258 Agri-Net的相关文章

各种最小生成树。 HDU 1863 HDU 1301 POJ 1258

畅通工程 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 18811    Accepted Submission(s): 7981 Problem Description 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).经过调查评估,得到的统计表中列出

poj 1258 Agri-Net 解题报告

题目链接:http://poj.org/problem?id=1258 题目意思:给出 n 个 farm,每个farm 之间通过一定数量的fiber 相连,问使得所有farm 直接或间接连通的 最少 fiber 数是多少. 赤裸裸的最小生成树,用prim做的. 有个地方写错,wa 了 几次. 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 5 const int farm_num = 10000

rwkj 1505 poj 1258 10.1.5.253 1505

#include <iostream>// poj 1258 10.1.5.253 1505using namespace std; #define N 105 // 顶点的最大个数 (多写 int a[N][N],low[N],n,ans;int min(int x,int y){ return x<y?x:y; } void prim(int u0) { int i,j,m,k; ans=0; // for (i=1;i<n;i++) low[i]=a[u0][i]; // l

poj 1258

题意:给n*n矩阵 表示个点个边  求最小生成树 思路:Kruskal 算法 //:简单介绍一下题意.农民要建立互联网络,目的使村庄里所有的农民连上网, //并且总费用最小.多组数据,每组数据给出一个n,然后给出n * n大小的无向图的邻接矩阵表示,值表示边权. //要求输出最小生成树的权值和. #include<iostream> #include<cstring> using namespace std; int map[101][101]; int dist[101]; in

poj 1258 kruscal

//hnldyhy(303882171) 8:54:04 #include <iostream> // poj 1258#include <algorithm> using namespace std; #define N 105int p[N];void init (int n){ for (int i=1;i<=n;i++) p[i]=i; }int find(int x){ if (p[x]==x)return x; else return p[x]=find(p[x]

[2016-04-14][POJ][1258][Agri-Net]

时间:2016-04-14 20:36:55 星期四 题目编号:[2016-04-14][POJ][1258][Agri-Net] 题目大意:求最小生成树 分析:直接prim算法 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 100 + 10 ; int g[maxn][maxn],vis[maxn],lowc[maxn]; int

poj 1258 Agri-Net(最小生成树果题)

题目链接:http://poj.org/problem?id=1258 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course. Farmer John ordered a high speed

POJ 1258 Agri-Net (最小生成树+Prim)

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39820   Accepted: 16192 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He nee

【POJ 1258】 Agri-Net

[POJ 1258] Agri-Net 最小生成树模板 Prim #include #define INF 0x3f3f3f3f using namespace std; int mp[501][501]; int dis[501]; bool vis[501]; int n; int Prim() { int i,j,w,p,sum = 0; memset(dis,-1,sizeof(dis)); memset(vis,0,sizeof(vis)); dis[1] = 0; for(i = 0

poj 1258 Agri-Net (最小生成树 prim)

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39499   Accepted: 16017 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He nee