HDU 1863-畅通工程(Prim / Kruskal最小生成树)

畅通工程

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 34565    Accepted Submission(s): 15320


Problem Description
省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本。现请你编写程序,计算出全省畅通需要的最低成本。
 

Input
测试输入包含若干测试用例。每个测试用例的第1行给出评估的道路条数 N、村庄数目M ( < 100 );随后的 N
行对应村庄间道路的成本,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间道路的成本(也是正整数)。为简单起见,村庄从1到M编号。当N为0时,全部输入结束,相应的结果不要输出。
 

Output
对每个测试用例,在1行里输出全省畅通需要的最低成本。若统计数据不足以保证畅通,则输出“?”。
 

Sample Input
 
 
3 31 2 11 3 22 3 41 32 3 20 100
 
Sample Output
 
 
3?
 

Source

解题思路:我们先按照修路的费用从低到高给所有的路排一个序,然后按照费用从低到高修建路,最后修建的路能连通所有城市就输出费用,不能连通就输出“?”即可。

AC代码(Prim):

没有优化的PrimO(n^2)

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<math.h>
#include<stdlib.h>
#include<queue>
#include<map>
#include<set>
#define bug printf("*********\n");
#define mem0(a) memset(a, 0, sizeof(a));
#define mem1(a) memset(a, -1, sizeof(a));
#define in1(a) scanf("%d" ,&a);
#define in2(a, b) scanf("%d%d", &a, &b);
#define out1(a) printf("%d\n", a);
#define out2(a, b) printf("%d %d\n", a, b);
#define pb(G, a, b) G[a].push_back(b);
using namespace std;
typedef long long LL;
typedef pair<int, int> par;
const int mod = 1e9+7;
const int INF = 1e9+7;
const int N = 1000010;
const double pi = 3.1415926;

int n, m, ans;
int way[110][110], vis[110], cost[110]; //cost代表连接到生成树中的最小花费

void prim()
{
    int cur = 1;
    int sum = 0;
    cost[cur] = 0;
    while(sum < m) {
        cur = -1;
        for(int i = 1; i <= m; i ++) { //寻求最小花费的先边
            if(!vis[i] && (cur == -1 || cost[i] < cost[cur])) {
                cur = i;
            }
        }
        vis[cur] = 1;
        ans += cost[cur];
        sum ++;
        if(ans >= INF) break; //没有可以连的边了
        for(int i = 1; i <= m; i ++) { //更新边权最小值
            cost[i] = min(cost[i], way[cur][i]);
        }
    }
}

int main()
{
    int x, y, z;
    while(~scanf("%d%d", &n, &m) && n) {
        mem0(vis);
        ans =0;
        fill(way[0], way[110], INF);
        fill(cost, cost+110, INF);
        for(int i = 0; i < n; i ++) {
            scanf("%d%d%d", &x, &y, &z);
            way[x][y] = way[y][x] = z;
        }
        prim();
        if(ans < INF) printf("%d\n", ans);
        else printf("?\n");
    }
    return 0;
}
优先队列优化的PrimO(n*logn)

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<math.h>
#include<stdlib.h>
#include<queue>
#include<map>
#include<set>
#define bug printf("*********\n");
#define mem0(a) memset(a, 0, sizeof(a));
#define mem1(a) memset(a, -1, sizeof(a));
#define in1(a) scanf("%d" ,&a);
#define in2(a, b) scanf("%d%d", &a, &b);
#define out1(a) printf("%d\n", a);
#define out2(a, b) printf("%d %d\n", a, b);
#define pb(G, a, b) G[a].push_back(b);
using namespace std;
typedef long long LL;
typedef pair<int, int> par;
const int mod = 1e9+7;
const int INF = 1e9+7;
const int N = 1000010;
const double pi = 3.1415926;

int n, m, ans;
int way[110][110], vis[110], cost[110]; //cost代表连接到生成树中的最小花费

bool prim()
{
    int sum = 0;
    cost[1] = 0;
    priority_queue<pair<int, int> > q;
    q.push(make_pair(0, 1));
    while(!q.empty()) {
        par cur = q.top(); //logn时间内得出的最小值
        q.pop();
        if(vis[cur.second]) continue;
        vis[cur.second] = 1;
        ans += -cur.first;
        sum ++;
        for(int i = 1; i <= m; i ++) {
            cost[i] = min(cost[i], way[cur.second][i]);
            if(!vis[i] && cost[i] < INF) q.push(make_pair(-cost[i], i));
        }
    }
    return sum == m;
}

int main()
{
    int x, y, z;
    while(~scanf("%d%d", &n, &m) && n) {
        mem0(vis);
        ans =0;
        fill(way[0], way[110], INF);
        fill(cost, cost+110, INF);
        for(int i = 0; i < n; i ++) {
            scanf("%d%d%d", &x, &y, &z);
            way[x][y] = way[y][x] = z;
        }
        if(prim()) printf("%d\n", ans);
        else printf("?\n");
    }
    return 0;
}


AC代码(Kruskal):O(n*logn)
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<time.h>
#include<map>
#include<string>
#include<algorithm>
#include<set>
#define N 110

using namespace std;

int root[N], sum, money = 0;

struct node
{
    int a;
    int b;
    int c;
}road[N]; //连接a-b城市的路修建费用为c

bool cmp(node a, node b) //按照费用从低到高排序
{
    return a.c < b.c;
}

int formalset(int n) //初始化
{
    for(int i = 1; i <= n ;i ++)
    {
        root[i] = i;
    }
}

int find_root(int k) //查找根
{
    if(root[k] != k)
        root[k] = find_root(root[k]);
    return root[k];
}

void merge_root(int a, int b, int c) //两城市连通,合并,并加上费用
{
    if(find_root(a) == find_root(b)) //已经连通,返回
        return;
    sum --;
    money += c;
    root[find_root(b)] = find_root(a); //没有连通,合并连通
}

int main()
{
    int m, n;
    while(~scanf("%d", &m))
    {
        if(!m) break;
        money = 0;
        scanf("%d", &n);
        sum = n - 1;
        formalset(n);
        for(int i = 0; i < m; i ++)
        {
            scanf("%d%d%d", &road[i].a, &road[i].b, &road[i].c);
        }
        sort(road, road+m, cmp);
        for(int i = 0; i < m; i ++)
        {
            if(sum == 0) break; //如果所有的城市连通,就退出
            merge_root(road[i].a, road[i].b, road[i].c);
        }
        if(sum) printf("?\n");
        else printf("%d\n",money);
    }
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值