Can We Build This One?
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 1475   Accepted: 546
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

  1. 3 4 3
  2. 1 2 10
  3. 1 3 6
  4. 2 3 4
  5. 1 3 7
  6. 4 6
  7. 1 7
  8. 1 5

Sample Output

  1. Yes
  2. No
  3. Yes
  4.  
  5. 思路:查询第i条边时,比较边的两个端点在(u,v)在树中的最长路与第i条边修改后值的大小。
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <vector>
  5. using namespace std;
  6. const int MAXN = ;
  7. const int INF = 0x3f3f3f3f;
  8. struct Edge{
  9. int u, v, w;
  10. int getTo(int u)
  11. {
  12. if(this->u == u) return this->v;
  13. else return this->u;
  14. }
  15. }es[];
  16. int n, m, q;
  17. vector<int> arc[MAXN];
  18. int d[MAXN], vis[MAXN], pre[MAXN], dp[MAXN][MAXN];
  19. void prim(int src)
  20. {
  21. for(int i = ; i <= n; i++)
  22. {
  23. d[i] = INF;
  24. vis[i] = ;
  25. pre[i] = -;
  26. for(int j = ; j <= n; j++)
  27. {
  28. dp[i][j] = ;
  29. }
  30. }
  31. int t = n;
  32. d[src] = ;
  33. while(t--)
  34. {
  35. int mincost = INF, k;
  36. for(int i = ; i <= n; i++)
  37. {
  38. if(!vis[i] && d[i] < mincost)
  39. {
  40. mincost = d[i];
  41. k = i;
  42. }
  43. }
  44. int fa = pre[k];
  45. for(int i = ; i <= n; i++)
  46. {
  47. if(vis[i])
  48. {
  49. dp[i][k] = dp[k][i] = max(dp[i][fa], mincost);
  50. }
  51. }
  52. vis[k] = ;
  53. for(int i = , size = arc[k].size(); i < size; i++)
  54. {
  55. int id = arc[k][i];
  56. int v = es[id].getTo(k);
  57. if(!vis[v] && d[v] > es[id].w)
  58. {
  59. d[v] = es[id].w;
  60. pre[v] = k;
  61. }
  62. }
  63. }
  64. }
  65. int main()
  66. {
  67. while(scanf("%d %d %d", &n ,&m, &q) != EOF)
  68. {
  69. for(int i = ; i <= n; i++) arc[i].clear();
  70. for(int i = ; i < m; i++)
  71. {
  72. scanf("%d %d %d", &es[i].u, &es[i].v, &es[i].w);
  73. arc[es[i].u].push_back(i);
  74. arc[es[i].v].push_back(i);
  75. }
  76. prim();
  77. while(q--)
  78. {
  79. int id, x;
  80. scanf("%d %d", &id, &x);
  81. id--;
  82. if(x <= dp[es[id].u][es[id].v])
  83. {
  84. printf("Yes\n");
  85. }
  86. else
  87. {
  88. printf("No\n");
  89. }
  90. }
  91. }
  92. return ;
  93. }

