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

 
次小生成树+不知道是不是的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=,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?的更多相关文章

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

    题目链接:http://poj.org/problem?id=2831 题意: 给你一个图,每条边有边权. 然后有q组询问(i,x),问你如果将第i条边的边权改为x,这条边是否有可能在新的最小生成树中 ...

  2. POJ(2784)Buy or Build

    Buy or Build Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1369   Accepted: 542 Descr ...

  3. POJ 2004 Mix and Build (预处理+dfs)

    题意: 给N个字符串,要求出一个序列,在该序列中,后一个串,是由前一个串加一个字母后得来的(顺序可以改动). 问最多能组成多长的序列.思路:将给的字符串排序,再对所有的字符串按长度从小到大排序,若长度 ...

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

    最小生成树算法简单 只是增加了一些新的东西,对于需要最小生成树算法 和中 并检查使用的一系列 还有一些更深入的了解. 方法的一些复杂问题 #include<cstdio> #include ...

  5. poj 2831 次小生成树模板

    /*次小生成树 题意:给你一些路径,现在将一部分路径权值减少后问是否可以替代最小生成树里面的边. 解:次小生成树,即将这条边连上,构成一个环 求出任意两点路径之间的除了这条边的最大值,比较这个最大值& ...

  6. POJ 2831

    次小生成树.求出两点间最短路径的最大权值,再把要加入的边与之比较即可. #include <iostream> #include <cstdio> #include <c ...

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

    Buy or Build Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1348   Accepted: 533 Descr ...

  8. [poj] 3907 Build Your Home || 求多边形面积

    原题 多组数据,到0为止. 每次给出按顺序的n个点(可能逆时针,可能顺时针),求多边形面积(保留整数) 多边形面积为依次每条边(向量)叉积/2的和 \(S=\sum _{i=1}^{n-1}p[i]* ...

  9. POJ 3907 Build Your Home | 计算多边形面积

    给个多边形 计算面积 输出要四舍五入 直接用向量叉乘就好 四舍五入可以+0.5向下取整 #include<cstdio> #include<algorithm> #includ ...

随机推荐

  1. (转)data Table的用法大全

    jqyery dataTable 基本用法 一:官方网站:[http://www.datatables.net/] 二:基本使用:[http://www.guoxk.com/node/jquery-d ...

  2. 20个Flutter实例视频教程-01节底部导航栏和切换效果的制作-1

    视频地址: https://www.bilibili.com/video/av39709290?zw 博客地址: https://jspang.com/post/flutterDemo.html#to ...

  3. bugfree-解决方案的意思

    BugFree的7种解决方案各自的含义: By Design - 就是这么设计的,无效的Bug Duplicate - 这个问题别人已经发现了,重复的Bug External - 是个外部因素(比如浏 ...

  4. 前端之CSS2

    CSS盒子模型 CSS盒子模型介绍 盒子模型解释 元素在页面中显示成一个方块,类似一个盒子,CSS盒子模型就是使用现实中盒子来做比喻,帮助我们设置元素对应的样式. 盒子模型示意图如下: 把元素叫做盒子 ...

  5. L2-023 图着色问题 (25 分)vector

    图着色问题是一个著名的NP完全问题.给定无向图,,问可否用K种颜色为V中的每一个顶点分配一种颜色,使得不会有两个相邻顶点具有同一种颜色? 但本题并不是要你解决这个着色问题,而是对给定的一种颜色分配,请 ...

  6. Linux下配置 禅道 (项目管理系统)

    1.官网下载源码 2.解析一个新的子域名,然后到linux服务器上新建一个站点 3.将下载的文件上传至linux服务器(新建站点的时候已经在 /home/wwwroot/ 里建好了域名对应的文件夹,将 ...

  7. laravel 安装配置前准备

    Laravel 框架使用 Composer 来管理其依赖性.所以,在你使用 Laravel 之前,你必须在你电脑上是否安装了 Composer.最简单的获取Composer的方式就是百度之,百度关键字 ...

  8. 洛谷P3265 [JLOI2015]装备购买(线性基+高斯消元)

    传送门 不知道线性基是什么东西的可以看看蒟蒻的总结 不难看出题目讲的就是线性基 这种最小化权值的问题一般都是贪心的,就是按价值从低到高考虑每一个是否能选 据说贪心的证明得用拟阵我不会 据说这题是实数意 ...

  9. 51Nod 1099 任务执行顺序 (贪心)

    #include <iostream> #include <algorithm> using namespace std; +; struct node{ int r, q; ...

  10. [題解]BZOJ_1260_塗色

    簡單的區間dp,結果竟然寫掛了......還掛的很徹底......狗屎 如果區間左右端點相等,那麼不需要在多花一次去刷,對 f [ i+1 ] [ j ],f [ i ] [ j-1 ]取個min, ...