Prim_poj 1287 Networking

原创 2012年03月26日 14:59:02

题意:给出一个具有n个顶点和m条边的图,求其最小生成树。

#include<cstdio>
#include<cstring>
#include<vector>

#define MAXVN 55
#define INF 10000000

using namespace std;
typedef struct{
    int u,v,value;
}Edge;

typedef vector<Edge> VE;
VE eg[MAXVN];

vector<int> UsedPoint;
vector<Edge> UsedEdge;
int visited[MAXVN];

int vn, en;

int Prim(){
    memset(visited, 0 , sizeof(visited));
    UsedEdge.clear();
    UsedPoint.clear();
    UsedPoint.push_back(1);
    visited[1] = 1;
    int count = 1, minE = INF, iflag,jflag;
    while(count++ < vn){
        minE = INF;
        for(int i = 0; i < UsedPoint.size(); i++){
            int tmp = UsedPoint[i];
            for(int j = 0; j < eg[tmp].size(); j++){
                Edge e = eg[tmp][j];
                if(!visited[e.v] && e.value < minE){
                    minE = e.value;
                    iflag = tmp;
                    jflag = j;
                }
            }
        }
        UsedPoint.push_back(eg[iflag][jflag].v);
        visited[eg[iflag][jflag].v] = 1;
        UsedEdge.push_back(eg[iflag][jflag]);
    }
    int sum = 0;
    for(int i = 0; i < UsedEdge.size(); i++){
        sum+=UsedEdge[i].value;
    }
    return sum;

}
int main(){
    int a,b,c,sum;
    Edge te;
    while(scanf("%d", &vn),vn){
        sum = 0;
        for(int i = 1; i <= vn; i++){
            eg[i].clear();
        }
        scanf("%d", &en);
        getchar();
        for(int i = 0; i < en; i++){
            scanf("%d%d%d", &a, &b, &c);
            te.u = a;
            te.v = b;
            te.value = c;
            eg[a].push_back(te);
            te.u = b;
            te.v = a;
            te.value = c;
            eg[b].push_back(te);
        }
        sum = Prim();
        printf("%d\n", sum);
    }
}


POJ - 1287----Networking(prim)

You are assigned to design network connections between certain points in a wide area. You are given ...

POJ1287 Networking(最小生成树,Kruskal,Prim)

题目: Networking Time Limit: 1000MS   Memory Limit: 10000K Total Submissi...

poj1287 Networking 最小生成树模板题。prim+kruskal算法AC

You are assigned to design network connections between certain points in a wide area. You are given ...

poj 1287 Networking prim最小生成树 基础!!!

这是双向的,一开始调数据怎么都不对,后来一想这有双向边才符合输出!!1A#include using namespace std; int map[55][55],dis[55],vis[55];...

Prim最小生成树【poj 2349 Arctic Network;poj 1287 NetWorking】

poj 2349 Arctic Network: 图我们构造完成,调用prim算法,把该算法中每一次贪心得到的最小权边存入一个数组中。算法结束后对那个数组sort递减排序。这样,前边最大的s个用...

【HDU 1287】Networking(最小生成树Prim)

DescriptionYou are assigned to design network connections between certain points in a wide area. You...

POJ1287Networking(最小生成树,裸题)

Description You are assigned to design network connections between certain points in a wide area....

POJ 1287 Networking(最小生成树)

题意  给你n个点 m条边  求最小生成树的权 这是最裸的最小生成树了 #include #include #include using namespace std; const int N = ...
  • acvay
  • acvay
  • 2014年10月23日 10:25
  • 1225

ZOJ1372 POJ 1287 Networking 网络设计 Kruskal算法

最小生成树,采用Kruskal算法能够规避重边的问题,因为边是按照长度从小到大排序,因而当选择了长度短的边后便不会选择长度更长的重边。...

POJ 1287 Networking(prime算法)

Networking Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3788   Acc...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Prim_poj 1287 Networking
举报原因:
原因补充:

(最多只允许输入30个字)