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

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 思路:查询第i条边时,比较边的两个端点在(u,v)在树中的最长路与第i条边修改后值的大小。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int MAXN = ;
const int INF = 0x3f3f3f3f;
struct Edge{
int u, v, w;
int getTo(int u)
{
if(this->u == u) return this->v;
else return this->u;
}
}es[];
int n, m, q;
vector<int> arc[MAXN];
int d[MAXN], vis[MAXN], pre[MAXN], dp[MAXN][MAXN];
void prim(int src)
{
for(int i = ; i <= n; i++)
{
d[i] = INF;
vis[i] = ;
pre[i] = -;
for(int j = ; j <= n; j++)
{
dp[i][j] = ;
}
}
int t = n;
d[src] = ;
while(t--)
{
int mincost = INF, k;
for(int i = ; i <= n; i++)
{
if(!vis[i] && d[i] < mincost)
{
mincost = d[i];
k = i;
}
}
int fa = pre[k];
for(int i = ; i <= n; i++)
{
if(vis[i])
{
dp[i][k] = dp[k][i] = max(dp[i][fa], mincost);
}
}
vis[k] = ;
for(int i = , size = arc[k].size(); i < size; i++)
{
int id = arc[k][i];
int v = es[id].getTo(k);
if(!vis[v] && d[v] > es[id].w)
{
d[v] = es[id].w;
pre[v] = k;
}
}
}
}
int main()
{
while(scanf("%d %d %d", &n ,&m, &q) != EOF)
{
for(int i = ; i <= n; i++) arc[i].clear();
for(int i = ; i < m; i++)
{
scanf("%d %d %d", &es[i].u, &es[i].v, &es[i].w);
arc[es[i].u].push_back(i);
arc[es[i].v].push_back(i);
}
prim();
while(q--)
{
int id, x;
scanf("%d %d", &id, &x);
id--;
if(x <= dp[es[id].u][es[id].v])
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
}
return ;
}

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. QGIS 编译

    QGIS 编译 在编译的过程中花费了很长时间,特别是编译Debug版本.release版本的编译可以从晚上找到很多的资料,但是Debug的编译相对较少.在Debug编译的过程中,需要单独build工程 ...

  2. 【算法】fhqtreap初探

    NOIP回来就一直想着学平衡树...平衡树写久了调不出来真的会头脑发热.jpg 大概只写了几道题... fhqtreap是不需要旋转的平衡树,仅使用分裂合并,一样可以保持平衡树的性质,并且可以非常简单 ...

  3. html div + css 下划线

    这里通过边框属性的虚线边框border控制虚线.以下设置的css 高度(css height)和css 宽度(css width)为350像素是为了便于观看演示 其它意思.一.四边为虚线边框borde ...

  4. mysql在表的某一位置增加一列的命令

    如果想在一个已经建好的表中添加一列,可以用诸如: alter table t1 add column addr varchar(20) not null; 这条语句会向已有的表t1中加入一列addr, ...

  5. XPath 文档 解析XMl

    http://www.zvon.org/xxl/XPathTutorial/General_chi/examples.html 简介 XPath由W3C的 XPath 1.0 标准描述.本教程通过实例 ...

  6. StringToInt(atoi) 字符串转换成整数

    public class StringToInt { public int atoi(String s) { long num = 0; int minus = 0; if(s==null) { re ...

  7. bzoj 2656 [Zjoi2012]数列(sequence) 递推+高精度

    2656: [Zjoi2012]数列(sequence) Time Limit: 2 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Descri ...

  8. graphlab 安装好后的导入配置

    本以为下好了的结果... 然后等个十几二十分钟... 关掉这个页面重新打开,再重新导入graphlab 貌似好了...但是,,, 发现是自己的文件放错盘了...在F盘... 然后就好啦~

  9. sqlserver设置具体的访问权限

    为具体的用户设置具体的访问权限 收藏 use 你的库名 go --新增用户 exec sp_addlogin 'test' --添加登录 exec sp_grantdbaccess N'test' - ...

  10. java:输出流程printStream

    // TODO 自动生成的方法存根 //路径文件 File file = new File("F:"+File.separator+"work"+File.se ...