PTA 7-2 公路村村通

文章描述了使用Prim算法解决村落间道路建设问题,给定成本数据,目标是找到最低成本使得所有村落都能通过公路相连。代码实现了一个最小生成树算法来计算所需的最低成本。

现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本,求使每个村落都有公路连通所需要的最低成本。

输入格式:

输入数据包括城镇数目正整数N(≤1000)和候选道路数目M(≤3N);随后的M行对应M条道路,每行给出3个正整数,分别是该条道路直接连通的两个城镇的编号以及该道路改建的预算成本。为简单起见,城镇从1到N编号。

输出格式:

输出村村通需要的最低成本。如果输入数据不足以保证畅通,则输出−1,表示需要建设更多公路。

输入样例:

6 15
1 2 5
1 3 3
1 4 7
1 5 4
1 6 2
2 3 4
2 4 6
2 5 2
2 6 6
3 4 6
3 5 1
3 6 1
4 5 10
4 6 8
5 6 3

输出样例:

12

 代码:

//最小生成树

#include<stdio.h>
#include<stdlib.h>

typedef struct GNode* MGraph;
struct GNode {
    int Nv;
    int Ne;
    int** G;
};

typedef struct ENode* Edge;
struct ENode {
    int V1, V2;
    int Weight; //权重
};

MGraph CreateGraph(int VertexNum) {
    int X, Y;
    MGraph Graph;

    Graph = (MGraph)malloc(sizeof(struct ENode));
    Graph->G = (int**)malloc(sizeof(int*) * 1001);
    for (int i = 0; i < 1001; i++) {
        Graph->G[i] = (int*)malloc(sizeof(int) * 1001);
    }
    Graph->Nv = VertexNum;
    Graph->Ne = 0;
    for (X = 1; X <= Graph->Nv; X++) {
        for (Y = 1; Y <= Graph->Nv; Y++) {
            Graph->G[X][Y] = 100000; //假设若X到Y没有直接的边,则Graph->G[X][Y]定义为100000
        }
    }

    return Graph;
}

void InsertEdge(MGraph Graph, Edge E) {
    Graph->G[E->V1][E->V2] = E->Weight;
    Graph->G[E->V2][E->V1] = E->Weight;
}

MGraph BuildGraph() {
    MGraph Graph;
    Edge E;
    int Nv, i;

    scanf("%d", &Nv);
    Graph = CreateGraph(Nv);

    scanf("%d", &(Graph->Ne));
    if (Graph->Ne != 0) {
        E = (Edge)malloc(sizeof(struct ENode));
        for (i = 0; i < Graph->Ne; i++) {
            scanf("%d %d %d", &E->V1, &E->V2, &E->Weight);
            InsertEdge(Graph, E);
        }
    }

    return Graph;
}

int FindMinDist(MGraph Graph, int dist[]) {
    int MinV, V;
    int MinDist = 100000;
    
    for (V = 1; V <= Graph->Nv; V++) {
        if (dist[V] != 0 && dist[V] < MinDist) {
            MinDist = dist[V];
            MinV = V;
        }
    }
    if (MinDist < 100000)
        return MinV;
    else
        return -1;
}

int Prim(MGraph Graph) {
    int dist[10001];
    int totalWeight; //权重和
    int V, W;
    int VCount=0; //收录的顶点数

    for (V = 1; V <= Graph->Nv; V++) {
        dist[V] = Graph->G[1][V];
    }
    totalWeight = 0;
    VCount++;
    dist[1] = 0;

    while (1) {
        V = FindMinDist(Graph, dist);

        if (V == -1)
            break;
        totalWeight += dist[V];
        dist[V] = 0;
        VCount++;

        for (W = 1; W <= Graph->Nv; W++) {
            if (dist[W] != 0 && Graph->G[V][W] < 100000) {
                if (Graph->G[V][W] < dist[W]) {
                    dist[W] = Graph->G[V][W];
                }
            }
        }
    }
    if (VCount < Graph->Nv)
        totalWeight = -1;
    return totalWeight;
}
int main() {

    MGraph Graph = BuildGraph();
    int result = Prim(Graph);

    printf("%d", result);
}

 

由于没有直接提供PTA 7 - 2发奖金题目的具体描述,但根据引用[1]中7 - 3奖金计算的代码推测,发奖金问题可能是根据不同的业绩区间来计算奖金。以下是对可能问题的解析及代码实现。 ### 题目解析 假设奖金计算规则如下: - 业绩在0 - 100000(包含100000),奖金为业绩的10%。 - 业绩在100000 - 200000(包含200000),超出100000部分的奖金为超出部分的7.5%,加上100000的10%。 - 业绩在200000 - 400000(包含400000),超出200000部分的奖金为超出部分的5%,加上前面区间的奖金。 - 业绩在400000 - 600000(包含600000),超出400000部分的奖金为超出部分的3%,加上前面区间的奖金。 - 业绩在600000 - 1000000(包含1000000),超出600000部分的奖金为超出部分的1.5%,加上前面区间的奖金。 - 业绩大于1000000,超出1000000部分的奖金为超出部分的1%,加上前面区间的奖金。 ### 代码实现 ```python x = eval(input()) sum = 0.0 if x > 1000000: sum += (x - 1000000) * 0.01 x = 1000000 if x > 600000: sum += (x - 600000) * 0.015 x = 600000 if x > 400000: sum += (x - 400000) * 0.03 x = 400000 if x > 200000: sum += (x - 200000) * 0.05 x = 200000 if x > 100000: sum += (x - 100000) * 0.075 x = 100000 if x >= 0: sum += x * 0.1 print("{:.2f}".format(sum)) else: print("0.00") ``` 此代码与引用[1]中的代码一致,通过多分支结构,根据不同的业绩区间计算奖金。首先读取输入的业绩值,然后根据业绩值所在的区间,逐步计算各区间的奖金并累加,最后输出奖金结果,结果保留两位小数。如果输入的业绩值为负数,奖金为0.00。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值