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

分析:构建一个完全图,使用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. cenos6.5 64位下PHP远程连接sql server2008成功案例

    准备工作: 1.sql server2008服务器(开放远程端口,默认为1433,我用的是192.168.1.129) 2.安装好php的centos服务器 步骤: 1.php安装mssql扩展.ce ...

  2. LINQ to SQL 基础

    取得数据库Gateway 要操作数据库,我们首先要获得一个DataContext对象,这个对象相当于一个数据 库的Gateway,所有的操作都是通过它进行的.这个对象的名字是“Linq to SQL ...

  3. (Error) The type AESKeyGenerator is not accessible due to restriction on required library.

    error for 'Access restriction: The type AESKeyGenerator is not accessible due to restriction on requ ...

  4. UICollectionView出现the behavior of the UICollectionViewFlowLayout is not defined because:

    2015-01-28 21:55:17.790 Demo[636:9351] the behavior of the UICollectionViewFlowLayout is notdefined ...

  5. hdu1142 A Walk Through the Forest( Dijkstra算法+搜索)

    看到这道题,想起了我家旁边的山! 那是一座叫做洪山寨的山,据说由当年洪秀全的小妾居住于此而得名! 山上盛产野果(很美味)! 好久没有爬上去了! #include<stdio.h> #inc ...

  6. 深入了解使用egret.WebSocket

    概念 本教程不讲解TCP/IP协议,Socket属于哪层,消息包体怎么设计等,主讲 egret.WebSocket 使用示例 与 protobuf 使用示例. 在使用egret.WebSocket之前 ...

  7. hdu1003 Max Sum(经典dp )

      A - 最大子段和 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Descr ...

  8. CSU 1160(进制问题)

    CSU 1160   Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu   Descrip ...

  9. JS对象与json字符串格式

    <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&quo ...

  10. codeforces567E. President and Roads

    题目大意:总统要回家,会经过一些街道,每条街道都是单向的并且拥有权值.现在,为了让总统更好的回家,要对每一条街道进行操作:1)如果该街道一定在最短路上,则输出“YES”.2)如果该街道修理过后,该边所 ...