您的位置:首页 > 其它

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

2015-08-19 11:17 330 查看
Agri-Net

Time Limit: 1000MSMemory Limit: 10000K
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 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

The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines
of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.
Output

For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.
Sample Input
4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0

Sample Output
28


开始学最小生成树了,照着郭老师的代码敲了一边,第一次交还错了。。。不过发现了二维vector的clear()函数的正确使用方法

#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>

using namespace std;

const int INF=0x3f3f3f3f;

struct Edge{
int v,w;//v表示边端点,另一个端点已知;w表示边权值,也表示v到最小生成树的距离
Edge(int vv=0,int ww=INF):v(vv),w(ww) {}
bool operator < (const Edge& a) const {
return w>a.w;
}
}u;

int n,dis[105],ans,cnt;//dis表示各顶点到最小生成树的距离
bool vis[105];//vis表示各顶点是否已被加入最小生成树
vector<vector<Edge> > g(105);//邻接表

void prim() {//prim算法求最小生成树
int i,j,v,w;
priority_queue<Edge> q;//存放顶点及其到最小生成树的距离
memset(vis,false,sizeof(vis));
memset(dis,0x3f,sizeof(dis));
cnt=ans=0;//cnt表示已被加入最小生成树的顶点数;ans表示最小生成树的总权值
q.push(Edge(0,0));//开始只有顶点0,它到最小生成树的距离为0
while(cnt<n&&!q.empty()) {
do {//每次从队列里面拿离最小生成树最近的点
u=q.top();
q.pop();
}while(vis[u.v]&&!q.empty());
if(!vis[u.v]) {
ans+=u.w;
vis[u.v]=true;
++cnt;
for(i=0,j=g[u.v].size();i<j;++i) {//更新加入点的邻点
if(!vis[v=g[u.v][i].v]&&dis[v]>(w=g[u.v][i].w)) {
dis[v]=w;
q.push(Edge(v,w));
}
}
}
}
//若此时cnt<n,则图不连通
}

int main() {
int i,j,w;
while(1==scanf("%d",&n)) {
/*g.clear();        //或者最外层一维清空并且对最外层resize()
g.resize(n+1);*/
for(i=0;i<n;++i) {
g[i].clear();//必须最内层每一维单独清空才能清空所有数据
for(j=0;j<n;++j) {
scanf("%d",&w);
g[i].push_back(Edge(j,w));
}
}
prim();
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: