某个地方政府想修建一些高速公路使他们每个乡镇都可以相同通达,不过以前已经修建过一些公路,现在要实现所有的联通,所花费的最小代价是多少?(也就是最小的修建长度),输出的是需要修的路,不过如果不需要修建就什么都不输出。

分析:构建一个完全图,使用krusal进行一些简单优化不知道可以不,试一下吧
已经T成狗了
下面是TLE的krusal代码
******************************************************************************
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std; #define maxn 1005 int f[maxn];
struct node{
    int u, v;double len;
    friend bool operator < (node a, node b){
        return a.len > b.len;
    }
};
struct point{int x, y;}p[maxn], ans[maxn];
double Len(point a, point b){
    int x = a.x-b.x;
    int y = a.y-b.y;     return sqrt(x*x+y*y);
}
int Find(int x)
{
    if(f[x] != x)
        f[x] = Find(f[x]);
    return f[x];
}
int Union(int x, int y)
{
    x = Find(x);
    y = Find(y);     if(x != y)
    {
        f[x] = y;
        return 1;
    }     return 0;
}
bool cmp(point a, point b)
{
    if(a.x != b.x)
        return a.x < b.x;
    return a.y < b.y;
}
int main()
{
    int N;     scanf("%d", &N);     int M, u, v, t=0, k=0;
    node s;
    priority_queue<node> Q;     for(s.u=1; s.u<=N; s.u++)
    {
        f[s.u] = s.u;
        scanf("%d%d", &p[s.u].x, &p[s.u].y);
        for(s.v=1; s.v<s.u; s.v++)
        {
            s.len = Len(p[s.u], p[s.v]);
            Q.push(s);
        }
    }     scanf("%d", &M);     while(M--)
    {
        scanf("%d%d", &u, &v);
        t += Union(u, v);
    }     while(Q.size() && t < N)
    {
        s = Q.top();Q.pop();         if(Union(s.u, s.v) == 1)
        {
            t += 1;
            ans[k].x = s.v;
            ans[k++].y = s.u;
        }
    }     for(int i=0; i<k; i++)
        printf("%d %d\n", ans[i].x, ans[i].y);     return 0;
}

****************************************************************************

下面换成prim试一下吧
终于A掉了,使用优先队列会造成超内存
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std; #define maxn 1005
#define oo 0xfffffff double G[maxn][maxn];
struct point{int x, y;}p[maxn], ans[maxn];
struct node{int v;double len;}dist[maxn];
double Len(point a, point b){
    int x = a.x-b.x;
    int y = a.y-b.y;     return sqrt(x*x+y*y);
} void prim(int N)
{
    int i, use[maxn] = {0, 1};     for(i=1; i<=N; i++)
        dist[i].v = 1, dist[i].len = G[1][i];     for(i=1; i<N; i++)
    {
        int k=0;double Min = oo;         for(int j=1; j<=N; j++)
        {
            if(!use[j] && Min > dist[j].len)
                Min = dist[j].len, k = j;
        }         use[k] = true;         for(int j=1; j<=N; j++)
        {
            if(!use[j] && k!=j && G[k][j] < dist[j].len)
                dist[j].len = G[k][j], dist[j].v = k;
        }
    }
} int main()
{
    int i, j, N, M, u, v;     scanf("%d", &N);     for(i=1; i<=N; i++)
        scanf("%d%d", &p[i].x, &p[i].y);     for(i=1; i<=N; i++)
    for(j=1; j<i; j++)
    {
        G[j][i] = G[i][j] = Len(p[i], p[j]);
    }     scanf("%d", &M);     while(M--)
    {
        scanf("%d%d", &u, &v);
        G[u][v] = G[v][u] = -1;
    }     prim(N);     for(int i=2; i<=N; i++)
    {
        if(dist[i].len > 0)
            printf("%d %d\n", i, dist[i].v);
    }     return 0;
}

