poj2831Can We Build This One?

Can We Build This One?
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 1857 Accepted: 681
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
题意:给你一幅图,再给你Q个询问,每个询问为id和cost,即如果将id这条边的边权改为cost的话,这条边是否可能是最小生成树中的一条边
思路:如果这两点在生成树中的距离大于等于cost,则可以,反之,则不行。
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
const int inf=0x3f3f3f3f;
const int N=1005;
int g[N][N];
int dis[N];
int path[N][N];
bool used[N][N];
int pre[N];
int vis[N];
int n,m,q;
int s[111111],e[111111];
void prime()
{
    memset(vis,0,sizeof(vis));
    memset(used,false,sizeof(used));
    memset(path,0,sizeof(path));
    int i,j,u,v;
    int tmp;
    for(i=1;i<=n;i++)
    {
        dis[i]=g[1][i];
        pre[i]=1;
    }
    vis[1]=1;
    int ans=0;
   while(1)
    {
        u=-1;
        tmp=inf;
        for(j=1;j<=n;j++)
        {
            if(!vis[j]&&dis[j]<tmp)
            {
                tmp=dis[j];
                u=j;
            }
        }
        vis[u]=1;
        //ans+=tmp;
        if(u==-1)break;
        //used[u][pre[u]]=used[pre[u]][u]=true;
        for(j=1;j<=n;j++)
        {
            if(vis[j]&&u!=j)
            {
                path[j][u]=path[u][j]=max(path[j][pre[u]],dis[u]);
            }
            if(!vis[j])
            {
                if(dis[j]>g[u][j])
                {
                    dis[j]=g[u][j];
                    pre[j]=u;
                }
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d%d",&n,&m,&q))
    {
        int i,j;
        memset(g,0x3f,sizeof(g));
        int x,y,z;
        for(i=1;i<=m;i++)
        {
            scanf("%d%d%d",&s[i],&e[i],&z);
            if(z<g[s[i]][e[i]])
            g[s[i]][e[i]]=g[e[i]][s[i]]=z;
        }
        prime();
        while(q--)
        {
            scanf("%d%d",&x,&y);
            if(path[s[x]][e[x]]<y)
            {
                printf("No\n");
            }
            else printf("Yes\n");
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值