POJ2831(次小生成树问题)
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 (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 思路:查询第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(次小生成树问题)的更多相关文章
- 2014.first[未填]
之后就按照自己的直觉,整理了第一套,难度为简单,差不多比2013noipday1水一点...先练练手而已 T1 vijos1196吃糖果游戏 博弈论 依题意,我们可知,如果去分数目为2,3,7,8必输 ...
- UVA-10600.Contest and Blackout.(Kruskal + 次小生成树)
题目链接 本题思路:模版的次小生成树问题,输出MST and Second_MST的值. 参考代码: #include <cstdio> #include <cstring> ...
- 次小生成树(Prim + Kruaskal)
问题引入: 我们先来回想一下生成树是如何定义的,生成树就是用n - 1条边将图中的所有n个顶点都连通为一个连通分量,这样的边连成子树称为生成树. 最小生成树很明显就是生成树中权值最小的生成树,那么我们 ...
- NetworkX系列教程(10)-算法之二:最小/大生成树问题
小书匠 Graph 图论 重头戏部分来了,写到这里我感觉得仔细认真点了,可能在NetworkX中,实现某些算法就一句话的事,但是这个算法是做什么的,用在什么地方,原理是怎么样的,不清除,所以,我决定 ...
- 【小程序分享篇 二 】web在线踢人小程序,维持用户只能在一个台电脑持登录状态
最近离职了, 突然记起来还一个小功能没做, 想想也挺简单,留下代码和思路给同事做个参考. 换工作心里挺忐忑, 对未来也充满了憧憬与担忧.(虽然已是老人, 换了N次工作了,但每次心里都和忐忑). 写写代 ...
- 【小程序分享篇 一 】开发了个JAVA小程序, 用于清除内存卡或者U盘里的垃圾文件非常有用
有一种场景, 手机内存卡空间被用光了,但又不知道哪个文件占用了太大,一个个文件夹去找又太麻烦,所以我开发了个小程序把手机所有文件(包括路径下所有层次子文件夹下的文件)进行一个排序,这样你就可以找出哪个 ...
- jQuery实践-网页版2048小游戏
▓▓▓▓▓▓ 大致介绍 看了一个实现网页版2048小游戏的视频,觉得能做出自己以前喜欢玩的小游戏很有意思便自己动手试了试,真正的验证了这句话-不要以为你以为的就是你以为的,看视频时觉得看懂了,会写了, ...
- 微信小程序开发心得
微信小程序也已出来有一段时间了,最近写了几款微信小程序项目,今天来说说感受. 首先开发一款微信小程序,最主要的就是针对于公司来运营的,因为,在申请appid(微信小程序ID号)时候,需要填写相关的公司 ...
- 前端网络、JavaScript优化以及开发小技巧
一.网络优化 YSlow有23条规则,中文可以参考这里.这几十条规则最主要是在做消除或减少不必要的网络延迟,将需要传输的数据压缩至最少. 1)合并压缩CSS.JavaScript.图片,静态资源CDN ...
随机推荐
- INSPIRED启示录 读书笔记 - 第6章 招聘产品经理
产品经理应有的特质 个人素质和态度:技术可以学习,素质却难以培养,有些素质是成功的产品经理必不可少的 对产品的热情:对产品有一种本能的热爱,是夜以继日克服困难.完善产品的动力 用户立场:能换位思考,能 ...
- win10打不开菜单且点击通知栏无反应的解决方法
1.在键盘上按下win+R键,或在开始菜单图标上点击右键选择"运行" 2.输入powershell,按下“确定”运行 3.在窗口里输入或复制粘贴以下命令,注意只有一行: Get-A ...
- 汽车AC键到底是干什么的?老司机告诉你
现在很多人都会开车,想我当初学车的时候一会就可以上手了,开车简单,但是很多细节方面的就是得慢慢学习的过程,比如说汽车的AC键,我相信很多车主,包括老司机都不知道到底有哪些作用,只知道开空调,其实它的用 ...
- spring boot 默认配置bug
问题场景:请求很耗时,当一次请求完成后,之后的20秒内的请求很快速,在之后的第一个请求很慢! 每隔一段时间,请求就会出发解压jar的操作,不确定是操作系统的问题还是sping-boot的bug &qu ...
- Apache Phoenix的序列
序列作为标准SQL特性,允许生成递增的序列并应用在典型的ID中.为了创建一个序列,可以使用: 0:jdbc:phoenix:SZB-L0023780:2181:/hbase114> CREATE ...
- CodeForces 266E More Queries to Array...(线段树+式子展开)
开始觉得是规律题的,自以为是的推了一个规律,结果测试数据都没过....看了love神的博客才发现只是把式子展开就找到规律了.不过挺6的是我虽然想错了,但是维护的的东西没有错,只是改改(改了进两个小时好 ...
- Http协议与生命周期
一.Http知识: 1.基于socket 浏览器(格式一) web服务器(格式一) MYSQL客户端(格式二) MYSQL服务端(格式三) ...
- 安装rackspace private cloud --4 配置Target hosts
在每个target host上执行以下操作: Naming target hosts. Install the operating system. Generate and set up securi ...
- RocketMQ原理讲解系列文章
[RocketMQ原理解析][http://blog.csdn.net/quhongwei_zhanqiu/article/category/2548637] [消息的可靠性.顺序和重复][https ...
- python的常见排序
在python程序中,我们往往自始至终都在与序列(列表.字典.元组)打交道,而最常用的操作就是对序列排序了.在此简单总结一下python中的排序. 基本排序方法 在python中,list对象具有 s ...