H - Highways - poj 1751(prim)的更多相关文章

  1. POJ 2253-Frogger (Prim)

    题目链接:Frogger 题意:两仅仅青蛙,A和B,A想到B哪里去,可是A得弹跳有限制,所以不能直接到B,可是有其它的石头作为过渡点,能够通过他们到达B,问A到B的全部路径中.它弹跳最大的跨度的最小值 ...

  2. c/c++ 用普利姆(prim)算法构造最小生成树

    c/c++ 用普利姆(prim)算法构造最小生成树 最小生成树(Minimum Cost Spanning Tree)的概念: ​ 假设要在n个城市之间建立公路,则连通n个城市只需要n-1条线路.这时 ...

  3. HDU.1233 还是畅通工程(Prim)

    HDU.1233 还是畅通工程(Prim) 题意分析 首先给出n,代表村庄的个数 然后出n*(n-1)/2个信息,每个信息包括村庄的起点,终点,距离, 要求求出最小生成树的权值之和. 注意村庄的编号从 ...

  4. linux内核中链表代码分析---list.h头文件分析(一)【转】

    转自:http://blog.chinaunix.net/uid-30254565-id-5637596.html linux内核中链表代码分析---list.h头文件分析(一) 16年2月27日17 ...

  5. linux内核中链表代码分析---list.h头文件分析(二)【转】

    转自:http://blog.chinaunix.net/uid-30254565-id-5637598.html linux内核中链表代码分析---list.h头文件分析(二) 16年2月28日16 ...

  6. ZOJ - 1586 QS Network (Prim)

    ZOJ - 1586 QS Network (Prim) #include<iostream> #include<cstring> using namespace std; + ...

  7. 普利姆算法(prim)

    普利姆算法(prim)求最小生成树(MST)过程详解 (原网址) 1 2 3 4 5 6 7 分步阅读 生活中最小生成树的应用十分广泛,比如:要连通n个城市需要n-1条边线路,那么怎么样建设才能使工程 ...

  8. POJ 1251 Jungle Roads (prim)

    D - Jungle Roads Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Su ...

  9. POJ题目(转)

    http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html 初期:一.基本算法:     (1)枚举. (poj1753,poj29 ...

随机推荐

  1. ansible小结

    一.Ansible的安装 1.yum源安装 以centos为例,默认在源里没有ansible,不过在fedora epel源里有ansible,配置完epel 源后,可以直接通过yum 进行安装.这里 ...

  2. JAVA 读取pdf文件

    第一个路口action /* * wuhan syspro author zhangrui 2010/08/23 */ package jp.co.syspro.poo.action; import ...

  3. 安装PHP过程中,make步骤报错:(集合网络上各种解决方法)

    安装PHP过程中,make步骤报错:(集合网络上各种解决方法) (1)-liconv -o sapi/fpm/php-fpm /usr/bin/ld: cannot find -liconv coll ...

  4. merge into Oracle里的 saveOrUapdate

    1.初始数据: SQL> select * from a; ID NAME ---------------------- ---------------------- 1 1 2 1 3 1 4 ...

  5. CSS 布局Float 【3】

    float 属性定义元素在哪个方向浮动. 浮动元素会生成一个块级框,而不论它本身是何种元素. 如果浮动非替换元素,则要指定一个明确的宽度:否则,它们会尽可能地窄. 注释:假如在一行之上只有极少的空间可 ...

  6. 【USACO 1.1.2】贪婪的送礼者

    [题目描述] 对于一群(NP个)要互送礼物的朋友,GY要确定每个人送出的钱比收到的多多少. 在这一个问题中,每个人都准备了一些钱来送礼物,而这些钱将会被平均分给那些将收到他的礼物的人. 然而,在任何一 ...

  7. C++ Primer 5th 第10章 泛型算法

    练习10.1:头文件algorithm中定义了一个名为count的函数,它类似find,接受一对迭代器和一个值作为参数.count返回给定值在序列中出现的次数.编写程序,读取int序列存入vector ...

  8. 原生JS 选项卡代码实现

    可实现同页面多个选项卡 效果图: 代码实现: HTML部分 <div class="main" id="tabs"> <div class=& ...

  9. ggts下载地址

    地址:http://spring.io/tools/ggts See All Versions可以下载更多版本,里面包含安装版和解压版

  10. html a标签

    <a> 标签定义超链接,用于从一张页面链接到另一张页面. <a> 元素最重要的属性是 href 属性,它指示链接的目标. 在所有浏览器中,链接的默认外观是: 未被访问的链接带有 ...