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 ...
随机推荐
- python os.system重定向stdout到变量 ,同时获取返回值
Python执行系统命令的方法 os.system(),os.popen(),commands 最近在做那个测试框架的时候发现 Python 的另一个获得系统执行命令的返回值和输出的类. 最开始的时候 ...
- 简单易用"里程碑"、"时间轴"<iOS小组件>
非常感谢,帮助我的朋友们,谢谢你们.上次我的好朋友指出了我编码上(jwTextFiled工具组件)存在一些不规范问题,这次注意提高. 呆毛地址:https://github.com/NIUXINGJI ...
- ping测试网络
https://jingyan.baidu.com/article/ac6a9a5e109d5f2b653eacbc.html 百度百科:https://baike.baidu.com/item/pi ...
- docker的安装与卸载
卸载老版本docker sudo apt-get remove docker docker-engine docker.io /var/lib/docker/目录下存放着 images, contai ...
- 发送邮件小工具(python)
#!/usr/bin/python # -*- coding:UTF- -*- import sys import smtplib import email.mime.multipart import ...
- java集合框架之比较器Comparator、Comparable
参考http://how2j.cn/k/collection/collection-comparator-comparable/693.html Comparator 假设Hero有三个属性 name ...
- 7.11实习培训日志-Git Linux
Git git子模块 先在GitHub创建两个空的respository,一个super_project和一个sub_project. 然后在git bash中向库中写入一些文件. 在super_pr ...
- U3D开发性能优化笔记(待增加版本.x)
http://blog.csdn.net/kaitiren/article/details/45071997 此总结由自己经验及网上收集整理优化内容 包括: .代码方面: .函数使用方面: .ui注意 ...
- IT兄弟连 JavaWeb教程 JSP内置对象1
JSP内置对象定义 JSP提供了由容器实现和管理的内置对象,也可以称之为隐含对象,这些内置对象不需要通过JSP页面编写来实例化,在所有的JSP页面中都可以直接使用,它们起到了简化页面的作用,JSP的内 ...
- Springboot整合elasticSearch的官方API实例
前言:在上一篇博客中,我介绍了从零开始安装ElasticSearch,es是可以理解为一个操作数据的中间件,可以把它作为数据的存储仓库来对待,它具备强大的吞吐能力和计算能力,其基于Lucene服务器开 ...