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

分析:构建一个完全图,使用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. python实现登录函数,比较简单

    一个简单的python实现登录以及修改密码的函数 #密码错误3次,锁定登录: password_list = ['] def account_login(): Tries = 3 while Trie ...

  2. 不同浏览器创建XMLHttpRequest对象

    function getXHR() { if (XMLHttpRequest) { return new XMLHttpRequest(); } else { return new ActiveXOb ...

  3. asp.net C# 导出EXCEL数据

    if (dt == null) { return ""; } Microsoft.Office.Interop.Excel.Application xlApp = new Micr ...

  4. UIWebView(本地数据部分)

    创建UIWebView和UISegmentedControl webView用于显示内容,segmentedControl用于切换读取内容的类型 为了方便起见 用拖拉控件形式布局完界面 /* 使用UI ...

  5. `~!$^*()[]{}\|;:'",<>/?在英文怎么读?

    `~!$^*()[]{}\|;:'",<>/?在英文怎么读? 'exclam'='!' 'at'='@' 'numbersign'='#' 'dollar'='$' 'perce ...

  6. BroadcastReceiver监听电量变化

    用BroadcastReceiver监听电量的变化,可以实现BroadcastReceiver接收电量变化的广播,然后获取电量百分比信息. BatteryChangedReceiver.java pu ...

  7. Objective-C中的协议(Protocol)和类别(Category)

    1.什么是协议? 2.协议与类别的声明和使用 1.什么是协议? 在Objective-C中,不支持多继承,即不允许一个类有多个父类,但是OC提供了类似的实现方法,也就是协议.协议有点类似于Java里的 ...

  8. ejabberd,erlang,简单看了一下,总结一下,很肤浅

    本来也没打算深入学习erlang,就是看一下他们的大概思路erlang每个自定义函数都能注册成进程,每个节点通过erl -name 'name@ip'.进去后,可以直接做远程调用,节点之间就靠一个连接 ...

  9. 写给新手看的Flask+uwsgi+Nginx+Ubuntu部署教程

    学习 Flask,写完一个 Flask 应用需要部署的时候,就想着折腾自己的服务器.根据搜索的教程照做,对于原理一知半解,磕磕碰碰,只要运行起来了,谢天谢地然后不再折腾了,到下一次还需要部署时,这样的 ...

  10. Android 开发中使用 SQLite 数据库

    SQLite 介绍 SQLite 一个非常流行的嵌入式数据库,它支持 SQL 语言,并且只利用很少的内存就有很好的性能. 此外它还是开源的,任何人都可以使用它.许多开源项目((Mozilla, PHP ...