POJ 2831 Can We Build This One (Prim+找圈中最大边)

本文介绍了一个针对村庄间高速公路建设的优化算法。面对众多可能的路径选择,如何在确保所有村庄都能通过直接或间接方式连接的同时,使总成本最低?文章详细阐述了采用Prim算法寻找最小生成树的过程,并通过优化策略确定哪些昂贵的高速公路可以被更经济的选择所替代。

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

Can We Build This One?
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions:1860 Accepted: 682
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 (abc) which means a highway can built between the a-th village and the b-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 NM and Q (1 < N ≤ 1,000, N − 1 ≤ M ≤ 100,000, 0 < Q ≤ 100,000), where N 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 integers ab, and c (1 ≤ ab ≤ Na ≠ b, 0 ≤ c ≤ 1,000,000). The triplet (abc) describes a path. Each of following Q lines contains two integer i and x (1 ≤ i ≤ M, 0 ≤ x) describing a query, “Can a highway be built along the i-th path if the cost of is reduced to x?” x is strictly lower than the original cost of building a highway along the i-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
若有n个点,则最小生成树中有n-1条边,任意两条不直接相邻的点连一条线
都会形成一个圈用path[i][j]来记录连接i,j形成的环中除去i,j直接相连的边
的其余边的最长边,即连接i,j的最长的一条路。若y<=path[i][j],则可用
新添加的边形成最小生成树。每向生成树里加加一条边前,用dp思想求一下
目前生成树里的每个点到该点的最长距离
代码:
 
#include<bits/stdc++.h>
const int INF=0x3f3f3f3f;
int n,m,q,rode[1002][1002],d[1002],per[1002],path[1002][1002];
int from[100002],to[100002];
bool vis[1002];
using namespace std;
void init()
{
    for(int i=0;i<=1000;i++)
        for(int j=0;j<=1000;j++)
        {
            if(i==j)rode[i][j]=0,path[i][j]=0;
            else rode[i][j]=INF,path[i][j]=INF;
        }
    for(int i=0;i<=1000;i++)
        d[i]=INF;
    d[1]=0;
}
void Prim()
{
    memset(vis,0,sizeof(vis));
    while(1)
    {
        int v=-1;
        for(int i=1;i<=n;i++)
            if(!vis[i]&&(v==-1||d[v]>d[i]))
                v=i;
        if(v==-1)break;
        for(int i=1;i<=n;i++)
        {
            if(vis[i])
            {
                path[i][v]=path[v][i]=max(path[per[v]][i],d[v]);
                //i到v之间的最长边即i到v之前的最长边或v点到生成树的
                //距离
            }
        }
        vis[v]=1;
        for(int i=1;i<=n;i++)
        {
            if(!vis[i]&&d[i]>rode[v][i])
            {
                d[i]=rode[v][i];
                per[i]=v;//记录i的前驱结点v
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d%d",&n,&m,&q))
    {
        init();
        int i,j;
        for(i=1;i<=m;i++)
        {
            int x,y,z;scanf("%d%d%d",&x,&y,&z);
            if(z<rode[x][y])rode[x][y]=rode[y][x]=z;
            from[i]=x;to[i]=y;
        }
        Prim();
        while(q--)
        {
            int x,y;scanf("%d%d",&x,&y);
            int ff=from[x],tt=to[x];
            if(path[ff][tt]>=y)printf("Yes\n");
            else printf("No\n");
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值