zoukankan      html  css  js  c++  java
  • POJ2831(次小生成树问题)

    Can We Build This One?
    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 1475   Accepted: 546
    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

    思路:查询第i条边时,比较边的两个端点在(u,v)在树中的最长路与第i条边修改后值的大小。
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    using namespace std;
    const int MAXN = 1005;
    const int INF = 0x3f3f3f3f;
    struct Edge{
        int u, v, w;
        int getTo(int u)
        {
            if(this->u == u)    return this->v;
            else    return this->u;
        }
    }es[100005];
    int n, m, q;
    vector<int> arc[MAXN];
    int d[MAXN], vis[MAXN], pre[MAXN], dp[MAXN][MAXN];
    void prim(int src)
    {
        for(int i = 1; i <= n; i++)
        {
            d[i] = INF;
            vis[i] = 0;
            pre[i] = -1;
            for(int j = 1; j <= n; j++)
            {
                dp[i][j] = 0;
            }
        }
        int t = n;
        d[src] = 0;
        while(t--)
        {
            int mincost = INF, k;
            for(int i = 1; i <= n; i++)
            {
                if(!vis[i] && d[i] < mincost)
                {
                    mincost = d[i];
                    k = i;
                }
            }
            int fa = pre[k];
            for(int i = 1; i <= n; i++)
            {
                if(vis[i])
                {
                    dp[i][k] = dp[k][i] = max(dp[i][fa], mincost);
                }
            }
            vis[k] = 1;
            for(int i = 0, size = arc[k].size(); i < size; i++)
            {
                int id = arc[k][i];
                int v = es[id].getTo(k);
                if(!vis[v] && d[v] > es[id].w)
                {
                    d[v] = es[id].w;
                    pre[v] = k;
                }
            }
        }
    }
    int main()
    {
        while(scanf("%d %d %d", &n ,&m, &q) != EOF)
        {
            for(int i = 1; i <= n; i++) arc[i].clear();
            for(int i = 0; i < m; i++)
            {
                scanf("%d %d %d", &es[i].u, &es[i].v, &es[i].w);
                arc[es[i].u].push_back(i);
                arc[es[i].v].push_back(i);
            }
            prim(1);
            while(q--)
            {
                int id, x;
                scanf("%d %d", &id, &x);
                id--;
                if(x <= dp[es[id].u][es[id].v])
                {
                    printf("Yes
    ");
                }
                else
                {
                    printf("No
    ");
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    【瞎口胡】基础数学 1 快速幂 整除 最大公约数
    【瞎口胡】二分图
    Windos下使用Redis
    VUE的踩坑日记(1)
    公告
    [维度打击]最大连续子序列
    常用函数
    树状数组
    高精度封装
    T4 模板之 单个文件
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5816350.html
Copyright © 2011-2022 走看看