求解为什么只有40分
查看原帖
求解为什么只有40分
2616
h8416楼主2014/10/26 17:03
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef struct EDGE
{    int beg;    int end;    int cost;    int operator <(const struct EDGE& tar) const    {        return cost < tar.cost;    }};
int parent[150]={0};
int Find(int f){    while ( parent[f] > 0)    {        f = parent[f];    }    return f;}
int unionl(EDGE edge)
{        int n = Find(edge.beg);        int m = Find(edge.end);
        if (n != m) 
        {            parent[n] = m;            return 1;        }
        return 0;
}
//并查集
long long ans = 0;
int main()
{
    vector<EDGE> edges;    EDGE tedge;    
    int N;    cin>>N; //输入n 
    for(int i=0;i<N;i++)
        for(int j=0;j<N;j++)
        {
            int tco;
            cin>>tco;
            if(tco!=0)
            {    tedge.beg=i; tedge.end=j; tedge.cost=tco; edges.push_back(tedge);    }
        }
    //vector 构造边集数组 
    sort(edges.begin(),edges.end()); //排序 
    for(int i=0;i<edges.size();i++)
    {    if(unionl(edges[i]))        {            ans += edges[i].cost;        }    }
    cout << ans ;
    return 0;
}

2014/10/26 17:03
加载中...