POJ2831(次小生成树问题)的更多相关文章

  1. 2014.first[未填]

    之后就按照自己的直觉,整理了第一套,难度为简单,差不多比2013noipday1水一点...先练练手而已 T1 vijos1196吃糖果游戏 博弈论 依题意,我们可知,如果去分数目为2,3,7,8必输 ...

  2. UVA-10600.Contest and Blackout.(Kruskal + 次小生成树)

    题目链接 本题思路:模版的次小生成树问题,输出MST and Second_MST的值. 参考代码: #include <cstdio> #include <cstring> ...

  3. 次小生成树(Prim + Kruaskal)

    问题引入: 我们先来回想一下生成树是如何定义的,生成树就是用n - 1条边将图中的所有n个顶点都连通为一个连通分量,这样的边连成子树称为生成树. 最小生成树很明显就是生成树中权值最小的生成树,那么我们 ...

  4. NetworkX系列教程(10)-算法之二:最小/大生成树问题

    小书匠 Graph 图论  重头戏部分来了,写到这里我感觉得仔细认真点了,可能在NetworkX中,实现某些算法就一句话的事,但是这个算法是做什么的,用在什么地方,原理是怎么样的,不清除,所以,我决定 ...

  5. 【小程序分享篇 二 】web在线踢人小程序,维持用户只能在一个台电脑持登录状态

    最近离职了, 突然记起来还一个小功能没做, 想想也挺简单,留下代码和思路给同事做个参考. 换工作心里挺忐忑, 对未来也充满了憧憬与担忧.(虽然已是老人, 换了N次工作了,但每次心里都和忐忑). 写写代 ...

  6. 【小程序分享篇 一 】开发了个JAVA小程序, 用于清除内存卡或者U盘里的垃圾文件非常有用

    有一种场景, 手机内存卡空间被用光了,但又不知道哪个文件占用了太大,一个个文件夹去找又太麻烦,所以我开发了个小程序把手机所有文件(包括路径下所有层次子文件夹下的文件)进行一个排序,这样你就可以找出哪个 ...

  7. jQuery实践-网页版2048小游戏

    ▓▓▓▓▓▓ 大致介绍 看了一个实现网页版2048小游戏的视频,觉得能做出自己以前喜欢玩的小游戏很有意思便自己动手试了试,真正的验证了这句话-不要以为你以为的就是你以为的,看视频时觉得看懂了,会写了, ...

  8. 微信小程序开发心得

    微信小程序也已出来有一段时间了,最近写了几款微信小程序项目,今天来说说感受. 首先开发一款微信小程序,最主要的就是针对于公司来运营的,因为,在申请appid(微信小程序ID号)时候,需要填写相关的公司 ...

  9. 前端网络、JavaScript优化以及开发小技巧

    一.网络优化 YSlow有23条规则,中文可以参考这里.这几十条规则最主要是在做消除或减少不必要的网络延迟,将需要传输的数据压缩至最少. 1)合并压缩CSS.JavaScript.图片,静态资源CDN ...

随机推荐

  1. INSPIRED启示录 读书笔记 - 第4章 产品管理与产品设计

    理解用户体验设计 1.用户研究:专门研究.分析用户,评估产品或产品原型是否符合特定用户的使用习惯.其具体工作包括拟订恰当的测试项目,监督测试,评估测试结果,提出改进方案 2.交互设计:在理解目标用户的 ...

  2. CentOS 7 导入epel库

    yum install epel-release 或者到百度云下载相应的 rpm 包进行安装 rpm -ivh epel-release-7-9.noarch.rpm

  3. poj 3468 线段树模板题

    #include<iostream> #include<algorithm> #include<stdio.h> using namespace std; #def ...

  4. 查看git安装目录

    有时候需要知道Git在电脑中的安装位置,这里简单介绍下: Mac平台:在命令行中输入which git, 就会显示git的安装位置了; Windows平台:打开cmd,输入where git就会显示g ...

  5. springcloud-搭建服务注册中心

    创建服务注册中心 1.创建一个springboot 命名为eureka-server 1)添加Eureka依赖 pom.xml <?xml version="1.0" enc ...

  6. Android深度探索(卷1)HAL与驱动开发 虚拟环境的安装

    前言: 最近在看<Android深度探索(卷1)HAL与驱动开发>安装随书带的虚拟环境浪费了很多时间,说是虚拟环境的安装倒不如说是虚拟环境的导入,其实没什么技术含量,也没有什么复杂的,只是 ...

  7. IOS 发布被拒 PLA 1.2问题 整个过程介绍 01

    公司前端时间发布IOS APP ,但是遇到一些很麻烦的问题,就是一个让人摸不着头脑的问题. 问题: The Seller and Artist names associated with your a ...

  8. Java中interface是否继承Object类

    首先我们从C++说起, c++可以多继承.也就是一个类型 --- class,可以继承自2个以上的父类型.多继承导致一个问题,很多人知道.例如,如果类型B,类型C均继承自类型A.然后类型D继承自类型B ...

  9. 0.00-050613_Makefile

    # Makefile for the simple example kernel. AS86 =as86 -0 -a LD86 =ld86 -0 AS =gas LD =gld LDFLAGS =-s ...

  10. IOS开发--解析复杂json数据

    json的自我介绍:JSON(JavaScript Object Notation)是一种轻量级的数据交换格式.JSON采用完全独立于语言的文本格式,这些特性使JSON成为理想的数据交换语言.易于人阅 ...