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);
    }
}


poj1287 - Networking

想看更多的解题报告: http://blog.csdn.net/wangjian8006/article/details/7870410                               ...
  • wangjian8006
  • wangjian8006
  • 2012-08-17 09:32:17
  • 1621

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

DescriptionYou are assigned to design network connections between certain points in a wide area. You...
  • iceiceicpc
  • iceiceicpc
  • 2016-07-18 16:19:25
  • 305

【POJ】-1287-Networking(最小生成树)

Networking Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8557   Acc...
  • Bear1998
  • Bear1998
  • 2016-08-02 21:10:20
  • 539

POJ 1287 Networking

Networking Time Limit:1000MS    Memory Limit:10000KB    64bit IO Format:%lld & %llu Descript...
  • FlytobuDu
  • FlytobuDu
  • 2016-08-04 11:58:46
  • 145

POJ 1287 Networking(最小生成树)

POJ 1287 Networking(最小生成树) http://poj.org/problem?id=1287 题意:        给你一个N个点和M条边的无向图,要求最小生成树的权值. 分析:...
  • u013480600
  • u013480600
  • 2014-07-20 19:49:55
  • 797

poj 1287 Networking

Networking Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6323   Acc...
  • u012866104
  • u012866104
  • 2014-09-01 22:56:35
  • 361

POJ - 1287 Networking prim算法

/** 水题 水题 prim 水过; 以后再来写一波克鲁斯卡尔;并查集不是很溜; */ #include #include using namespace std;const int inf=0x3f...
  • hypHuangYanPing
  • hypHuangYanPing
  • 2017-11-28 19:44:30
  • 62

poj 1287 networking kruskal

题目:点击打开链接。 #include #include #include #define MAX 60 #define INF 0x3f3f3f3f using namespace std; str...
  • define_danmu_primer
  • define_danmu_primer
  • 2015-05-06 21:44:20
  • 217

POJ 1287 Networking

Kruskal建图更加方便,不管三七二十一全部扔进去排序,然后并查集会自动帮助我们去重的。建图之后裸最小生成树 /************************************...
  • u013008291
  • u013008291
  • 2015-02-28 20:45:51
  • 298

Poj 1287 Networking

Networking Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7726   Acc...
  • xuhuanfeng232
  • xuhuanfeng232
  • 2016-01-22 17:14:53
  • 231
收藏助手
不良信息举报
您举报文章:Prim_poj 1287 Networking
举报原因:
原因补充:

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