风雪梦

柳絮因风起

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  4 Posts :: 76 Stories :: 3 Comments :: 0 Trackbacks

常用链接

留言簿

我参与的团队

搜索

  •  

最新评论

  • 1. re: LightOJ1080 Binary Simulation
  • 话说加个PushDown操作不就OK了咩?
  • --仗剑奔走天涯
  • 2. re: 正式开博
  • 加油!
  • --leafcloudsky
  • 3. re: 启航杯啊
  • 太屎了!!我竟然就这么的WA了两次,最终发现,第四题少了两句初始化,第五题把数组开错地方了,算法没问题,结果就这么从四题跌到二题,太伤不起了!!可怜我调spfa调了一晚上!!尼玛啊!!
  • --浅雨歌

阅读排行榜

评论排行榜

题目链接:http://poj.org/problem?id=1258

裸的最小生成树求边权值,给的是邻接矩阵,但是邻接矩阵的kruskal我不会用,就转换成了边表然后使用,说实话挺蛋疼的,但是好歹A掉了。。。

#include <iostream>
#include 
<cstdio>
#include 
<cstdlib>
#include 
<cstring>
#include 
<cmath>
#include 
<algorithm>
using namespace std;
#define N 101
int a[N][N];
struct edge
{
    
int u, v, w;
}e[
20010];
int tot, p[N];
void add(int x, int y, int z)
{
    tot
++;
    e[tot].u 
= x;
    e[tot].v 
= y;
    e[tot].w 
= z;
}
int find(int x)
{
    
return p[x] != x ? p[x] = find(p[x]) : x;
}
int cmp(edge a, edge b)
{
    
return a.w < b.w;
}
int main()
{
    
int r1, r2, ans, n;
    
while (scanf("%d"&n) != EOF)
    {
        tot 
= 0; memset(e, 0sizeof(e));
        
for (int i = 1; i <= n; i++)
        
for (int j = 1; j <= n; j++)
        {
            scanf(
"%d"&a[i][j]);
            
if (a[i][j] != 0) add(i, j, a[i][j]);
        }
        
for (int i = 1; i <= n; i++) p[i] = i;
        ans 
= 0;
        sort(e 
+ 1, e + tot + 1, cmp);
        
for (int i = 1; i <= tot; i++)
        {
            r1 
= find(e[i].u); r2 = find(e[i].v);
            
if (r1 != r2)
            {
                p[r2] 
= r1;
                ans 
+= e[i].w;
            }
        }
        printf(
"%d\n", ans);
    }
    
return 0;
}

posted on 2013-01-04 00:14 浅雨歌 阅读(62) 评论(0)  编辑 收藏 引用 所属分类: 并查集

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理