POJ 2831 Can We Build This One?

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 1728   Accepted: 643
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

Source

POJ Monthly--2006.05.28, zhucheng

次小生成树+不知道是不是的spfa

如果降低后费用小于等于两点间的最大费用,则输出Yes.

否则输出No.

prim算法好写些,但我忘记怎么写了。。

屠龙宝刀点击就送

#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#define M 100005
#define N 1005

using namespace std;
struct Edge
{
    int x,y,z;
    bool operator <(Edge a)const
    {
        return z<a.z;
    }
}edge[M],oedge[M];
bool vis[N];
int fa[N],n,m,q,dist[N][N];
int find_(int x) {return x==fa[x]?x:fa[x]=find_(fa[x]);}
struct node
{
    int to,dis;
    node (int to=0,int dis=0) : to(to),dis(dis) {}
};
vector<node>vec[N];
void update(int s)
{
    memset(vis,0,sizeof(vis));
    dist[s][s]=0;
    vis[s]=1;
    queue<int>Q;
    Q.push(s);
    for(int now=Q.front();!Q.empty();Q.pop(),now=Q.front())
    {
        for(int i=0;i<vec[now].size();i++)
        {
            int v=vec[now][i].to,w=vec[now][i].dis;
            if(vis[v]) continue;
            dist[s][v]=max(dist[s][now],w);
            vis[v]=1;
            Q.push(v);
        }
    }
}
int main()
{
    scanf("%d%d%d",&n,&m,&q);
    for(int a,b,c,i=1;i<=m;i++)
    {
        scanf("%d%d%d",&a,&b,&c);
        edge[i].x=a;
        edge[i].y=b;
        edge[i].z=c;
        oedge[i]=edge[i];
    }
    for(int i=1;i<=n;i++) fa[i]=i;
    sort(edge+1,edge+1+m);
    int num=0;
    for(int i=1;i<=m;i++)
    {
        int fx=find_(edge[i].x),fy=find_(edge[i].y);
        if(fx!=fy)
        {
            fa[fy]=fx;
            num++;
            vec[edge[i].x].push_back(node(edge[i].y,edge[i].z));
            vec[edge[i].y].push_back(node(edge[i].x,edge[i].z));
            if(num==n-1) break;
        }
    }
    for(int i=1;i<=n;i++) update(i);
    for(int xx,yy;q--;)
    {
        scanf("%d%d",&xx,&yy);
        if(dist[oedge[xx].x][oedge[xx].y]>=yy) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}
时间: 2024-12-17 09:50:47

POJ 2831 Can We Build This One?的相关文章

POJ 2831 Can We Build This One:次小生成树【N^2预处理】

题目链接:http://poj.org/problem?id=2831 题意: 给你一个图,每条边有边权. 然后有q组询问(i,x),问你如果将第i条边的边权改为x,这条边是否有可能在新的最小生成树中. 题解: 更改边权相当于新添加了一条边. 新边在新MST中的充要条件是: 加入新边后,在原来的MST上形成的环中,有一条旧边的边权>=x. (因为如果这样的话,新边可以替换掉那条最大的边) 所以可以预处理出 maxn[i][j]:在原来的MST上,任意两点间路径上的最大边权. dfs即可. 对于每

POJ 2831

次小生成树.求出两点间最短路径的最大权值,再把要加入的边与之比较即可. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 7 const int MAXN=1010; 8 const int MAXM=100010; 9 const int inf=100000000; 10 int ma

poj 2831 次小生成树模板

/*次小生成树 题意:给你一些路径,现在将一部分路径权值减少后问是否可以替代最小生成树里面的边. 解:次小生成树,即将这条边连上,构成一个环 求出任意两点路径之间的除了这条边的最大值,比较这个最大值>=这条边,说明可以替换. prime算法次小生成树模板 */ #include<stdio.h> #include<string.h> #define N 1100 #define inf 0x3fffffff int ma[N][N]; int Min(int a,int b)

POJ 2784 Buy or Build最小生成树

Buy or Build Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1360   Accepted: 539 Description World Wide Networks (WWN) is a leading company that operates large telecommunication networks. WWN would like to setup a new network in Borduri

POJ 2784 Buy or Build

Description World Wide Networks (WWN) is a leading company that operates large telecommunication networks. WWN would like to setup a new network in Borduria, a nice country that recently managed to get rid of its military dictator Kurvi-Tasch and whi

uva 1151 - Buy or Build poj 2784 Buy or Build(最小生成树)

也是简单的最小生成树算法 不过添加了一些新的东西,需要对最小生成树算法 以及其中的 并查集的使用 有一些比较深入的理解. 处理问题的方法也有些复杂 #include<cstdio> #include<cstring> #include<vector> #include<algorithm> using namespace std; const int maxn = 1005; struct point { int x; int y; }pp[maxn]; s

poj 2831(次小生成树)

题意:给你一幅图,再给你Q个询问,每个询问为id cost,即如果将id这条边的边权改为cost的话,这条边是否可能是最小生成树中的一条边 解题思路:将第i条边(u,v)的权值修改的话,要判断是否是最小生成树中的一条边,首先要把它加入进去,此时必定会引起原来的生成树成环,所以必定要擦去一条边,擦去的是哪一条边,这就利用到了次小生成树的原理了. 之前写过一个次小生成树的题,现在回过头看,感觉又有点不对了. 这里再总结一次: 首先肯定是构造一颗最小生成树,接下来就是枚举不在生成树里的边,假定为(u,

poj 3907 Build Your Home 多边形面积

题意: 给一个多边形,求它的面积. 分析: 算一遍叉积即可. 代码: //poj 3907 //sep9 #include <iostream> #include <cmath> using namespace std; int main() { float x0,y0,x1,y1; short n; while(scanf("%hd",&n)==1&&n){ float sum=0; scanf("%f%f",&am

Buy or Build (poj 2784 最小生成树)

Buy or Build Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1348   Accepted: 533 Description World Wide Networks (WWN) is a leading company that operates large telecommunication networks. WWN would like to setup a new network in Borduri