最小生成树(削减某条边后该边能否构成最小生成树的成分)
来源:互联网 发布:网络古风翻唱歌手 编辑:程序博客网 时间:2024/06/07 00:03
http://poj.org/problem?id=2831
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 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 N, M 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 a, b, and c (1 ≤ a, b ≤ N, a ≠ b, 0 ≤ c ≤ 1,000,000). The triplet (a, b, c) 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 31 2 101 3 62 3 41 3 74 61 71 5
Sample Output
YesNoYes
题意:村庄之间要修高速公路,让这n个村庄可以连通,给出可以建公路的建议,然后让你选择一些公路,其实就是最小生成树,但是询问的是如果把某条边的费用削减成x,问x是否可以有被选择的可能;
分析:首先任意选择一个最小生成树,对于没有选择的边枚举,每次加入一条边权值w,然后肯定会构成一个环,找出该环中除了这条边之外费用最大的边p;如果削减后的边w小于等于p,则可以构成最小生成树的组成成分;
程序:
#include"stdio.h"#include"string.h"#include"stack"#include"math.h"#include"algorithm"#include"stdlib.h"#define M 1009#define inf 100000000#define eps 1e-12using namespace std;struct node{ int u,v,w,next,id,mark;}e[100009],edge[M*2];int f[M],head[M],t,pre[M],deep[M],prep[M];int num[100009],use[100009];void init(){ t=0; memset(head,-1,sizeof(head));}void add(int u,int v,int w){ edge[t].u=u; edge[t].v=v; edge[t].w=w; edge[t].next=head[u]; head[u]=t++;}int finde(int x){ if(x!=f[x]) f[x]=finde(f[x]); return f[x];}void dfs(int u,int f,int id){ deep[u]=id; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(v==f)continue; pre[v]=u; prep[v]=edge[i].w; dfs(v,u,id+1); }}int Lca(int u,int v//Lca搜最大的边{ int maxi=-inf; while(u!=v) { if(deep[u]>deep[v]) { maxi=max(maxi,prep[u]); u=pre[u]; } else { maxi=max(maxi,prep[v]); v=pre[v]; } } return maxi;}int cmp(node a,node b){ return a.w<b.w;}int main(){ int n,m,k,i,x; while(scanf("%d%d%d",&n,&m,&k)!=-1) { for(i=0;i<m;i++) { scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w); e[i].id=i; } sort(e,e+m,cmp); for(i=0;i<m;i++) num[e[i].id]=i; for(i=1;i<=n;i++) f[i]=i; memset(use,0,sizeof(use)); init(); for(i=0;i<m;i++) { int x=finde(e[i].u); int y=finde(e[i].v); if(x!=y) { f[x]=y; use[i]=1; add(e[i].u,e[i].v,e[i].w); add(e[i].v,e[i].u,e[i].w); } } dfs(1,1,1); for(i=0;i<m;i++)//枚举不在最小生成树里面的边 if(!use[i]) e[i].mark=Lca(e[i].u,e[i].v);//记录可以成为最小生成树的边的上限 while(k--) { scanf("%d%d",&i,&x); i--; if(use[num[i]]) printf("Yes\n"); else { if(x<=e[num[i]].mark) printf("Yes\n"); else printf("No\n"); } } } return 0;}
- 最小生成树(削减某条边后该边能否构成最小生成树的成分)
- LA3887 最小生成树 枚举最小边
- 最小比例 最小生成树
- POJ 3522 最大边与最小边差值最小的生成树(最小生成树的性质)
- 最小生成树&&次最小生成树
- 【vijos】【生成树】最小生成树的最小完全图
- 图的最小生成树
- 最小生成树的总结
- kruskal的最小生成树
- 最小生成树的算法
- 图的最小生成树
- 图的最小生成树
- spfa的最小生成树~
- 最小生成树的讲解
- 图的最小生成树
- 图的最小生成树
- 最小生成树的模板
- 图的最小生成树
- 几个名词术语
- hdu 4148 Length of S(n)(找规律)
- HDU 2089 不要62 (数位DP)
- ARM Linux内核驱动异常定位方法分析--反汇编方式
- HDU 1498 50 years, 50 colors
- 最小生成树(削减某条边后该边能否构成最小生成树的成分)
- version `GLIBCXX_3.4.11' not found错误及解决
- 内核Oops小结
- 2013谷歌笔试题
- Java遍历Properties
- tbnet 网络框架解析
- 如何调用notification在状态栏显示提醒,点击后弹出指定的activity并避免重复实例
- 智慧农业物联网方案,大棚管家自动灌溉泵站监控温湿度监控系统
- poj 1635 由搜索序列判断树是否同构