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 (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 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
#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=,int dis=) : to(to),dis(dis) {}
};
vector<node>vec[N];
void update(int s)
{
memset(vis,,sizeof(vis));
dist[s][s]=;
vis[s]=;
queue<int>Q;
Q.push(s);
for(int now=Q.front();!Q.empty();Q.pop(),now=Q.front())
{
for(int i=;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]=;
Q.push(v);
}
}
}
int main()
{
scanf("%d%d%d",&n,&m,&q);
for(int a,b,c,i=;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=;i<=n;i++) fa[i]=i;
sort(edge+,edge++m);
int num=;
for(int i=;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-) break;
}
}
for(int i=;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 ;
}
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,这条边是否有可能在新的最小生成树中 ...
- POJ(2784)Buy or Build
Buy or Build Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1369 Accepted: 542 Descr ...
- POJ 2004 Mix and Build (预处理+dfs)
题意: 给N个字符串,要求出一个序列,在该序列中,后一个串,是由前一个串加一个字母后得来的(顺序可以改动). 问最多能组成多长的序列.思路:将给的字符串排序,再对所有的字符串按长度从小到大排序,若长度 ...
- uva 1151 - Buy or Build poj 2784 Buy or Build(最小生成树)
最小生成树算法简单 只是增加了一些新的东西,对于需要最小生成树算法 和中 并检查使用的一系列 还有一些更深入的了解. 方法的一些复杂问题 #include<cstdio> #include ...
- poj 2831 次小生成树模板
/*次小生成树 题意:给你一些路径,现在将一部分路径权值减少后问是否可以替代最小生成树里面的边. 解:次小生成树,即将这条边连上,构成一个环 求出任意两点路径之间的除了这条边的最大值,比较这个最大值& ...
- POJ 2831
次小生成树.求出两点间最短路径的最大权值,再把要加入的边与之比较即可. #include <iostream> #include <cstdio> #include <c ...
- Buy or Build (poj 2784 最小生成树)
Buy or Build Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1348 Accepted: 533 Descr ...
- [poj] 3907 Build Your Home || 求多边形面积
原题 多组数据,到0为止. 每次给出按顺序的n个点(可能逆时针,可能顺时针),求多边形面积(保留整数) 多边形面积为依次每条边(向量)叉积/2的和 \(S=\sum _{i=1}^{n-1}p[i]* ...
- POJ 3907 Build Your Home | 计算多边形面积
给个多边形 计算面积 输出要四舍五入 直接用向量叉乘就好 四舍五入可以+0.5向下取整 #include<cstdio> #include<algorithm> #includ ...
随机推荐
- 64位windows7下安装python,配置numpy和matplotlib库
一.Python的安装 1.下载python2.7,下载地址:http://www.python.org/,选择系统相应版本,我选择是的是python2.7.6 . python-2.7.6rc1.a ...
- dubbo项目中包的依赖说明
依赖 (+) (#) 必需依赖 JDK1.5+ 理论上Dubbo可以只依赖JDK,不依赖于任何三方库运行,只需配置使用JDK相关实现策略. 缺省依赖 通过mvn dependency:tree > ...
- 查看Spring源码的方法
来自为知笔记(Wiz)
- ubuntu删除g2o
解决方法为:(1)删除/usr/local/include/g2o,指令为sudo rm -rf /usr/local/include/g2o:(2)删除/usr/local/lib下有关libg2o ...
- 我所理解的Restful API最佳实践
一直在公司负责API数据接口的开发,期间也遇到了不小的坑,本篇博客算是做一个小小的记录. 1. 不要纠结于无意义的规范 在开始本文之前,我想先说这么一句:RESTful 真的很好,但它只是一种软 ...
- UVa 1658 Admiral (最小费用流)
题意:给定一个图,求1-n的两条不相交的路线,并且权值和最小. 析:最小费用流,把每个结点都拆成两个点,中间连一条容量为1的边,然后一个作为入点,另一个是出点.最后跑两次最小费用流就行了. 代码如下: ...
- Swift异常处理
在Swift里,抛出的异常必须继承Error这个协议.那么这个协议是什么呢? 按住command再点击Error我们可以看到, public protocol Error { } extension ...
- zepto+mui开发中的tap事件重复执行
zepto.js和mui一起使用的时候,因为都有tap事件绑定tab事件后会多次触发还会报错,这时不引用zepto中的touch.js就可以了,只用mui的tap相关事件. $(function () ...
- OpenCV第一课
1.OpenCV下载地址:http://opencv.org/downloads.html 因为本人电脑装的是vs2010,所以下载的是opencv-2.4.11.exe(vc10.vc11.vc12 ...
- 解决overflow: auto在Ios中滑动不流畅
[bug]—— H5页面在 ios 端滑动不流畅的问题 IOS系统的惯性滑动效果非常6,但是当我们对div加overflow-y:auto;后是不会出这个效果的,滑动的时候会感觉很生涩.怎么办? ...