POJ2831 Can We Build This One?(次小生成树)

本文探讨了如何通过次小生成树算法确定最优高速公路建设方案,确保任意两村庄间的连通性,同时考虑成本降低后的路线选择可能性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Can We Build This One?
Time Limit: 5000MS

Memory Limit: 65536K
Total Submissions: 1412

Accepted: 525
Case Time Limit: 2000MS

Description

“Highways are built, then life is rich.” Now people of Big Town want to become rich, so they are planning to build highways to connect their villages.

Big Town is really big and has many villages. Its people plan to build some highways between some pairs of villages so that every pair of villages is connected by the highways either directly or indirectly. After surveying the geographical surroundings, they find that there are some paths along with highways can be built. Every path is denoted by a triplet (a,b, c) which means a highway can built between the a-th village and theb-th village with a cost of c. In order to save money, they will select only part of the paths to build highways along so that the total cost to build highways along the selected paths is minimal under the condition that every pair of villages is connected.

It is possible that multiple such selections exist. People from every village want to have those highways of good interest to them built. But some highways can never appear in the selection since they are much too costly. So people ask whether a certain highway can be selected if they agree to cut the cost. Your task is to design a program to answer their queries.

Input

The first line of input contains three integers N, M and Q (1 < N ≤ 1,000, N − 1 ≤ M ≤ 100,000, 0 < Q ≤ 100,000), whereN is the number of villages, M is the number of paths, and Q is the number of queries. Each of the next M lines contains three integersa, b, and c (1 ≤ a, bN, ab, 0 ≤ c ≤ 1,000,000). The triplet (a, b,c) describes a path. Each of following Q lines contains two integeri and x (1 ≤ iM, 0 ≤ x) describing a query, “Can a highway be built along thei-th path if the cost of is reduced to x?” x is strictly lower than the original cost of building a highway along thei-th path. It is assumed that every pair of village will be connected either directly or indirectly if all possible highways are built. And there may be more than one highway that can be built between a pair of villages.

Output

Output one line for each query. Output either “Yes” or “No” as the answer to the the query.

Sample Input

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

Sample Output

Yes
No
Yes

Source

题意:有N个村庄,用M条边修建公路,每条边有各自的费用,要求使得任意两个村庄联通(直接或者间接),使得总费用最小。现在人们想知道如果第i条边的费用降低至x后,能不能修建该条边。
思路:次小生成树,设第i条边联通u,v两点,如果x小于或等于u,v两点间最大费用的边的话就可以修建,否则不可以。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define maxn 1005
#define ll long long int
bool vis[maxn];
int val[maxn][maxn], pre[maxn];
struct Edge{
    int u, v;
}edge[maxn*100];
int N;
ll MST, dp[maxn][maxn], mincost[maxn], x, cost;
void prim()
{
    memset(vis, 0, sizeof vis);
    memset(dp, 0, sizeof dp);
    for(int i = 1;i <= N;i++){
        mincost[i] = val[1][i];
        pre[i] = 1;
    }
    MST = 0;
    vis[1] = 1;
    while(true){
        int v = -1;
        for(int u = 1;u <= N;u++){
            if(!vis[u]&&(v==-1||mincost[u]<mincost[v])) v = u;
        }
        if(v == -1) break;
        //used[v][pre[v]] = used[pre[v]][v] = true;
        vis[v] = true;
        MST += val[v][pre[v]];
        for(int i = 1;i <= N;i++){
            if(vis[i]&&i!=v){
                dp[i][v] = dp[v][i] = max(dp[i][pre[v]], mincost[v]);
            }
            if(!vis[i]&&mincost[i]>val[v][i]){
                mincost[i] = val[v][i];
                pre[i] = v;
            }
        }
    }
}
int main()
{
    int M, Q, i, j;
    while(scanf("%d %d %d", &N, &M, &Q) != EOF){
        memset(val, 127, sizeof val);
        //printf("%I64d\n", val[0][0]);
        for(i = 1;i <= M;i++){
            scanf("%d %d %I64d", &edge[i].u, &edge[i].v, &cost);
            if(val[edge[i].u][edge[i].v]>cost){
                val[edge[i].u][edge[i].v] = val[edge[i].v][edge[i].u] = cost;
            }
        }
        prim();
        for(j = 1;j <= Q;j++){
            scanf("%d %I64d", &i, &x);
            if(dp[edge[i].u][edge[i].v] >= x) printf("Yes\n");
            //else if(!used[edge[i].u][edge[i].v]&&dp[edge[i].u][edge[i].v] >= x) printf("YES\n");
            else printf("No\n");
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值